This article mainly for you to share the C # socket simple console case, with a certain reference value, interested in small partners can refer to
One, server-side
1. Instantiate and set the socket instance object
A. Creating an IP address and port
B. Binding listening Address
C. Set the number of simultaneous accesses allowed
2. Monitoring the connection
A. By initiating a new thread execution so that the main thread does not feign animation (start thread, the parameter must be of type Object)
B. Using a loop to wait for a connection and return a socket instance responsible for communication
C. The IP address of the connected pickup can be obtained in the returned socket instance
3. Receive the message sent by customer service
A. Start a new thread execution in the Listener method
B. Using loops to get messages sent over, calling the method that gets the message needs to pass a byte variable argument as a container. The method return value is int, which indicates the number of valid bytes fetched
C. Jump out of the loop if the number of valid bytes is 0
D. Receive message to customer service return message
4. Console program server-side code
Using system;using system.net;using system.net.sockets;using system.text;using system.threading;namespace Serversocket{class Program {static void Main (string[] args) {Console.WriteLine ("Hello world!"); Socket ServerSocket = new socket (sockettype.stream, protocoltype.tcp); IPAddress IP = ipaddress.any; IPEndPoint point = new IPEndPoint (IP, 2333); Socket binding Listener Address serversocket.bind (point); Console.WriteLine ("Listen Success"); Set the number of simultaneous connections Serversocket.listen (10); Use the thread background to perform the listening, or the program will be suspended animation thread thread = new Thread (Listen); Thread. IsBackground = true; Thread. Start (ServerSocket); Console.read (); }///<summary>/////</summary>//<param name= "o" ></param> static void Listen (OBJEC t o) {var serversocket = o as Socket; while (true) {//waits for a connection and creates a socket var send = Serversocket.accept () that is responsible for communication; Gets the IP address of the link var sendipoint = send. Remoteendpoint.tostring (); Console.WriteLine ($ "{sendipoint}connection"); Open a new threadKeep receiving messages thread thread = new Thread (Recive); Thread. IsBackground = true; Thread. Start (send); }}////<summary>/////</summary>//<param name= "o" ></param> static void Recive (ob Ject o) {var send = o as Socket; while (true) {//Gets the message container sent over byte[] buffer = new BYTE[1024 * 1024 * 2]; var effective = send. Receive (buffer); A valid byte of 0 skips the IF (effective = = 0) {break; } var str = Encoding.UTF8.GetString (buffer,0, effective); Console.WriteLine (str); var buffers = Encoding.UTF8.GetBytes ("Server Return Message"); Send. Send (buffers); } } }}
Second, the client
1. Instantiate and connect the socket instance Object
A. Creating an IP address and port (server IP and port)
B. Establishing links with the server side
2. Messages sent by the receiving server
A. Starting a new thread execution
B. Using loops to get messages sent over, calling the method that gets the message needs to pass a byte variable argument as a container. The method return value is int, which indicates the number of valid bytes fetched
C. Jump out of the loop if the number of valid bytes is 0
3. Send a message to the server
A. Calling the Send () method of the socket object sends directly
4. Console program client code
Using system;using system.net;using system.net.sockets;using system.text;using system.threading;namespace Socketclient{class Program {static void Main (string[] args) {Console.WriteLine ("Hello world!"); Create an instance socket Socketclient = new socket (sockettype.stream, protocoltype.tcp); IPAddress IP = ipaddress.parse ("192.168.0.111"); IPEndPoint point = new IPEndPoint (IP, 2333); To connect Socketclient.connect (point); Constant receiving server-side messages sent thread thread = new Thread (Recive); Thread. IsBackground = true; Thread. Start (socketclient); Sending data to the server int i = 0; while (true) {i++; var buffter = Encoding.UTF8.GetBytes ($ "Test Send message:{i}"); var temp = socketclient.send (buffter); Thread.Sleep (1000); }}///<summary>///Receive messages///</summary>//<param name= "o" ></param> static void Recive (Object o) {var send = o as Socket; while (true) {//Gets the message sent over byte[] buffer = new BYTE[1024 * 1024 * 2]; var effective =Send. Receive (buffer); if (effective = = 0) {break; } var str = Encoding.UTF8.GetString (buffer, 0, effective); Console.WriteLine (str); } } }}