Java 仿QQ聊天源代码(二)

2014-11-24 09:10:14 · 作者: · 浏览: 1
user
socket = new Socket("127.0.0.1", 8090);
// scoket=new Socket(loclhost, 8090)
String ip = Inet4Address.getLocalHost().getHostAddress();
StringBuffer sr = new StringBuffer("USERS:");
sr.append(ip).append(":").append(juser.getText().trim());
OutputStream os = socket.getOutputStream();
PrintWriter pw = new PrintWriter(os);
pw.println(sr.toString());
pw.flush();
ClientThread ct = new ClientThread(this, socket);
ct.start();
send.setEnabled(true);
exit.setEnabled(true);
connect.setEnabled(false);
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
if (name.equals("发送")) {
System.out.println(socket.toString());
// 群聊
StringBuffer mess = new StringBuffer("MESS");
System.out.println("以下是信息!");
System.out.println(jcb.getSelectedItem().toString());
if (jcb.getSelectedItem().toString().equals("USER")) {
mess.append(":").append("USERS").append(":")
.append(juser.getText().trim()).append(":")
.append(jmess.getText().trim());
try {
PrintWriter pw = new PrintWriter(socket.getOutputStream());

System.out.println(mess.toString());
pw.println(mess.toString());
pw.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

jmess.setText("");
} else {
mess.append(":").append(jcb.getSelectedItem().toString()).append(":")
.append(juser.getText().trim()).append(":")
.append(jmess.getText().trim());
try {
PrintWriter pw = new PrintWriter(socket.getOutputStream());

System.out.println("我是私聊"+mess.toString());
pw.println(mess.toString());
pw.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

jmess.setText("");
}
}

}

}