A simple Web server

Source: Internet
Author: User

       Static voidMain (string[] args) {IPAddress localaddress= Ipaddress.loopback;//get the IP address of this machineIPEndPoint EndPoint =NewIPEndPoint (LocalAddress,49155); Socket Socket=NewSocket (ADDRESSFAMILY.INTERNETWORK,SOCKETTYPE.STREAM,PROTOCOLTYPE.TCP); Socket.            Bind (EndPoint); Socket. Listen (Ten);  while(true) {Console.WriteLine ("Request Connection Waiting ..."); Socket Clientsocket=socket.                Accept (); Console.WriteLine ("the address of the client is: {0}", Clientsocket.remoteendpoint); byte[] buffer =New byte[2048]; intReceivelength = clientsocket.receive (buffer,2048, Socketflags.none); stringrequeststring = Encoding.UTF8.GetString (buffer,0, receivelength);                Console.WriteLine (requeststring); //the service side to make the appropriate content                stringStatusline ="http/1.1 ok\r\n"; byte[] Responsestatuslinebytes =Encoding.UTF8.GetBytes (Statusline); stringResponsebody =""; stringResponseheader =string. Format ("content-type:text/html; charset=utf-8\r\ncontent-length: {0}\r\n", responsebody.length); byte[] Responsebodybytes =Encoding.UTF8.GetBytes (responsebody); byte[] Responseheaderbytes =Encoding.UTF8.GetBytes (Responseheader); //send a status line to the clientclientsocket.send (responsestatuslinebytes); //send a response header message to the clientclientsocket.send (responseheaderbytes); //send headers and empty lines of contentClientsocket.send (New byte[] { -,Ten }); //want the client to send the body partclientsocket.send (responsebodybytes); //Disconnect ConnectionClientsocket.close ();                Console.readkey ();  Break; }            //shutting down the serversocket.        Close (); }

Source Address: http://www.cnblogs.com/zhili/archive/2012/08/23/WebServer.html

Reference Address: http://www.cnblogs.com/mcad/p/4343358.html

A simple Web server

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.