C # One of the socket communication programming

Source: Internet
Author: User

information about socket programming (including examples) There are a lot of online, and I wrote this article was originally intended only to record some of their own experience. The socket provides an interface that allows programmers to send and receive data on the network conveniently. When programming with sockets, you first need to initialize a new instance of the socket class with the specified address family (such as common IPV4 or IPV6), socket type (such as stream, Dgram, and so on) and protocol (such as TCP, UDP, etc.), and then need to bind to the IP address and port.     These are the most basic conditions for programming with sockets. Socket programming usually sub-server and client, the server is responsible for receiving client requests and processing the request, must have multi-client processing capacity, performance requirements, stability and other requirements, the client after connecting the server to the server to send data or receive data from the server, and then processing,     relatively simple. On the server side, TCP communication as an example, the basic idea of socket network communication is:1, create a streaming socket and return the socket size, such as: M_serversocket =Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); 2, connect the socket to the local address, e.g. M_serversocket.bind (m_localendpoint); 3, start listening for client connection requests4, waiting for client connections5, the client establishes a connection with the server and gets a new socket, such as Clientsocket =m_serversocket.accept (); 6, read and write data on the socket until the data exchange is complete7after the service with the specified client finishes, the client's socket is closed8, shut down the server, such as M_serversocket.close ();     During the run of the server, steps 4th through 7th are a cyclic process in which each client session goes in the same processing mode. In the client, also take the TCP communication as an example, the socket realizes the network communication basic idea is:1, create a streaming socket and return the socket size, such as: M_clientsocket =Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); 2, connect the socket to the remote host (that is, the server), such as: M_serverendpoint =NewIPEndPoint (Ipaddress.parse (ServerIP), serverport); M_clientsocket.connect (M_serverendpoint); 3, if the connection is successful, start receiving data from the server side, such as:if(m_clientsocket.connected) {m_clientsocket.beginreceive (M_receivebuffer,0, M_receivebuffer.length,0,NewAsyncCallback (ReceiveCallback),NULL); }       4, as required, send data to the server, such as: M_clientsocket.send (M_sendbuffer, M_sendbuffer.length, Socketflags.none); 5, close socket

C # One of the socket communication programming

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.