bind () and connect () - used to count network socket

Move from the c language Chinese network: http: //c.biancheng.net/cpp/html/3033.html

Socket () function to create a socket servers use bind () function socket is bound up with specific IP addresses and ports so that flow through the IP address and port data in order to deal with the socket : client establishes a connection with the connect ().

bind () function

1 int bind(int sock, struct sockaddr *addr, socklen_t addrlen); //Linux
2 int bind(SOCKET sock, const struct sockaddr *addr, int addrlen); //Windows 

socket file descriptors for the sock, addr sockaddr structure pointer variable, the size addrlen addr variable by the sizeof () calculated

1  // Create a socket 
2  int serv_sock = Socket (AF_INET, SOCK_STREAM, IPPROTO_TCP go);
 . 3  
. 4  // Create sockaddr_in structure variables 
. 5  struct sockaddr_in serv_addr;
 . 6 Memset (& serv_addr, 0 , the sizeof (serv_addr));   // every bytes are filled with zeros 
. 7 serv_addr.sin_family = AF_INET;   // use IPv4 addresses 
. 8 serv_addr.sin_addr.s_addr the inet_addr = ( " 127.0.0.1 " );   // specific IP address 
. 9 serv_addr.sin_port = the htons ( 1234 ) ;   // port
10  
. 11  // socket and IP, port binding 
12 is the bind (serv_sock, ( struct the sockaddr *) & serv_addr, the sizeof (serv_addr));

sockaddr_in structure

the sockaddr_in {struct 
    sa_family_t the sin_family; // address family (Address Family), i.e. address type 
    uint16_t sin_port; // 16-bit port number 
    struct in_addr sin_addr; // 32-bit IP address 
    char sin_zero [8]; // do not use, is generally filled with 0 
};

  

in_addr structure

struct in_addr { 
    in_addr_t s_addr;   // the IP address of 32 bits 
};

in_addr_t defined in the header <netinet / in.h> are equivalent to unsigned long, a length of 4 bytes. That is, s_addr is an integer, and the IP address is a string, it is necessary the inet_addr () function to convert, for example:

unsigned long ip = inet_addr("127.0.0.1");
printf("%ld\n", ip);

 

Why not use sockaddr sockaddr_in

struct the sockaddr { 
    sa_family_t the sin_family;    // address family (Address Family), i.e. the address type 
    char          sa_data [ 14 ];   // the IP address and port number 
};

Sockaddr sockaddr_in same and the length is 16 bytes, except that the IP address and port number are merged together, it is represented by a member sa_data. To give sa_data assignment, you must specify the IP address and port number, such as "127.0.0.1:80", unfortunately, no form of this function will need to convert a string, it is difficult to give a variable of type sockaddr assignment, so use sockaddr_in instead. These two structures are the same length, the force will not be lost when converting byte type, no extra byte.

It can be considered, the sockaddr is a universal structure, can be used to store various types of IP address and port number, and is a structure designed to sockaddr_in stored IPv4 addresses.

 

Guess you like

Origin www.cnblogs.com/Theo-sblogs/p/11130205.html