我正在做一个客户机-服务器聊天项目。其中客户端A和客户端B应该连接到服务器。我可以连接客户端A和服务器,但客户端B出现问题。
我想把客户端A和客户端B连接到服务器。客户端A连接成功。我也想和客户A和客户B沟通,但是我失败了,如果有人帮我的话,我会非常感激的。
谢谢
String msgin = "";
try{
ss= new ServerSocket(5000);
s= ss.accept();
din= new DataInputStream(s.getInputStream());
dout= new DataOutputStream(s.getOutputStream());
while(!msgin.equals("exit")) {
msgin = din.readUTF();
msg_area.setText(msg_area.getText().trim()+"\n Client A:\t"+msgin);
}
}catch(IOException e){
}
String msgIn = "";
try{
ss= new ServerSocket(6000);
s= ss.accept();
din= new DataInputStream(s.getInputStream());
dout= new DataOutputStream(s.getOutputStream());
while(!msgIn.equals("exit")) {
msgIn = din.readUTF();
msg_area.setText(msg_area.getText().trim()+"\n Clint B:\t"+msgIn);
}
}catch(Exception e){
}
客户端A
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyClientSocket().setVisible(true);
}
});
try{
s = new Socket("127.0.0.1",5000);
din = new DataInputStream(s.getInputStream());
dout = new DataOutputStream(s.getOutputStream());
String msgin="";
while(!msgin.equals("exit")){
msgin = din.readUTF();
msg_area.setText(msg_area.getText().trim()+"\n Server:\t"+msgin);
}
}catch(Exception e){
}
客户乙
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyClientServerB().setVisible(true);
}
});
try{
s = new Socket("127.0.0.1",6000);
din = new DataInputStream(s.getInputStream());
dout = new DataOutputStream(s.getOutputStream());
String msgIn="";
while(!msgIn.equals("exit")){
msgIn = din.readUTF();
msg_area.setText(msg_area.getText().trim()+"\n Server:\t"+msgIn);
}
}catch(Exception e){
}