A simple Stocket Programming Web server

Source: Internet
Author: User

In. NET, the System.Net namespace provides most of the data types for network programming, as well as common operations, which are commonly used in the following types:

The IPAddress class represents an IP address.

The IPEndPoint class represents an IP address and a good combination of ports.

The System.Net.Socket namespace provides data types that are based on stocket programming.

The socket class encapsulates the operation of the socket.

Common operations are as follows:

Listen: Sets the length of the connection queue.

Accept: Waits for a new connection, and returns a Stocket object for the row connection when the communication arrives.

Receive: Receives direct data through the stock, saves it in a byte array, and returns the actual number of bytes received.

Send: The data that is pre-persisted in the byte array is sent through Stocket.

Example code:

  

Static voidMain (string[] args) {            //get the loopback network address for this machineIPAddress address =Ipaddress.loopback; //Create an accessible endpointIPEndPoint EndPoint =NewIPEndPoint (Address,9494); //creating sockets, using IPV4 addresses, Transmission Control Protocol TCP, bidirectional, reliable, connection-based byte streamsSocket socket =Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp)            ; //to tell the socket to bind to the endpointsocket.            Bind (EndPoint); //set the length of the connection queueSocket. Listen (Ten); Console.WriteLine ("start listening, port number: {0}. ", Endpoint.port);  while(true)            {                //start listening, this method blocks the execution of the thread until it receives a connection request from a clientSocket client =socket.                Accept (); //Output Client AddressConsole.WriteLine (client.                Remoteendpoint); //ready to read the data requested by the client, and the data read will be stored in an array                byte[] buffer =New byte[4096]; //Receive Data                intLength = client. Receive (Buffer,4096, Socketflags.none); //Please translate the requested data into UTF-8System.Text.Encoding UTF8 =System.Text.Encoding.UTF8; stringrequeststring = UTF8. GetString (Buffer,0, length); //display the requested messageConsole.WriteLine (requeststring); //responding to execution status                stringStatusline ="http/1.1 ok\r\n"; byte[] Statuslinebytes =UTF8.                GetBytes (Statusline); //prepare a webpage to send to the client                stringResponsebody =""; byte[] Responsebodybytes =UTF8.                GetBytes (responsebody); //response to the head                stringResponseheader =string. Format ("content-type:text/html;charset=utf-8\r\ncontent-length:{0}\r\n", responsebody.length); byte[] Responseheaderbytes =UTF8.                GetBytes (Responseheader); //sending status information to clientsclient.                Send (statuslinebytes); //send a response header to the clientclient.                Send (responseheaderbytes); //header-to-content separation lineClient. Send (New byte[] { -,Ten }); //Send content section to clientclient.                Send (responsebodybytes); //disconnecting from the clientclient.                Close (); if(console.keyavailable) Break; }            //shutting down the serversocket.        Close (); }

After running, enter http://localhost:9494/in the browser, which will appear as an effect

  

In the command window you can see that the road says the output effect

  

A simple Stocket Programming 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.