Introduction to WinSock programming-4. connection-oriented communication

Source: Internet
Author: User
Connection-oriented communication is implemented through the TCP/IP protocol. TCP enables reliable data transmission between two computers. Program During TCP communication, a virtual connection is established between the target and the source computer. Once the connection is established, two-way data stream transmission can be performed between the target and the source computer.

Number of server APIs

A server is a process waiting for any number of customers to connect. The server aims to provide services for customer requests. the server must use a common name to listen to the connection. in TCP/IP, the name is the IP address and port number of the Local interface. each protocol has different addressing methods, so their naming methods are also different. in WinSock programming, the first step is to call socket or wsasocket and bind the socket to the Universal Naming protocol. BIND is completed through the API method. next, set the socket to the listening mode through the API function listen. finally, when the customer tries to connect, use the API function accept or wsaaccept to accept the connection.

Bind

Once a specific protocol is created, you must bind it to a public address. The API function associates the socket and the public address. The function declaration is as follows:

Int BIND (
Socket s,
Const struct sockaddr far * Name,
Int namelen
);

Parameter description:

S: The socket waiting for the customer to connect.
Name: IsSockaddr Structure, You must use the protocol used for filling, and convert it to the sockaddr structure. The Winsock header file defines the sockaddr structureSockaddrType.
Namelen: Specifies the length of the Protocol address structure.
 
The following example shows how to use bind in TCP:

Socket S;
Sockaddr_in tcpaddr;
Int Port = 5150;

S = socket (af_inet, sock_stream, ipproto_tcp );

Tcpaddr. sin_family = af_inet;
Tcpaddr. sin_port = htons (port );
Tcpaddr. sin_addr.s_addr = htonl (inaddr_any );

BIND (S, (sockaddr *) & tcpaddr, sizeof (tcpaddr ));

In the preceding example, a stream socket is created and the TCP/IP address structure used to accept the client connection is set up. in this example, the socket is bound to an inaddr_any address and occupies port 5150. we can clearly specify an IP address, but inaddr_any allows us to bind to all available interfaces in the system, so that the customer can use any interface (of course, the port number must be correct) the connection will be accepted by our listening socket. to call bind, you usually use the local IP address and port to establish an association with the socket.

Once an error occurs,BIND will returnSocket_Error.Bind usually produces errors:Wsaeaddrinuse. In TCP/IP,Wsaeaddrinuse indicates that another process has been bound to a local IP address and port, or the specified IP address and port are inTime_wait statusIf you call bind to a bound socketWsaefault.

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.