Linux socket (1)
Socket features are determined by three attributes: domain, type, and protocol.
1. Domain
AF_UNIX UNIX domain protocol (File System socket)
AF_INET ARPA Internet Protocol (UNIX network socket)
... Omitted
2. Type
1) stream socket: SOCK_STREAM
2) datagram socket: SOCK_DGRAM
3. Protocol
Generally, this parameter is determined by the socket type and socket domain. You do not need to select this parameter. setting this parameter to 0 indicates that the default protocol is used.
Socket address.
1) AF_UNIX
Struct sockaddr_un {
Sa_family_t sun_family;/* AF_UNIX */
Char sun_path [];/* pathname */
};
2) AF_INET
Struct sockaddr_in {
Short int sin_family;/* AF_INET */
Unsigned short int sin_port;/* port number */
Struct in_addr sin_addr;/* Internet address */
}; This structure is defined in the header file netinet/in. h.
The IP address structure in_addr is defined:
Struct in_addr {
Unsigned long int s_addr;
};
1. Named socket
# Include <sys/socket. h>
Int bind (int socket, const struct sockaddr * address, size_t address_len );
2. Create a socket queue
# Include <sys/socket. h>
Int listen (int socket, int backlog );
3. Accept connections
# Include <sys/socket. h>
Int accept (int socket, struct sockaddr * address, size_t * address_len );
4. Request connection
# Include <sys/socket. h>
Int connect (int socket, const struct sockaddr * address, size_t address_len );
5. Disable socket
Close ();