java c/s server

來源:互聯網
上載者:User

 /**<br /> *<br /> */</p><p>import java.io.*;<br />import java.net.*;<br />/**<br /> * @author xinyu126<br /> *<br />*/<br />class Node{<br />String username;<br />Socket socket;<br />ObjectOutputStream out;<br />ObjectInputStream in;<br />Node next;<br />public Node(){<br />username=null;<br />socket=null;<br />out=null;<br />in=null;<br />next=null;<br />}<br />}<br />class Onlinelist{<br />Node root;<br />Node point;<br />int count;<br />public Onlinelist(){<br />root=new Node();<br />root.next=null;<br />point=root;<br />count=0;<br />}<br />public void add(Node node){<br />point=root;<br />while(point.next!=null)<br />point=point.next;<br />point.next=node;<br />count++;<br />System.out.println("online"+count);<br />}<br />public void del(Node node){<br />point=root;<br />while(point.next!=null){<br />if(point.next==node){<br />point.next=node.next;<br />count--;<br />break;<br />}<br />point=point.next;<br />}<br />System.out.println("online"+count);<br />}<br />public Node find(String username){<br />point=root.next;<br />while(point!=null){<br />if(point.username.equals(username))<br />return point;<br />point=point.next;<br />}<br />return null;<br />}<br />public String showall(){<br />String str="";<br />point=root.next;<br />while(point!=null){<br />str=str+"|"+point.username;<br />point=point.next;<br />}<br />return str;<br />}</p><p>}</p><p>public class server {</p><p>ServerSocket socket;<br />serverlistenthread sth;<br />Node client;<br />Onlinelist online;<br />public void init(){</p><p>try{<br />socket=new ServerSocket(5126);<br />//System.out.println("連接埠(5126)開啟成功");<br />online=new Onlinelist();<br />sth=new serverlistenthread(socket,online);<br />sth.start();<br />}catch(Exception err){<br />//System.out.println("連接埠開啟失敗!");<br />//System.out.println(err.getMessage());<br />System.exit(1);<br />}</p><p>}<br />public static void main(String args[]){<br />server server=new server();<br />server.init();<br />}</p><p>}</p><p>class serverlistenthread extends Thread{<br />ServerSocket socket;<br />Node client;<br />Onlinelist online;<br />serverclientthread clientthread;<br />public serverlistenthread(ServerSocket socket,Onlinelist online){<br />this.socket=socket;<br />this.online=online;<br />}<br />public void sendmsgtoall(String msg){<br />Node nodetemp=online.root.next;<br />try{<br />while(nodetemp!=null){<br />nodetemp.out.write(0);<br />nodetemp.out.flush();<br />nodetemp.out.writeObject(msg);<br />nodetemp.out.flush();<br />nodetemp=nodetemp.next;<br />}<br />}catch(Exception e){<br />System.out.println(e.toString());<br />}<br />}<br />public void run(){<br />while(!socket.isClosed()){<br />client=new Node();<br />try{<br />//System.out.println("開始偵聽");<br />client.socket=socket.accept();<br />//System.out.println("客戶串連");<br />client.out=new ObjectOutputStream(client.socket.getOutputStream());<br />client.out.flush();<br />client.in=new ObjectInputStream(client.socket.getInputStream());<br />client.username=client.in.readObject().toString();<br />online.add(client);<br />sendmsgtoall(client.username+"上線了");<br />sendmsgtoall("線上人數:"+online.count);<br />sendmsgtoall(online.showall());<br />clientthread=new serverclientthread(online,client);<br />clientthread.start();<br />}catch(Exception err){<br />System.out.println(err.getMessage());<br />}<br />}<br />}<br />}</p><p>class serverclientthread extends Thread{<br />Node client,tempnode;<br />Onlinelist online;<br />int system;<br />String touser;<br />public serverclientthread(Onlinelist online,Node client){<br />this.client=client;<br />this.online=online;<br />}<br />public void run(){<br />try{<br />//開始偵聽;<br />while(!client.socket.isClosed()){<br />system=client.in.read();<br />System.out.println("system="+system);<br />if(system==1){<br />touser=client.in.readObject().toString();<br />System.out.println("touser="+touser);<br />tempnode=online.find(touser);<br />System.out.println("temp="+tempnode);<br />if(tempnode!=null){<br />tempnode.out.write(1);<br />tempnode.out.flush();<br />tempnode.out.writeObject(touser);<br />tempnode.out.flush();<br />System.out.print("發送1");<br />tempnode.out.writeObject(client.in.readObject());<br />tempnode.out.flush();<br />System.out.print("發送2");<br />tempnode.out.writeObject(client.in.readObject());<br />tempnode.out.flush();<br />System.out.print("發送3");<br />}<br />else{<br />client.out.write(0);<br />client.out.flush();<br />client.out.writeObject("系統訊息:使用者不線上");<br />client.out.flush();<br />client.in.readObject();<br />client.in.readObject();<br />}<br />}<br />else if(system==0){<br />client.out.write(0);<br />client.out.flush();<br />client.out.writeObject("系統訊息");<br />client.out.flush();<br />}<br />else{<br />System.out.print(client.in.readObject().toString());<br />}<br />//System.out.println("發送");<br />//client.out.flush();<br />}<br />}catch(Exception err){<br />System.out.println(err.toString());<br />online.del(client);<br />}<br />}<br />}<br />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.