主题:初学者的一个很急的问题 帮帮忙哈~~
这个聊天室还没做完 只做了一部分遇到问题然后就做不下去了 我是个初学者 希望各位高手指教!!!!挺着急 ~ 问题是:如果只运行一个客户端的话可以将输入的数据传到服务端并打印出来 但是我开两个客户端就传不过去了 找不到原因在那~~帮帮我哈!!!!
客户端:
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
class ChatClient extends Frame implements ActionListener
{
Panel panel=new Panel();
Label lb=new Label("您的称呼:");
TextField tf1,tf2;
TextArea ta=new TextArea();
Button bt=new Button("点击连接");
Socket s=null;
DataOutputStream dos=null;
public void mod()
{
tf1=new TextField();
tf2=new TextField(8);
add(panel,BorderLayout.NORTH);
add(ta,BorderLayout.CENTER);
add(tf1,BorderLayout.SOUTH);
panel.setLayout(new FlowLayout(12));
panel.add(lb);
panel.add(tf2);
panel.add(bt);
pack();
setSize(300,400);
setVisible(true);
bt.addActionListener(new lianjie());
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
tf1.addActionListener(this);
//connect();
}
public void connect()
{
try
{
if (s==null)//如果SOCKET还没有连接的话则连接
{
s=new Socket("localhost",8888);
System.out.println("a client is connected");
}
}
catch (Exception e)
{
}/*finally
{
try
{
s.close();
}
catch (Exception e)
{
}
}*/
}
public void actionPerformed(ActionEvent e)
{
String str;
str=tf1.getText();
ta.setText(ta.getText()+tf2.getText()+":"+str+"\n");
tf1.setText("");
try
{
dos=new DataOutputStream(s.getOutputStream());
dos.writeUTF(tf2.getText()+":"+str);
dos.flush();
}
catch (Exception ee)
{
}
}
public static void main(String args[])
{
new ChatClient().mod();
}
class lianjie implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
connect();
}
}
}
服务器端:
import java.net.*;
import java.io.*;
class ChatServer
{
ServerSocket ss=null;
DataInputStream dis=null;
String str=null;
public static void main(String args[])
{
new ChatServer().resv();
}
public void resv()
{
Socket s=null;
try
{
ss=new ServerSocket(8888);
while(true)
{
s=ss.accept();
System.out.println("connected");
ClientNo c=new ClientNo(s);
new Thread(c).start();
}
}
catch (IOException e)
{
}/*finally
{
try
{
ss.close();
s.close();
}
catch (Exception e)
{
}
}*/
}
class ClientNo implements Runnable
{
private Socket s=null;
public ClientNo(Socket s)
{
this.s=s;
try
{
dis=new DataInputStream(s.getInputStream());
}
catch (Exception e)
{
}
}
public void run()
{
String str1=null;
try
{
while(true)
{
str1=dis.readUTF();
System.out.println(str1);
}
}
catch (Exception e)
{
}
}
}
}
客户端:
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
class ChatClient extends Frame implements ActionListener
{
Panel panel=new Panel();
Label lb=new Label("您的称呼:");
TextField tf1,tf2;
TextArea ta=new TextArea();
Button bt=new Button("点击连接");
Socket s=null;
DataOutputStream dos=null;
public void mod()
{
tf1=new TextField();
tf2=new TextField(8);
add(panel,BorderLayout.NORTH);
add(ta,BorderLayout.CENTER);
add(tf1,BorderLayout.SOUTH);
panel.setLayout(new FlowLayout(12));
panel.add(lb);
panel.add(tf2);
panel.add(bt);
pack();
setSize(300,400);
setVisible(true);
bt.addActionListener(new lianjie());
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
tf1.addActionListener(this);
//connect();
}
public void connect()
{
try
{
if (s==null)//如果SOCKET还没有连接的话则连接
{
s=new Socket("localhost",8888);
System.out.println("a client is connected");
}
}
catch (Exception e)
{
}/*finally
{
try
{
s.close();
}
catch (Exception e)
{
}
}*/
}
public void actionPerformed(ActionEvent e)
{
String str;
str=tf1.getText();
ta.setText(ta.getText()+tf2.getText()+":"+str+"\n");
tf1.setText("");
try
{
dos=new DataOutputStream(s.getOutputStream());
dos.writeUTF(tf2.getText()+":"+str);
dos.flush();
}
catch (Exception ee)
{
}
}
public static void main(String args[])
{
new ChatClient().mod();
}
class lianjie implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
connect();
}
}
}
服务器端:
import java.net.*;
import java.io.*;
class ChatServer
{
ServerSocket ss=null;
DataInputStream dis=null;
String str=null;
public static void main(String args[])
{
new ChatServer().resv();
}
public void resv()
{
Socket s=null;
try
{
ss=new ServerSocket(8888);
while(true)
{
s=ss.accept();
System.out.println("connected");
ClientNo c=new ClientNo(s);
new Thread(c).start();
}
}
catch (IOException e)
{
}/*finally
{
try
{
ss.close();
s.close();
}
catch (Exception e)
{
}
}*/
}
class ClientNo implements Runnable
{
private Socket s=null;
public ClientNo(Socket s)
{
this.s=s;
try
{
dis=new DataInputStream(s.getInputStream());
}
catch (Exception e)
{
}
}
public void run()
{
String str1=null;
try
{
while(true)
{
str1=dis.readUTF();
System.out.println(str1);
}
}
catch (Exception e)
{
}
}
}
}