C + + Socket Programming Tutorial Five: Use the bind () and connect () functions __oracle

Source: Internet
Author: User
Tags htons
The socket () function is used to create sockets, determines the various properties of the socket, and then the server side uses the bind () function to bind the socket to a specific IP address and port, so that data flowing through the IP address and port is passed to the socket processing, and the client uses connect () function to establish a connection. bind () functionThe following prototype of the BIND () function is:
int bind (int sock, struct sockaddr *addr, socklen_t addrlen);  Linux
int bind (SOCKET sock, const struct SOCKADDR *addr, int addrlen);  Windows
Here's what Linux is for example, and Windows is similar to this. Sock is a socket file descriptor, addr as a pointer to a sockaddr struct variable, addrlen to the size of a addr variable, which can be computed by sizeof ().

The following code binds the socket created with the IP address 127.0.0.1, port 1234:
Create socket int serv_sock = socket (af_inet, sock_stream, ipproto_tcp); Create sockaddr_in structure variable struct sockaddr_in serv_addr; memset (&serv_addr, 0, sizeof (SERV_ADDR)); Each byte is filled with 0 serv_addr.sin_family = af_inet; Use IPv4 address serv_addr.sin_addr.s_addr = inet_addr ("127.0.0.1"); The specific IP address Serv_addr.sin_port = htons (1234); Port//socket and IP, port binding bind (Serv_sock, (struct sockaddr*) &serv_addr, sizeof (SERV_ADDR)); Here we use the SOCKADDR_IN structure and then cast it to the sockaddr type, and then we'll explain why. sockaddr_in Structural BodyNext you might want to look at the SOCKADDR_IN structure, which has the following member variables:
struct sockaddr_in{sa_family_t sin_family;//Address family (address family), that is, addresses type uint16_t sin_port;//16-bit port number struct in_addr sin _ADDR; 32-bit IP address char sin_zero[8]; Not used, generally with 0 fill}; 1 The first parameter of sin_family and socket () has the same meaning, and the value should be consistent.

2) Sin_prot is the port number. The length of the uint16_t is two bytes, and theoretically the port number is 0~65536, but 0~1023 ports are generally allocated by the system to a particular service program, such as the port number of the Web service is 21 for the 80,FTP service, so our program should try to 1024~ Allocate the port number between 65536.

The port number needs to be converted using the htons () function, which is explained later.

3) SIN_ADDR is a variable of struct in_addr struct type, which is explained in detail below.

4) Sin_zero[8] is an excess of 8 bytes, which is not used, and is generally populated with a memset () function of 0. In the above code, first use memset () to fill all the bytes of the structure to 0, and then to the first 3 members of the assignment, the remaining Sin_zero natural is 0. in_addr Structural BodyThe 3rd member of the sockaddr_in is a struct of the in_addr type, which contains only one member, as follows:
struct in_addr{in_addr_t s_addr;//32-bit IP address}; in_addr_t is defined in header file <netinet/in.h>, equivalent to unsigned long, with a length of 4 bytes. That is, S_addr is an integer, and the IP address is a string, so the inet_addr () function needs to be converted, for example:
unsigned long IP = inet_addr ("127.0.0.1"); printf ("%ld\n", IP); Run Result:
16777343

Graphic SOCKADDR_IN structure body
Why is it so complicated that nested structures in a struct are not sockaddr_in a member variable to indicate an IP address? The first parameter of the socket () function already indicates the type of address, why do you want to explain it again in the SOCKADDR_IN structure?

These tedious details do give beginners a certain obstacle, I think, this may be historical reasons, the following interface will always be compatible with the previous code. Readers must have patience, temporarily do not understand the relationship, according to the code in the tutorial "suit" can be, a long time will naturally accept. Why use sockaddr_in without using sockaddrThe second parameter of bind () is of type sockaddr, but the code uses SOCKADDR_IN and then casts it to sockaddr.

The SOCKADDR structure is defined as follows:
struct sockaddr{sa_family_t sin_family;//Address family (address family), that is, type char sa_data[14];//IP address and port number}; The following figure shows the contrast between sockaddr and sockaddr_in (the number in parentheses indicates the number of bytes occupied):


Sockaddr and sockaddr_in are of the same length, are 16 bytes, and simply combine the IP address and port number together, represented by a member Sa_data. To assign a value to a sa_data, you must specify both the IP address and the port number, such as "127.0.0.1:80", and unfortunately, there is no correlation function that converts the string to the desired form, and it is difficult to assign a value to a variable of type sockaddr, so use sockaddr_in instead. The two structures are of the same length, and no bytes are lost and no extra bytes are cast when casting the type.

It can be argued that sockaddr is a generic structure that can be used to hold multiple types of IP addresses and port numbers, while sockaddr_in is a structure that is designed to hold IPV4 addresses. There is also a sockaddr_in6 to save the IPV6 address, which is defined as follows:
struct SOCKADDR_IN6 {sa_family_t sin6_family;//(2) address type, value is Af_inet6 in_port_t sin6_port;//(2) 16-bit port number uint32_t sin6_ Flowinfo; (4) IPv6 flow information struct IN6_ADDR sin6_addr; (4) The specific IPV6 address uint32_t sin6_scope_id; (4) interface range ID}; It is because of the inconvenience of sockaddr use of common structure that different structures are defined for different address types. Connect () functionThe Connect () function is used to establish a connection, and its prototype is:
int connect (int sock, struct sockaddr *serv_addr, socklen_t addrlen);  Linux
int Connect (SOCKET sock, const struct SOCKADDR *serv_addr, int addrlen);  Windows
The description of each parameter is the same as bind () and is no longer redundant.

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.