当前位置: 代码迷 >> Java相关 >> 有关通讯器的问题!
  详细解决方案

有关通讯器的问题!

热度:140   发布时间:2007-04-18 22:35:13.0
有关通讯器的问题!

最近我在学做通讯器!做了简单的服务端和客户端,但是出现了些问题,希望各位可以帮帮忙!
问题:两个运行后,服务端看不到客户端输入的字!
代码:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class client extends JFrame implements ActionListener{
Socket sock;
JTextArea t1=new JTextArea();
JTextField t2=new JTextField(20);
JButton b1=new JButton("send");
JButton b2=new JButton("link...");
DataOutputStream out;
DataInputStream in;
public client()
{
JScrollPane jsp=new JScrollPane(t1);
this.getContentPane().add(jsp,"Center");
JPanel p1=new JPanel();
p1.add(t2);
p1.add(b1);
JPanel p2=new JPanel();
p2.add(b2);
this.getContentPane().add(p2,"North");
this.getContentPane().add(p1,"South");
b1.addActionListener(this);
b2.addActionListener(this);
setTitle("client");
setSize(340,200);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{try{
out.writeUTF("bye");
}
catch(Exception ee)
{}
dispose();
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
if(!t2.getText().equals("")){
try{
out.writeUTF(t2.getText());
t1.append("client say:"+t2.getText()+"\n");
}
catch(Exception ee){}
}
}
else{
try{
sock=new Socket("127.0.0.1",6000);
OutputStream os=sock.getOutputStream();
out=new DataOutputStream(os);
InputStream is=sock.getInputStream();
in=new DataInputStream(is);
out.writeUTF("ok");
Communion th=new Communion(this);
th.start();
}
catch(IOException ee){
JOptionPane.showMessageDialog(null,"err");
return;
}
}
}
public static void main(String f[])
{
client mainframe=new client();
}
}
class Communion extends Thread {
client fp;
Communion(client fp){
this.fp=fp;
}
public void run()
{
String msg=null;
while(true)
{
try{
msg=fp.in.readUTF();
if(msg.equals("bye")){
fp.t1.append("close\n");
break;
}
fp.t1.append("sever say:"+msg+"\n");
}
catch(Exception ee){break;
}
}
try{
fp.out.close();
fp.in.close();
fp.sock.close();
}
catch(Exception ee){}
}
}

****************************************************************8
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class sever extends JFrame implements ActionListener{
ServerSocket severSock;
Socket sock;
JTextArea t1=new JTextArea();
JTextField t2=new JTextField(20);
JButton b1=new JButton("send");
DataOutputStream out;
DataInputStream in;
String cname=null;
public sever()
{
try{
severSock =new ServerSocket(6000);
}
catch(IOException e){
JOptionPane.showMessageDialog(null,"err");
return;
}
JScrollPane jsp=new JScrollPane(t1);
this.getContentPane().add(jsp,"Center");
JPanel p1=new JPanel();
p1.add(t2);p1.add(b1);
this.getContentPane().add(p1,"South");
b1.addActionListener(this);
setTitle("sever");
setSize(340,200);
setVisible(true);
try{
sock =severSock.accept();
out=new DataOutputStream(sock.getOutputStream());
in=new DataInputStream(sock.getInputStream());
out.writeUTF("ok");
Communion th=new Communion(this);
th.start();
}
catch(Exception e){}
addWindowListener(new WindowAdapter()
{
public void windowCloseing(WindowEvent e){
try{
out.writeUTF("bye");
}
catch(Exception ee){}
dispose();
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e){
if(! t2.getText().equals("")){
try{
out.writeUTF(t2.getText());
t1.append("sever say:"+t2.getText()+"\n");
}
catch(Exception ee){}
}
}


public static void main(String d[])
{
sever mainFrame=new sever();
}
}


class Communion extends Thread{
sever fp;
Communion(sever fp){
this.fp=fp;
}
public void run()
{
String msg=null;
while(true){
try{
msg=fp.in.readUTF();
if(msg.equals("bye")){
fp.t1.append("leave\n");
break;
}
fp.t1.append("client say:"+msg+"\n");
}
catch(Exception ee){break;}
}
try{
fp.out.close();
fp.in.close();
fp.sock.close();
fp.severSock.close();
}
catch(Exception ee){}
}
}

搜索更多相关的解决方案: 通讯器  

----------------解决方案--------------------------------------------------------
  相关解决方案