Simplest web server instance

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.