Share a foreign site to see the socket program, the program is not strong, but can see from the code of the foreigner is how to write code, we can learn from each other

Source: Internet
Author: User

The original: To share a foreign site to see the socket program, the program is not strong, but can see from the code of the foreigner is how to write code, we can learn from each other

Source code: Http://www.zuidaima.com/share/1550463702764544.htm

Package com.zuidaima.xs;/*****************************************************************version:1.0*date:02/ 03/2007* Author:www.zuidaima.com**description*this is a Server Side application of Chat System.*thi s application is used for receiving the messages from all Client*and send to all and every client and in this we can main Tain The*list of all online users.**remarks*this application are unable to provide the private chatting facility*********** /import Java.io.*;import Java.net.*;import java.util.*; public class Myserver{serversocket SS; Socket s; ArrayList al=new ArrayList (); ArrayList al1=new ArrayList (); ArrayList al2=new ArrayList (); ArrayList alname=new ArrayList (); Socket s1,s2; MyServer () throws Ioexception{ss=new ServerSocket (1004);//Create Server Socketwhile (True) {s=ss.accept ();//accept the Client sockets1=ss.accept (); s2=ss.accept (); Al.add (s);//Add the client socket in Arraylistal1.add (S1); Al2.add (S2); SYstem.out.println ("Client is Connected"); MyThread2 m=new MyThread2 (s2,al2,alname); New thread for maintaning the list of user Namethread t2=new thread (m); T2.start (); MyThread r=new MyThread (s,al);//new thread for receive and sending the Messagesthread t=new thread (r); T.start (); MyThread1 my=new MyThread1 (S1,AL1,S,S2); New thread for update the list of user Namethread t1=new thread (my); T1.start ();}} public static void Main (string[] args) {try{new MyServer ();} catch (IOException e) {}}}//class is used to update the list of user Nameclass MyThread1 implements Runnable{socket S1,s,s2 ; static ArrayList Al1;datainputstream Ddin; String sname; MyThread1 (Socket s1,arraylist al1,socket s,socket s2) {this.s1=s1;this.al1=al1;this.s=s;this.s2=s2;} public void Run () {try{ddin=new datainputstream (S1.getinputstream ()), while (true) {Sname=ddin.readutf (); System.out.println ("Exit:" +sname); MyThread2.alname.remove (sname);//remove the logout user name from Arraylistmythread2.every (); Al1.remove (S1); MyThread.al.remOve (s); MyThread2.al2.remove (S2); if (Al1.isempty ()) system.exit (0); All client have been Logout}}catch (Exception IE) {}}}//class is used to maintain the list of all online Usersclass Mythre Ad2 implements Runnable{socket s2;static ArrayList al2;static ArrayList alname;static datainputstream din1;static DataOutputStream dout1; MyThread2 (Socket s2,arraylist al2,arraylist alname) {this.s2=s2;this.al2=al2;this.alname=alname;} public void Run () {try{din1= new DataInputStream (S2.getinputstream ()); Alname.add (Din1.readutf ());//Store the user name In Arraylistevery ();} catch (Exception oe) {System.out.println ("Main expression" +oe);}} Send the list of user name to all clientstatic void every () throws Exception{iterator I1=al2.iterator (); Socket St1;while (I1.hasnext ()) {st1= (socket) i1.next ();d out1=new DataOutputStream (St1.getoutputstream ()); O Bjectoutputstream obj=new ObjectOutputStream (DOUT1); Obj.writeobject (Alname); Write the list of users in stream of all Clientsdout1.flush (); Obj.flush ();}}} Class isUsed to receive the message and send it to all Clientsclass MyThread implements Runnable{socket s;static ArrayList Al;data InputStream Din;dataoutputstream dout; MyThread (Socket S, ArrayList al) {This.s=s;this.al=al;} public void Run () {String str;int i=1;try{din=new datainputstream (S.getinputstream ());} catch (Exception e) {}while (i==1) {Try{str=din.readutf ();//read the Messagedistribute (str);} catch (IOException e) {}}}//send it to all clientspublic void Distribute (String str) throws Ioexception{iterator I=al.itera Tor (); Socket St;while (I.hasnext ()) {st= (socket) i.next ();d out=new DataOutputStream (St.getoutputstream ());d Out.writeutf (     STR);d Out.flush ();}}}


Share a foreign site to see the socket program, the program is not strong, but can see from the code of the foreigner is how to write code, we can learn from each other

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.