I made a little improvement on the basis of the previous socket communication, using multithreading to use, the program is more concise and practical. Please advise me of the shortcomings!
Words do not say much, before the essay also has introduced, directly on the code!
Server socket (ServerSocket):
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading.Tasks;6 usingSystem.Net;7 usingSystem.Threading;8 usingSystem.Net.Sockets;9 Ten namespaceServerSocketTest6 One { A class Program - { - StaticSocket ServerSocket; the Static intPort =2723; - - Static voidMain (string[] args) - { +Console.WriteLine ("Service side:"); - + //instantiating the Socket class AServerSocket =NewSocket (addressfamily.internetwork,sockettype.stream,protocoltype.tcp); atIPAddress IP = ipaddress.parse ("173.168.5.15"); - //Identify network addresses -EndPoint point =NewIPEndPoint (ip,port); - //bind IP Address and port number - Serversocket.bind (point); - in //Start listening for client connections -Serversocket.listen (Ten); toConsole.WriteLine ("waiting for client to connect ..."); + -Thread MyThread =NewThread (listenclientsocket); the Mythread.start (); * $ Panax Notoginseng Console.readkey (); - the + A } the + /// <summary> - ///Listening Client $ /// </summary> $ Static voidListenclientsocket () - { - while(true) the { -Socket clientsocket = serversocket.accept ();//accepting connections from clientsWuyiConsole.WriteLine ("Client Connection succeeded"); the - stringMessage ="2017, New start, Happy New Year! Come on, keep trying!!!"; Wu byte[] Data=Encoding.UTF8.GetBytes (message); - clientsocket.send (data); About $Thread receive =NewThread (Receivesocket);//Receivesocket method of being passed -Receive. Start (Clientsocket);//Clientsocket Parameters passed - } - A } + the /// <summary> - ///receiving messages from the client $ /// </summary> the Static voidReceivesocket (ObjectClientsocket)//////The parameter here is the parameter in the thread, the parameter type must be object type the { theSocket Myclientsocket = (socket) Clientsocket;////Convert parameter of type object to socket type use parameter to start thread, execute subsequent code the while(true) - { in byte[] Data=New byte[1024x768]; the intLength =myclientsocket.receive (data); the stringMessage=encoding.utf8.getstring (data,0, length); AboutConsole.WriteLine ("receive a message from the client:"+message); the the the } + } - } the}
The client's code basically hasn't changed ~
Client socket (Clientsocket):
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Net;usingSystem.Threading;usingSystem.Net.Sockets;namespaceclientsockettest6{classProgram {StaticSocket Clientsocket; Static intPort =2723; Static voidMain (string[] args) {Console.WriteLine ("Client:"); //The power of a socket classClientsocket =Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); IPAddress IP= Ipaddress.parse ("173.168.5.15"); //client request connection to the serverclientsocket.connect (IP, port); Console.WriteLine ("The server connection was successful."); //Accept Data byte[] data =New byte[1024x768]; intLength =clientsocket.receive (data); stringMessage = Encoding.UTF8.GetString (data,0, length); Console.WriteLine ("receive the blessing from the service side:"+message); //Send to the server a number of for(inti =0; I <Ten; i++) {Thread.Sleep ( -);//make the thread pause (hibernate) 2s stringMessage2 ="fighting!!";//can be written as String Message2=console.readline (); byte[] Data2 =Encoding.UTF8.GetBytes (Message2); Clientsocket.send (DATA2); Console.WriteLine ("send Message to server: {0}", Message2); } console.readkey (); } }}
C#socket Communication-----Multithreading