/** * */ import java.io.*; import java.net.*; /** * @author xinyu126 * */ class Node{ String username; Socket socket; ObjectOutputStream out; ObjectInputStream in; Node next; public Node(){ username=null; socket=null; out=null; in=null; next=null; } } class Onlinelist{ Node root; Node point; int count; public Onlinelist(){ root=new Node(); root.next=null; point=root; count=0; } public void add(Node node){ point=root; while(point.next!=null) point=point.next; point.next=node; count++; System.out.println("online"+count); } public void del(Node node){ point=root; while(point.next!=null){ if(point.next==node){ point.next=node.next; count--; break; } point=point.next; } System.out.println("online"+count); } public Node find(String username){ point=root.next; while(point!=null){ if(point.username.equals(username)) return point; point=point.next; } return null; } public String showall(){ String str=""; point=root.next; while(point!=null){ str=str+"|"+point.username; point=point.next; } return str; } } public class server { ServerSocket socket; serverlistenthread sth; Node client; Onlinelist online; public void init(){ try{ socket=new ServerSocket(5126); //System.out.println("端口(5126)打开成功"); online=new Onlinelist(); sth=new serverlistenthread(socket,online); sth.start(); }catch(Exception err){ //System.out.println("端口打开失败!"); //System.out.println(err.getMessage()); System.exit(1); } } public static void main(String args[]){ server server=new server(); server.init(); } } class serverlistenthread extends Thread{ ServerSocket socket; Node client; Onlinelist online; serverclientthread clientthread; public serverlistenthread(ServerSocket socket,Onlinelist online){ this.socket=socket; this.online=online; } public void sendmsgtoall(String msg){ Node nodetemp=online.root.next; try{ while(nodetemp!=null){ nodetemp.out.write(0); nodetemp.out.flush(); nodetemp.out.writeObject(msg); nodetemp.out.flush(); nodetemp=nodetemp.next; } }catch(Exception e){ System.out.println(e.toString()); } } public void run(){ while(!socket.isClosed()){ client=new Node(); try{ //System.out.println("开始侦听"); client.socket=socket.accept(); //System.out.println("客户连接"); client.out=new ObjectOutputStream(client.socket.getOutputStream()); client.out.flush(); client.in=new ObjectInputStream(client.socket.getInputStream()); client.username=client.in.readObject().toString(); online.add(client); sendmsgtoall(client.username+"上线了"); sendmsgtoall("在线人数:"+online.count); sendmsgtoall(online.showall()); clientthread=new serverclientthread(online,client); clientthread.start(); }catch(Exception err){ System.out.println(err.getMessage()); } } } } class serverclientthread extends Thread{ Node client,tempnode; Onlinelist online; int system; String touser; public serverclientthread(Onlinelist online,Node client){ this.client=client; this.online=online; } public void run(){ try{ //开始侦听; while(!client.socket.isClosed()){ system=client.in.read(); System.out.println("system="+system); if(system==1){ touser=client.in.readObject().toString(); System.out.println("touser="+touser); tempnode=online.find(touser); System.out.println("temp="+tempnode); if(tempnode!=null){ tempnode.out.write(1); tempnode.out.flush(); tempnode.out.writeObject(touser); tempnode.out.flush(); System.out.print("发送1"); tempnode.out.writeObject(client.in.readObject()); tempnode.out.flush(); System.out.print("发送2"); tempnode.out.writeObject(client.in.readObject()); tempnode.out.flush(); System.out.print("发送3"); } else{ client.out.write(0); client.out.flush(); client.out.writeObject("系统消息:用户不在线"); client.out.flush(); client.in.readObject(); client.in.readObject(); } } else if(system==0){ client.out.write(0); client.out.flush(); client.out.writeObject("系统消息"); client.out.flush(); } else{ System.out.print(client.in.readObject().toString()); } // System.out.println("发送"); //client.out.flush(); } }catch(Exception err){ System.out.println(err.toString()); online.del(client); } } }