Message format:
Directly run the Code:
Using System; using System. collections. generic; using System. text; using System. net; using System. net. sockets; namespace SimpleSocketServer {class Program {static void Main (string [] args) {// obtain the local Loopback network address, such as 127.0.0.1 IPAddress = IPAddress. loopback; // create an accessible endpoint. 49152 indicates the port number. If it is set to 0, it indicates setting an idle port IPEndPoint endpoint = new IPEndPoint (address, 49152 ); // create a Socket using an IPV4 address and the transport protocol TCP. bidirectional, reliable, and connection-based byte stream Socket socket = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); // bind the Socket to the socket on the endpoint. bind (endpoint); // set the length of the connection queue socket. listen (10); Console. writeLine ("Start listening port {0}", endpoint. port); while (true) {// start listening. This method will block thread execution until it receives a client response Socket client = socket. accept (); // output the client address Console. writeLine (client. remoteEndPoint); // prepare the request for the interface client and save it in an array byte [] buffer = new byte [4 096]; // receive data int length = client. receive (buffer, 4096, SocketFlags. none); System. text. encoding utf8 = System. text. encoding. UTF8; // convert the data type to UTF-8 string requestring = utf8.GetString (buffer); // display the client's response Console. writeLine (requestring); // response status line string statusline = "HTTP/1.1 200 OK \ r \ n"; byte [] statuslinebytes = utf8.GetBytes (statusline ); // The webpage sent to the client string requestHtml = "
Access address:
Http: // 127.0.0.1: 49152/
Effect: