1. Server side
1. Instantiate and set the socket instance object
a. Create an IP address and port
b.Bind the listening address
c. Set the number of allowed simultaneous accesses
2. Listen to the connection
a. By starting a new thread to execute, the main thread will not fake it (the parameters brought by starting the thread must be of object type)
b. Use a loop to wait for the connection and return a socket instance responsible for communication
c. The IP address of the connected customer service can be obtained from the returned socket instance
3. Receive messages sent by customer service
a. Start a new thread execution in the listening method
b. Use a loop to obtain the sent message. Calling the method to obtain the message requires passing a byte variable parameter as a container. The return value of the method is int, indicating the number of valid bytes obtained
c. If the number of valid bytes is 0, the loop will jump out.
d. Received the return message to the customer service
4. Console program server-side code
using System; using ; using ; using ; using ; namespace ServerSocket { class Program { static void Main(string[] args) { ("Hello World!"); Socket serverSocket = new Socket(, ); IPAddress ip = ; IPEndPoint point = new IPEndPoint(ip, 2333); //Socket bound listener address (point); ("Listen Success"); //Set the number of connections at the same time (10); //Use the thread background to execute listening, otherwise the program will be faked Thread thread = new Thread(Listen); = true; (serverSocket); (); } /// <summary> /// Listen to the connection /// </summary> /// <param name="o"></param> static void Listen(object o) { var serverSocket = o as Socket; while (true) { //Waiting for connection and creating a socket responsible for communication var send = (); //Get the IP address of the link var sendIpoint = (); ($"{sendIpoint}Connection"); //Open a new thread to receive messages constantly Thread thread = new Thread(Recive); = true; (send); } } /// <summary> /// Receive message /// </summary> /// <param name="o"></param> static void Recive(object o) { var send = o as Socket; while (true) { //Get the sent message container byte[] buffer = new byte[1024 * 1024 * 2]; var effective = (buffer); // If the valid byte is 0, skip if (effective == 0) { break; } var str = Encoding.(buffer,0, effective); (str); var buffers = Encoding.("Server Return Message"); (buffers); } } } }
2. Client
1. Instantiate and connect to socket instance object
a. Create an IP address and port (the server's IP and port)
b. Establish links with the server side
2. Receive messages sent by the server
a. Start a new thread to execute
b. Use a loop to obtain the sent message. Calling the method to obtain the message requires passing a byte variable parameter as a container. The return value of the method is int, indicating the number of valid bytes obtained
c. If the number of valid bytes is 0, the loop will jump out.
3. Send a message to the server
a. Call the send() method of the socket object to send directly
4. Console program client code
using System; using ; using ; using ; using ; namespace SocketClient { class Program { static void Main(string[] args) { ("Hello World!"); //Create an instance Socket socketClient = new Socket(, ); IPAddress ip = ("192.168.0.111"); IPEndPoint point = new IPEndPoint(ip, 2333); //Connect (point); //Receive messages sent by the server constantly Thread thread = new Thread(Recive); = true; (socketClient); //Send data to the server constantly int i = 0; while (true) { i++; var buffter = Encoding.($"Test Send Message:{i}"); var temp = (buffter); (1000); } } /// <summary> /// Receive message /// </summary> /// <param name="o"></param> static void Recive(object o) { var send = o as Socket; while (true) { //Get the sent message byte[] buffer = new byte[1024 * 1024 * 2]; var effective = (buffer); if (effective == 0) { break; } var str = Encoding.(buffer, 0, effective); (str); } } } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.