Sockaddr
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
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
Sockaddr_un
One way of inter-process communication is to use Unix sockets. In this way, people usually use a local socket instead of a network socket. In this way, hackers are not allowed to leave a backdoor.
Create
The socket function is used to create a socket, but the parameters passed are different from those of the network socket. The domain parameter should be pf_local or pf_unix, rather than pf_inet. The communication type of the local socket should be sock_stream or sock_dgram. The protocol is the default protocol. For example:
Int sockfd;
Sockfd = socket (pf_local, sock_stream, 0 );
Bind
After a socket is created, you must bind it to use it. Different from network socket binding, the local socket is bound to the struct sockaddr_un structure. The struct sockaddr_un structure has two parameters: sun_family and sun_path. Sun_family can only be af_local or af_unix, while sun_path is the path of the local file. Usually put the file in the/tmp directory. For example:
Struct sockaddr_un sun;
Sun. sun_family = af_local;
Strcpy (sun. sun_path, filepath );
BIND (sockfd, (struct sockaddr *) & Sun, sizeof (Sun ));
Listeners
The listening and accepting connection operations of local sockets are similar to those of network sockets.
Connection
Before connecting to a socket being monitored, you also need to fill in the struct sockaddr_un structure and then call the connect function.
After the connection is established successfully, we can send and accept the connection just like using a network socket. You can even set the connection to a non-blocking mode.