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. The IPEndPoint class (endpoint) is a combination of IP addresses and ports (IP: Point). It uniquely identifies an application of a computer on 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. the Socket class 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:
// Configure //----------------------------------------------------------------------------------------
IPEndPoint endPoint = new IPEndPoint (IPAddress. Parse ("192.168.43.104"), 8080 );
// Create a socket and use an ipv4 address. The transmission protocol is tcp, Which is 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: {0}", endPoint. Port );
While (true)
{
// Start listening. This method blocks thread execution until it receives connection requests from a client.
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 );
// Prepare the webpage sent to the client
String responseBody = "Byte [] responseBodyBytes = Encoding. UTF8.GetBytes (responseBody );
// Response Header
String responseHeader = string. Format ("Content-type: text/html; charset = UTF-8 \ r \ nContent-Length: {0} \ r \ n", responseBody. Length );
Byte [] responseHeaderBytes = Encoding. UTF8.GetBytes (responseHeader );
Client. Send (statusLineBytes); // Send status information
Client. Send (responseHeaderBytes); // sends a Response Header
Client. Send (new byte [] {13, 10}); // split the line between the header and content
Client. Send (responseBodyBytes); // sends content
Client. Close (); // disconnect from the client
If (Console. KeyAvailable)
Break; // jump out of the endless loop
}
// Close the server
Socket. Close ();
Because the http protocol is stateless, the server automatically disconnects from the client every time the server completes one or more requests to keep the server's resources
Running result:
Source code http://www.bkjia.com/uploadfile/2012/0425/20120425090640204.rar
Click to download the children's shoes that want to run the results. For more information, see.
From wlitsoft