1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Net;8 usingSystem.Net.Sockets;9 usingSystem.Text;Ten usingSystem.Threading; One usingSystem.Threading.Tasks; A usingSystem.Windows.Forms; - - namespace_02chatserver the { - Public Partial classChatserver:form - { - PublicChatserver () + { -Control.checkforillegalcrossthreadcalls =false; + InitializeComponent (); A } atSocket Sklisten =NULL;//for the server to be able to control the interception of the stop, the listening socket is declared outside. -dictionary<string, socket> dicclient =Newdictionary<string, socket>(); - Private voidButton1_Click (Objectsender, EventArgs e) - { - if(Sklisten = =NULL) - { in //open a socket to listen for communication port -Sklisten =Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); to } + //binding IP and port numbers -Sklisten.bind (NewIPEndPoint (Ipaddress.parse ("127.0.0.1"),9999)); the //set the queue for listening *Sklisten.listen (Ten); $Lblstate.forecolor =Color.green;Panax NotoginsengLblstate.font =NewFont ("blackbody", -); -Lblstate.text ="Listening in:"; the + A theThread thread =NewThread (NewThreadStart (() = { + //continuously receives the user's connection request. - while(true) $ { $ //prepare the client connection; -Socket skconnetion = sklisten.accept ();//This sentence will block the main thread, so put him in a separate thread. - //The IP port number and socket are set to a set when the client connects the Dicclient.add (skConnetion.RemoteEndPoint.ToString (), skconnetion); - ListboxClients.Items.Add (skConnetion.RemoteEndPoint.ToString ());WuyiTxtlog.appendtext (skConnetion.RemoteEndPoint.ToString () +"it's connected ."+Environment.NewLine); the //When a user connects, it will block the current thread if it does not send a message. All the threads that are about to create another thread to receive the user's message. - //using line pool to create a thread to perform a client-side receive user message, the following Lamada expression implements the socket for the internal access client connection of the function through the incoming client connection socket. This method can be used to pass in the client socket when the input is not an Lanmeda expression. WuThreadPool.QueueUserWorkItem (NewWaitCallback (obj=>{ -Socket skreceive = skconnetion asSocket; About $ byte[] buffer=New byte[1024x768*1024x768*4]; - //because the message sent by the client is not only sent one, it is repeated to receive the user's message repeatedly - while(true) - { A + intCount=skreceive.receive (buffer); the if(Count = =0) - { $ //the user exited theTxtlog.appendtext ("Client"+skreceive.remoteendpoint.tostring () +"has exited"); the Dicclient.remove (skReceive.RemoteEndPoint.ToString ()); the ListboxClients.Items.Remove (skReceive.RemoteEndPoint.ToString ()); the Break; - } in stringmsg = Encoding.UTF8.GetString (buffer,0, count); theTxtlog.appendtext ("Client"+skconnetion.remoteendpoint.tostring () +"to send a message:"+msg+environment.newline);//Easy Wermen Special the } About the }), skconnetion); the } the + })); -Thread. IsBackground =true; the thread. Start ();Bayi the the } - - Private voidButton3_Click (Objectsender, EventArgs e) the { the //determine if the IP port you want to communicate with is selected the if(Listboxclients.selecteditem! =NULL) the { - //based on the user's selected port, find the scoket of the communication theSocket SK =dicclient[listboxclients.selecteditem.tostring ()]; the if(SK! =NULL) the {94 //converts a user-entered message into a byte array the byte[] buffer =Encoding.UTF8.GetBytes (TxtSendMsg.Text.Trim ()); the byte[] sendbuffer=New byte[Buffer. length+1]; the 98Buffer.blockcopy (Buffer,0, Sendbuffer,1, buffer. Length); About //sent a message to the client - SK. Send (sendbuffer);101 //Write Log102Txtlog.appendtext ("server to ["+ listboxClients.SelectedItem.ToString () +"] Sent:"+ Txtsendmsg.text +Environment.NewLine);103 }104 } the Else106 {107MessageBox.Show ("Please select the client you want to send the message to");108 }109 } the 111 Private voidButton4_Click (Objectsender, EventArgs e) the {113 if(Listboxclients.selecteditem! =NULL) the { the //1. Select the socket for communication based on the selected client port IP theSocket skclient=dicclient[listboxclients.selecteditem.tostring ()];117Skclient.send (New byte[] {2});//send a flag and then prompt the client to send a splash screen118 119 } - Else121 {122MessageBox.Show ("Please select the client to send");123 }124 } the }126}
04Socket Network programming Server notation