public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } public String getSource() { return source; } public void setSource(String source) { this.source = source; } public String getTarget() { return target; } public void setTarget(String target) { this.target = target; } public String getType() { return type; } public void setType(String type) { this.type = type; }
}package com.tsing.chat.server; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.Hashtable;import com.tsing.thread.ServerThread; public class Server {
可以看看这里的
http://blog.csdn.net/preferme/article/details/4360958http://blog.csdn.net/preferme/article/details/5667002
public class ClientThread implements Runnable {
ClientFrame frame;
public ClientThread(ClientFrame frame){
this.frame=frame;
}
public void run() {
try {
while(true){
ObjectInputStream objin =new ObjectInputStream(frame.socket.getInputStream());
MsgBean bean=(MsgBean) objin.readObject();
// 得到的信息往窗体上的list里写,list会自动更新显示
frame.list.add( bean.getSource()+" 说: "+bean.getInfo()+"\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.tsing.chat.frame;
import java.io.Serializable;public class MsgBean implements Serializable{ String type;
String source;
String target;
String info;
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}package com.tsing.chat.server;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Hashtable;import com.tsing.thread.ServerThread;
public class Server {
Hashtable allsocket=new Hashtable ();
public Server(){
try {
ServerSocket serverSocket=new ServerSocket(9000);
while(true) {
Socket socket= serverSocket.accept();
//启动线程
new Thread(new ServerThread(allsocket,socket)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Server();
}
}
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Hashtable; import com.tsing.chat.frame.MsgBean;public class ServerThread implements Runnable { Hashtable allsocket;
Socket wo;
public ServerThread(Hashtable allsocket, Socket wo) {
this.allsocket = allsocket;
this.wo = wo;
}
public void run() {
try {
while (true) {
InputStream in = wo.getInputStream();
ObjectInputStream objIn = new ObjectInputStream(in);
// 还原对象
MsgBean bean = (MsgBean) objIn.readObject();
if (bean.getType().equals("请求登陆")) {
allsocket.put(bean.getSource(), wo);
} else if (bean.getType().equals("请求聊天")) {
// 向所有客户 转发消息
for (Object obj : allsocket.keySet()) {
Socket s = (Socket) allsocket.get(obj);
OutputStream out = s.getOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(
out);
objectOutputStream.writeObject(bean);
objectOutputStream.flush();
} } else if (bean.getType().equals("请求下线")) {
allsocket.remove(bean.getSource());
break;
} } } catch (Exception e) {
e.printStackTrace();
} }}