Follow the last socket sever and append the server and Tcplistener of tcplistener and Httplistener.

Source: Internet
Author: User

Follow the last socket sever and append the server and Tcplistener of tcplistener and Httplistener.
1. Build a web server using Tcplistener

1. Like socket, Tcplistener is actually an encapsulation of socket to facilitate programming. initialize tcplistener and start listening.

1 // initialize the endpoint Information 2 IPAddress address = IPAddress. loopback; 3 IPEndPoint endPoint = new IPEndPoint (address, 23333); 4 // initialize and start the listener 5 TcpListener server = new TcpListener (endPoint); 6 server. start (); 7 Console. writeLine ("Start listening... \ n ");

2. Add the listening Tcpclient in a loop for corresponding processing. In this case, unlike socket, Tcplistener returns the listening request to a NetworkStream object.

It is convenient for programmers to program and use stream-based reading and writing.

1 while (true) 2 {3 // pending and accepting request 4 TcpClient client = server. acceptTcpClient (); 5 Console. writeLine ("Create a connection. "); 6 // get the network stream object 7 NetworkStream ns = client. getStream (); 8 // use utf8 to transcode 9 System. text. encoding utf8 = System. text. encoding. UTF8; 10 byte [] request = new byte [4096]; 11 int length = ns. read (requests, 0, 4096); 12 string requestString = utf8.GetString (request, 0, length); 13 Console. writeLine (requestString); 14 15 // initialization status line 16 string statusLine = "HTTP/1.1 200 OK \ r \ n"; 17 byte [] statusLineBytes = utf8.GetBytes (statusLine ); 18 19 // initialize the response body 20 string responseBody = "

3. At this point, the web server has been built

2. Build a Web Server based on HttpListener

1. The HttpListener class is provided in the. net namespace System. This class can only work with Http. sys System components. Therefore, check whether the System supports it. Initialize the HttpListener object and start listening.

1 // check whether the system supports 2 if (! HttpListener. isSupported) 3 throw new System. invalidOperationException ("Your system must be winXP or Server 2003 or later"); 4 string [] prefixes = new string [] {"http: // localhost: 23333 /"}; 5 6 // initialize HttpListener 7 HttpListener listener = new HttpListener (); 8 foreach (string str in prefixes) 9 listener. prefixes. add (str); 10 11 // enable listening 12 listener. start (); 13 Console. writeLine ("Start listening... \ n ");

2. Call GetContext to obtain the context object and parse the request and response from the object. Obtain request information through attributes such as HttpMethod, AcceptTypes, userages, UserAgent, and Headers of the request. Configure the sending back information by setting response attributes. Use the standard Stream object to send back the response

1 while (true) 2 {3 // The blocking thread knows the request arrival 4 HttpListenerContext context = listener. getContext (); 5 // GET request information 6 HttpListenerRequest request = context. request; 7 Console. writeLine ("{0} {1} HTTP/1.1", request. httpMethod, request. rawUrl); 8 Console. writeLine ("Accept: {0}", string. join (",", request. acceptTypes); 9 Console. writeLine ("Accept-Encoding: {0}", request. headers ["Accept-Encoding"]); 10 11 // get the response object 12 HttpListenerResponse response = context. response; 13 // initialize the response body 14 string responseString = @ "

3. Demo completed

 

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.