In this case, some classes and methods are first listed one by one.
1. The IPAddress class is used to indicate an IP address.
1.1 IPAddress. Parse ("192.168.43.104") converts a string of IP addresses to IP addresses.
1.2 IPAddress. Loopback get the local Loopback address 127.0.0.1
2. IPEndPoint class (endpoint)Simply put, it is the combination of IP addresses and ports (IP: Point). It can uniquely identify an application of a computer in the network.
IPEndPoint endPoint = new IPEndPoint (IPAddress. Parse ("192.168.43.104"), 8080); // creates a network endPoint whose IP address is 192.168.43.104 with port number 8080
2.1.1 Address: obtain or set the IP Address of the endPoint ex: endPoint. Address // 192.168.43.104
2.1.2 AddressFamily get network protocol ex: endPoint. AddressFamily // http
2.1.3 AddressPoint: Obtain the port number. ex: endPoint. Point // 8080
3. Socket classIt is located in the System. Net. Socket namespace and encapsulates Socket operations.
3.1 set the Listen-based socket to enter the listening status and set the length of the waiting queue. Ex: socket. Listen (10) // only 10 clients are allowed to send requests simultaneously
3.2 Accept waits for a new connection. When the new connection arrives, a Socket object for the new connection is returned. That is, each client that establishes a connection has a Socket object on the server. Through this object, the client can communicate with the server. Ex: Socket client = socket. Accept ();
3.3 Receive uses Socket to accept byte data and saves it to one byte data. It returns an int type data (the actual number of bytes received ).
Ex: // create a buffer
Byte [] buffer = new byte [2048];
// Accept data
Int length = client. Receive (buffer, buffer. Length, SocketFlags. None );
3.4 "Send" indicates "send data". "Yes," Send "sends data pre-stored in the byte array through Socket.
The complete example code is as follows:
// Define IPEndPoint endPoint = new IPEndPoint (IPAddress. parse ("192.168.43.104"), 8080); // creates a socket using an ipv4 address. The transmission protocol is tcp, bidirectional, reliable, and connection-based byte stream Socket socket = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); // bind it to an endpoint socket. bind (endPoint); // set the length of the connection queue socket. listen (10); Console. writeLine ("start listening, Port Number: {0} ", endPoint. Port); while (true) {// start listening. This method blocks thread execution until it receives a client Connection Request Socket client = socket. accept (); // output client address Console. writeLine ("client address: {0}", client. remoteEndPoint); // create a buffer byte [] buffer = new byte [2048]; // accept data int length = client. receive (buffer, buffer. length, SocketFlags. none); // convert the request data to UTF-8 // Encoding utf8 = Encoding. UTF8; string requestString = Encoding. UTF8.GetString (buffer, 0, length); // display the request message Console. writeLine (requestString); // response status line string statusLine = "HTTP/1.1 200 OK \ r \ n"; byte [] statusLineBytes = Encoding. UTF8.GetBytes (statusLine ); // string responseBody = "
Because the http protocol isStateless connectionThe server is automatically disconnected from the client every time the request is completed or multiple times to keep the server's resources
Running result:
For more information about the source code of this article, see