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