Simple TcpListener-based Web server instance

Source: Internet
Author: User
Tags getstream

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

Related Article

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.