[java]代码库
package com.qq.MyClient;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import com.qq.user.SendToMessage;
import com.qq.user.user;
public class MyServer {
ServerSocket server;
public MyServer() {
try {
server=new ServerSocket(8000);
while(true){
System.out.println("服务器正在监听....");
Socket socket=server.accept();
ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
user u=(user)ois.readObject();
ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
SendToMessage sm=new SendToMessage();
//判断密码
if("123456".equals(u.getUserPd())){
sm.setMessType("1");
oos.writeObject(sm);
sendMessageToClient smt=new sendMessageToClient(socket);
// Thread t=new Thread(smt);
magaeMessage.addSendMessage(u.getUserId(),smt);
smt.start();
}else{
sm.setMessType("2");
oos.writeObject(sm);
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// public static void main(String[] args) {
// new MyServer();
// }
}
class sendMessageToClient extends Thread{
Socket socket;
public sendMessageToClient(Socket socket) {
this.socket=socket;
}
@Override
public void run() {
SendToMessage sm;
while(true){
try {
ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
sm=(SendToMessage)ois.readObject();
sendMessageToClient st=magaeMessage.checksendMessageToClient(sm.getGetter());
ObjectOutputStream oos=new ObjectOutputStream(st.socket.getOutputStream());
oos.writeObject(sm);
System.out.println();
System.out.println(sm.getSender()+"给"+sm.getGetter()+"说:"+sm.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class magaeMessage{
public static Map<String, sendMessageToClient> hm =new HashMap<String,sendMessageToClient>();
public static void addSendMessage(String id,sendMessageToClient mc){
hm.put(id, mc);
}
public static sendMessageToClient checksendMessageToClient(String id){
return (sendMessageToClient)hm.get(id);
}
}
初级程序员
by: 埃楚斯卡熊 发表于:2013-12-10 11:24:27 顶(0) | 踩(0) 回复
代码不全,,注释也少,没有价值。。
回复评论