1. Create a console project. The Code is as follows:
Static void Main (string [] args)
{
IPAddress address = IPAddress. Loopback;
IPEndPoint endPoint = new IPEndPoint (addresses, 50000 );
TcpListener newServer = new TcpListener (endPoint );
NewServer. Start (10 );
Console. WriteLine ("Start listening... ");
While (true)
{
TcpClient newClient = newServer. AcceptTcpClient ();
Console. WriteLine ("a connection is established ");
NetworkStream ns = newClient. GetStream ();
System. Text. Encoding utf8 = System. Text. Encoding. UTF8;
Byte [] buffer = new byte [4096];
Int size = ns. Read (buffer, 0, 4096 );
String requestString = utf8.GetString (buffer, 0, length); // receives request information
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
// Output Response Information
Ns. Write (statusLineBytes, 0, statusLineBytes. Length );
Ns. Write (responseHeaderBytes, 0, responseHeaderBytes. Length );
Ns. Write (new byte [] {13, 10}, 0, 2 );
Ns. Write (responseBodyBytes, 0, responseBodyBytes. Length );
NewClient. Close ();
If (Console. KeyAvailable)
{
Break;
}
}
NewServer. Stop ();
}
Static void Main (string [] args)
{
IPAddress address = IPAddress. Loopback;
IPEndPoint endPoint = new IPEndPoint (addresses, 50000 );
TcpListener newServer = new TcpListener (endPoint );
NewServer. Start (10 );
Console. WriteLine ("Start listening... ");
While (true)
{
TcpClient newClient = newServer. AcceptTcpClient ();
Console. WriteLine ("a connection is established ");
NetworkStream ns = newClient. GetStream ();
System. Text. Encoding utf8 = System. Text. Encoding. UTF8;
Byte [] buffer = new byte [4096];
Int size = ns. Read (buffer, 0, 4096 );
String requestString = utf8.GetString (buffer, 0, length); // receives request information
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
// Output Response Information www.2cto.com
Ns. Write (statusLineBytes, 0, statusLineBytes. Length );
Ns. Write (responseHeaderBytes, 0, responseHeaderBytes. Length );
Ns. Write (new byte [] {13, 10}, 0, 2 );
Ns. Write (responseBodyBytes, 0, responseBodyBytes. Length );
NewClient. Close ();
If (Console. KeyAvailable)
{
Break;
}
}
NewServer. Stop ();
}
2. Run the project
3. as follows
From hi_dzj's column