The last time the socket server side is just appetizers, this time we put the last code to perfect and append.
The form is as follows
I've rewritten the code according to the information I read.
And a lot of comments on the source code, I hope you can read
Public partial class Form1:form {public Form1 () {InitializeComponent (); }//proxy delegate delegate void Flushsocket (); The collection of all proxy sockets list<socket> agentsocketlist = new list<socket> (); private void Btnopen_click (object sender, EventArgs e) {//Create a Socket object Socket Seversocket = New Socket (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); Bind port and IP ipendpoint endPoint = new IPEndPoint (Ipaddress.parse (txtip.text), Int. Parse (Txtport.text)); Seversocket.bind (EndPoint); Start monitoring Seversocket.listen (10); Use a different thread to connect to the client ThreadPool.QueueUserWorkItem (new WaitCallback (Callbacksocket), seversocket); }//callback function public void Callbacksocket (object obj) {//Parameter obj is strongly turned into socket type socket SE Versocket = (Socket) obj; Start connection//mainThe thread will be stuck here until the connection to a client//return value is a proxy communication socket object//May connect multiple clients, need to loop while (true) { Socket socket = seversocket.accept (); Add proxy socket Agentsocketlist.add (socket) to the collection; Troubleshoot cross-thread access control issues if (this.txtLog.InvokeRequired) {//flushsocket fs = new Fl Ushsocket (Callbacksocket); Txtlog.invoke (new Flushsocket () = {flushmsg (socket); })); } else {flushmsg (socket); }}}//Show connection received public void flushmsg (socket socket) {Txtlog.text = "has been Connection: "+ socket. Remoteendpoint + "\ r \ n" + this.txtLog.Text; Receive Data ThreadPool.QueueUserWorkItem (new WaitCallback (recmsgfromclient), socket); }//Receive data publicvoid Recmsgfromclient (Object obj) {socket socket = (socket) obj; 1M buffer byte[] recbyte = new byte[1024 * 1024]; while (true) {//The received data is stored in the buffer//because the Receive method blocks the thread, another thread is required to wait for I NT count = socket. Receive (recbyte, 0, Recbyte.length, socketflags.none); The other side exits if (count <= 0) {if (this.txtLog.InvokeRequired) {Txtlog.invoke (new flushsocket () = { Txtlog.text = string. Format ("message from client {0}; the other side exited \r\n{1}", socket. Remoteendpoint, Txtlog.text); })); } else {//assigns a value to the receiving text box Txtlog.text = Stri Ng. Format ("message from client {0}; the other side exited \r\n{1}", socket. Remoteendpoint, Txtlog.text); } //Close the proxy socket socket. Shutdown (Socketshutdown.both); Socket. Close (); Agentsocketlist.remove (socket); Return }//Convert data in the buffer to string str = Encoding.Default.GetString (recbyte, 0, Count); if (this.txtLog.InvokeRequired) {Txtlog.invoke (New Flushsocket () => ; {Txtlog.text = string. Format ("content from client {0}"; { 1}\R\N{2} ", socket. Remoteendpoint, str, txtlog.text); })); } else {//assigns a value Txtlog.text = string to the receiving text box. Format ("content from client {0}"; { 1}\R\N{2} ", socket. Remoteendpoint, str, txtlog.text); }}}//Send message to client private void Btnsend_click (object sender, EventArgs e) {//Traverse each proxy socket foreach (var sOcket in Agentsocketlist) {//If the connection is maintained, the data is transmitted if (socket. Connected) {byte[] Mybuffer = Encoding.Default.GetBytes (Txtsend.text); Socket. Send (Mybuffer,0,mybuffer.length,socketflags.none); } } } }Here is the display effect
On the right is the client, we can first do not care, after I will write the client, here is used by others to verify the server side.
I use "123" to send and receive, no problem, no bug found
Full version of the socket server side