1. Create a console project with the following code:
Static void Main (string [] args)
{
// Define the IP address
IPAddress address = IPAddress. Loopback;
IPEndPoint endPoint = new IPEndPoint (addresses, 50000 );
// Create a socket object
Socket socket = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp );
// Bind
Socket. Bind (endPoint );
// Listen
Socket. Listen (10 );
Console. WriteLine ("start listening, Port: {0}", endPoint. Port );
While (true)
{
// Accept
Socket clientSocket = socket. Accept ();
Console. WriteLine ("a connection is established, peer endpoint {0}", clientSocket. RemoteEndPoint );
// Start accepting information from the other party and display
Byte [] buffer = new byte [4096];
Int length = clientSocket. Receive (buffer, 4096, SocketFlags. None );
System. Text. Encoding utf8 = System. Text. Encoding. UTF8;
String requestString = utf8.GetString (buffer, 0, length );
Console. WriteLine (requestString );
// Send Response Information
String statusLine = "HTTP/1.1 200 OK \ r \ n ";
Byte [] statusLineBytes = utf8.GetBytes (statusLine); // status line
String responseBody = "Byte [] responseBodyBytes = utf8.GetBytes (responseBody); // content
String responseHeader = String. Format ("Content-Type: text/html; charset = UTF-8 \ r \ nContent-Length: {0} \ r \ n ",
ResponseBody. Length );
Byte [] responseHeaderBytes = utf8.GetBytes (responseHeader); // Response Header
// Send a message
ClientSocket. Send (statusLineBytes );
ClientSocket. Send (responseHeaderBytes );
ClientSocket. Send (new byte [] {13, 10 });
ClientSocket. Send (responseBodyBytes );
// Disconnect from the client
ClientSocket. Close ();
If (Console. KeyAvailable)
{
Break;
}
}
Socket. Close ();
}
Static void Main (string [] args)
{
// Define the IP address www.2cto.com
IPAddress address = IPAddress. Loopback;
IPEndPoint endPoint = new IPEndPoint (addresses, 50000 );
// Create a socket object
Socket socket = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp );
// Bind
Socket. Bind (endPoint );
// Listen
Socket. Listen (10 );
Console. WriteLine ("start listening, Port: {0}", endPoint. Port );
While (true)
{
// Accept
Socket clientSocket = socket. Accept ();
Console. WriteLine ("a connection is established, peer endpoint {0}", clientSocket. RemoteEndPoint );
// Start accepting information from the other party and display
Byte [] buffer = new byte [4096];
Int length = clientSocket. Receive (buffer, 4096, SocketFlags. None );
System. Text. Encoding utf8 = System. Text. Encoding. UTF8;
String requestString = utf8.GetString (buffer, 0, length );
Console. WriteLine (requestString );
// Send Response Information
String statusLine = "HTTP/1.1 200 OK \ r \ n ";
Byte [] statusLineBytes = utf8.GetBytes (statusLine); // status line
String responseBody = "Byte [] responseBodyBytes = utf8.GetBytes (responseBody); // content
String responseHeader = String. Format ("Content-Type: text/html; charset = UTF-8 \ r \ nContent-Length: {0} \ r \ n ",
ResponseBody. Length );
Byte [] responseHeaderBytes = utf8.GetBytes (responseHeader); // Response Header
// Send a message
ClientSocket. Send (statusLineBytes );
ClientSocket. Send (responseHeaderBytes );
ClientSocket. Send (new byte [] {13, 10 });
ClientSocket. Send (responseBodyBytes );
// Disconnect from the client
ClientSocket. Close ();
If (Console. KeyAvailable)
{
Break;
}
}
Socket. Close ();
}
2. Run the project
3. Open your browser and enter http: // localhost: 50000/in the address bar. You can use httpwatch to get a rough picture of the process of your browser and server.
From hi_dzj's column