Teach you to write a Web server using asp.net

Source: Internet
Author: User

Write yourself a simple Web server that helps you understand the HTTP protocol better, let's take a look at a TcpListener Web server:

Class Program {static void Main (string[] args) {ipaddress address = Ipaddress.loopback;
    
            IPEndPoint endPoint = new IPEndPoint (address, 49152);
            TcpListener newserver = new TcpListener (endPoint); NewServer.
            Start ();
                
            Console.WriteLine ("Start listening ..."); while (true) {TcpClient newclient = NewServer.
                AcceptTcpClient ();
    
                Console.WriteLine ("Established Connection"); NetworkStream ns = newclient.
    
                GetStream ();
    
                System.Text.Encoding UTF8 = System.Text.Encoding.UTF8;
                Byte[] Request=new byte[4096]; int lehgth = ns.
                Read (Request, 0, 4096); String requeststring = UTF8.
    
                GetString (Request);
    
                Console.WriteLine (requeststring);
                String statusline = "http/1.1 ok\r\n"; byte[] statuslinebytes = UTF8.
 GetBytes (Statusline);   
                String responsebody = " 

Run the program, then open the browser, in the browser window, enter the address of the server: http://localhost:49152, then return to see the data returned by the server.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/aspx/

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.