求一个用java socket写的聊天室程序,可以带源代码运行,包括客户端和服务器端。
服务器:
导入Java . io . *;
导入Java . net . *;
导入Java . util . *;
公共类ChatServer {
boolean started = false
ServerSocket ss = null
列表& lt客户端& gtclients = new ArrayList & lt客户端& gt();
公共静态void main(String[] args) {
新的ChatServer()。start();
}
public void start() {
尝试{
ss = new server socket(8888);
开始=真;
} catch (BindException e) {
System.out.println("端口正在使用中...");
System.out.println("请关闭相关程序,重新运行服务器!");
system . exit(0);
} catch (IOException e) {
e . printstacktrace();
}
尝试{
当(开始){
socket s = ss . accept();
客户c =新客户;
System.out.println("客户端已连接!");
新线程(c)。start();
clients . add(c);
}
} catch (IOException e) {
e . printstacktrace();
}最后{
尝试{
ss . close();
} catch (IOException e) {
e . printstacktrace();
}
}
}
类客户端实现Runnable {
私有套接字s;
私有DataInputStream dis = null
private data output stream dos = null;
private boolean b connected = false;
公共客户端(套接字){
this.s = s
尝试{
dis = new data inputstream(s . getinputstream());
dos = new data output stream(s . get output stream());
bConnected = true
} catch (IOException e) {
e . printstacktrace();
}
}
public void send(String str) {
尝试{
dos . write utf(str);
} catch (IOException e) {
e . printstacktrace();
}
}
公共无效运行(){
尝试{
while(b已连接){
string str = dis . read utf();
system . out . println(str);
for(int I = 0;我& ltclients . size();i++) {
client c = clients . get(I);
发送(str);
}
}
} catch (EOFException e) {
System.out.println("客户端关闭!");
} catch (IOException e) {
e . printstacktrace();
}最后{
尝试{
如果(dis!= null)dis . close();
如果(dos!= null)dos . close();
如果(s!= null) {
s . close();
//s = null;
}
} catch (IOException e1) {
e 1 . printstacktrace();
}
}
}
}
}
客户端,打开两个就可以聊天了...
导入Java . awt . *;
导入Java . awt . event . *;
导入Java . io . *;
导入Java . net . *;
公共类ChatClient扩展框架{
套接字s = null
DataOutputStream dos = null
DataInputStream dis = null
private boolean b connected = false;
TextField TF txt = new TextField();
TextArea taContent = new TextArea();
Thread tRecv = new Thread(new RecvThread());
公共静态void main(String[] args) {
新聊天客户端()。launch frame();
}
公共void launchFrame() {
setLocation(400,300);
this.setSize(300,300);
add(tfTxt,BorderLayout。南);
add(taContent,BorderLayout。北);
pack();
this . addwindowlistener(new window adapter(){
@覆盖
public void window closing(window event arg 0){
disconnect();
system . exit(0);
}
});
TF txt . addactionlistener(new TF listener());
set visible(true);
connect();
trecv . start();
}
公共void connect() {
尝试{
s =新插座(" 127.0.0.1 ",8888);
dos = new data output stream(s . get output stream());
dis = new data inputstream(s . getinputstream());
System.out.println("connected!");
bConnected = true
} catch (UnknownHostException e) {
e . printstacktrace();
} catch (IOException e) {
e . printstacktrace();
}
}
公共void disconnect() {
尝试{
dos . close();
dis . close();
s . close();
} catch (IOException e) {
e . printstacktrace();
}
}
私有类TFListener实现ActionListener {
public void action performed(action event e){
String str = tfTxt.getText()。trim();
TF txt . settext(" ");
尝试{
dos . write utf(str);
dos . flush();
} catch (IOException e1) {
e 1 . printstacktrace();
}
}
}
私有类RecvThread实现Runnable {
公共无效运行(){
尝试{
while(b已连接){
string str = dis . read utf();
tacontent . settext(tacontent . gettext()+str+' \ n ');
}
} catch (SocketException e) {
System.out.println("?再见!”);
} catch (IOException e) {
e . printstacktrace();
}
}
}
}