C # connection-oriented sockets in Network Programming

Source: Internet
Author: User

                                                                                                                                                                                       

Server
Socket
BIND ()
Listen ()
Accept ()
Receive ()
Send ()
Close

To create a socket, you must bind the local IP address and port BIND (ipendpoint address) used for TCP communication)

The listen (INT backlog) Backlog parameter indicates that the system is waiting for the user.ProgramThe number of connections in the service queue. Any customer that exceeds the number of connections cannot access the server.

After the listen method is executed, the server is ready to receive any incoming connections, that is, the accept method is used. Socket myclient = mysocket. accect ();

When the program executes the accept method, it is paused until a client requests a connection. Once a client connects to the server, the myclient object contains all the connection information that communicates with the client. At this time, the client

The server can call the receive () and send () Methods to receive and send data.

Client
Socket
 
 
Connect ()
Send ()
Receive ()
Close

For client programs, the client must also bind an address to the created socket object. However, instead of using the bind method, the client uses the connect method:

For example:

IPaddress IP = IPaddress. parse ("127.0.0.1 ");
Ipendpoint IEP = new ipendpoint (IP, 8000 );

Socket socket = new socket (addressfamily. InterNetwork,
Sockettype. Stream, protocoltype. TCP );

Socket. Connect (IEP );

Once a connection is established, the client can use the send () and receive () Methods to communicate with each other just like the method used by the server. Note that after the communication is complete, you must stop the session with shutdown and then close the session.

Closed socket

For example:

Socket. Shutdown (socketshutdown. Both );

Socket. Close ();

This method allows the socket object to wait until the data in the internal buffer zone is sent.

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.