Struct sockaddr {
Unsigned short sa_family;/* address family, af_xxx */
Char sa_data [14];/* 14 bytes of Protocol address */
};
Sa_family is the address family, generally in the form of "af_xxx. It seems that af_inet is commonly used.
Sa_data is a 14-byte Protocol address.
This data structure uses parameters for functions such as bind, connect, recvfrom, and sendto to specify the address information.
However, in general programming, we do not directly operate on this data structure, but use another data structure equivalent to sockaddr.
Sockaddr_in (defined in netinet/in. h ):
Struct sockaddr_in {
Short int sin_family;/* address family */
Unsigned short int sin_port;/* Port Number */
Struct in_addr sin_addr;/* Internet address */
Unsigned char sin_zero [8];/* same size as struct sockaddr */
};
Struct in_addr {
Unsigned long s_addr;
};
Typedef struct in_addr {
Union {
Struct {
Unsigned char s_b1,
S_b2,
S_b3,
S_b4;
} S_un_ B;
Struct {
Unsigned short s_w1,
S_w2;
} S_un_w;
Unsigned long s_addr;
} S_un;
} In_addr;
Sin_family refers to the protocol family. It can only be af_inet in socket programming.
Sin_port storage port number (in bytes)
Sin_addr storage IP address, using the in_addr Data Structure
Sin_zero is an empty byte reserved to keep sockaddr and sockaddr_in data structures of the same size.
S_addr stores IP addresses in byte sequence.
Sockaddr_in and sockaddr are parallel structures. The pointer to the addr_in struct can also point
Add struct and replace it. That is to say, you can use sockaddr_in to create the information you need,
Bzero (char *) & mysock, sizeof (mysock) can be used for type conversion at the end; // Initialization
Mysock struct name
Mysock. sa_family = af_inet;
Mysock. sin_addr.s_addr = inet_addr ("192.168.0.1 ");
......
To use the following code for conversion:
(Struct sockaddr *) mysock
The meaning of sa_data is determined by sa_family.
If sa_family = af_inet
Then sa_data is the sin_addr and sin_port of sockaddr_in.
In other words, sockaddr can be viewed as sockaddr_in.
Sockfd is the socket descriptor returned by the socket function. my_addr is a sockaddr type that points to information including the local IP address and port number.
Needle; addrlen is often set to sizeof (struct sockaddr ).
The structsockaddr structure type is used to save socket information:
Struct sockaddr {
Unsigned short sa_family;/* address family, af_xxx */
Char sa_data [14];/* 14-byte Protocol address */
};
Sa_family is generally af_inet, representing the Internet (TCP/IP) address family;
Sa_data contains the IP address and port number of the socket.
There is also a structure type:
Struct sockaddr_in {
Short int sin_family;/* address family */
Unsigned short int sin_port;/* Port Number */
Struct in_addr sin_addr;/* IP Address */
Unsigned char sin_zero [8];/* fill 0 to keep the same size as struct sockaddr */
}; This structure is more convenient to use. Sin_zero is used to fill the sockaddr_in structure with the struct
The same length of sockaddr can be set to zero using the bzero () or memset () function. The pointer pointing to sockaddr_in and the pointer pointing to sockaddr can be converted to each other, which means that if the parameter type required for a function is sockaddr, you can call a function to point
The addr_in pointer is converted to the sockaddr pointer; or vice versa.
Sockaddr_in and sockaddr are parallel structures. the pointer to the addr_in struct can also point to the add struct and replace it. That is to say, you can use sockaddr_in to create the information you need and use it for type conversion. (Sorry, this is because I have read a lot of programs about socket. They generally use this method.) Suppose your structure name is mysock,
Bzero (char *) & mysock, sizeof (mysock); // Initialization
Mysock. sa_family = af_inet;
Mysock. sin_addr.s_addr = inet_addr ("192.168.0.1 ");
......
Wait until the conversion is done ...... (Struct sockaddr *) mysock ...... That's all.
Struct sockaddr {
Unsigned short sa_family;/* address family, af_xxx */
Char sa_data [14];/* 14-byte Protocol address */
};
The above is a general socket address, specific to the internet socket, with the following structure, the two can be type conversion
Struct sockaddr_in {
Short int sin_family;/* address family */
Unsigned short int sin_port;/* Port Number */
Struct in_addr sin_addr;/* Internet address */
Unsigned char sin_zero [8];/* the same length as struct sockaddr */
};
Struct in_addr is a 32-bit IP address.
Struct in_addr {
Unsigned long s_addr;
};
Yes
Struct in_addr {
Union {
Struct {u_char s_b1, s_b2, s_b3, s_b4;} s_un_ B;
Struct {u_short s_w1, s_w2;} s_un_w;
U_long s_addr;
} S_un;
};
Use u_long htonl (u_long hostlong); to convert the host's byte sequence to the TCP/IP network's byte sequence.
Use u_short htons (u_short hostshort) to convert host byte to TCP/IP network byte.
Inet_addr () is to convert a point-based IP address (such as 192.168.0.1) to the 32-bit IP address (0xc0a80001) required in the preceding structure ).
The common usage is:
Int sockfd;
Struct sockaddr_in my_addr;
Sockfd = socket (af_inet, sock_stream, 0);/* do some error checks! */
My_addr.sin_family = af_inet;/* Host byte order */
My_addr.sin_port = htons (myport);/* short, network byte order */
My_addr.sin_addr.s_addr = inet_addr ("192.168.0.1 ");
Bzero (& (my_addr.sin_zero), 8);/* zero the rest of the struct */
/* Do not forget to perform an error check for BIND :*/
BIND (sockfd, (struct sockaddr *) & my_addr, sizeof (struct sockaddr ));
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/yy_msdn/archive/2007/07/15/1691988.aspx