Linux programming-memoir 7: A memoir of linux Programming
=== Network communication ====
# Include <sys/socket. h>
Int socket (int domain, int type, int protocol );
Create a socket. The domain value is as follows:
AF_UNIX unix domain socket, used locally
AF_INET IPV4 socket
AF_INET6 IPV6 socket
There are two types: SOCK_STREAM (TCP stream) and SOCK_DGRAM (UDP stream)
Protocol is basically 0, and very few will set it
0 is returned, and-1 fails.
# Include <sys/socket. h>
Int bind (int sockfd, const struct sockaddr * addr, socklen_t addrlen );
Bind the socket to the addr address. 0 is returned and-1 fails.
# Include <sys/socket. h>
Int listen (int sockfd, int backlog );
Listening for socket link. 0,-1 failed to be returned.
# Include <sys/socket. h>
Int accept (int sockfd, struct sockaddr * addr, socklen_t * addrlen );
Accept the customer's link and return a file descriptor. If an error occurs, the value-1 is returned.
# Include <sys/socket. h>
Int connect (int sockfd, const struct sockaddr * addr, socklen_t addrlen );
The link to the addr address is established. 0 is returned for success, and-1 is returned for failure.
# Include <sys/socket. h>
Ssize_t recvfrom (int sockfd, void * buffer, size_t length, int flags,
Struct sockaddr * src_addr, socklen_t * addrlen );
Accept UDP data. If successful, the number of accepted bytes is returned. If 0 is returned, the acceptance is complete, and an error occurs in-1.
Ssize_t sendto (int sockfd, const void * buffer, size_t length,
Int flags, const struct sockaddr * dest_addr, socklen_t addrlen );
UDP data is sent. The number of actually sent bytes is returned successfully, and an error in-1 is returned.
UNIX socket address Structure
Struct sockaddr_un {
Sa_family_t sun_family;/* always equal to AF_UNIX */
Char sun_path [108];/* domain path name, ending with '\ 0 */
};
# Include <sys/socket. h>
Int socketpair (int domain, int type, int protocol, int sockfd [2]);
Used Between processes to create a unix domain socket. Success returns 0,-1 failed
# Include <arpa/inet. h>
Uint16_t htons (uint16_t host_uint16 );
Uint16 host byte order to network byte order
Uint32_t htonl (uint32_t host_uint32 );
Uint32 host byte order to network byte order
Uint16_t ntohs (uint16_t net_uint16 );
Uint16 network byte order conversion bit host byte order
Uint32_t ntohl (uint32_t net_uint32 );
Uint32 network-byte sequence Conversion
Struct in_addr {
In_addr_t s_addr/* unsigned 32-bit integer */
};
Struct sockaddr_in {
Sa_family_t sin_family;/* always marked as AF_INET */
In_port_t sin_port/* port */
Struct in_addr sin_addr;/* IP Address */
Unsinged char _ pad [X];/* used only for structure alignment */
};
Sin_port, sin_addr is the network byte order
Struct in6_addr {
Uint8_t s6_addr [16];/* IP6 address */
};
Struct sockaddr_in6 {
Sa_family_t sin6_family;/* always marked as AF_INET6 */
In_port_t sin6_port;/* port */
Uint32_t sin6_flowinfo;/* IPV6 flow info */
Struct in6_addr sin6_addr;/* IPV6 address */
Uint32_t sin6_scope_id;/* added (kernel2.4 or later )*/
};
All structures are in network byte order
# Include <arpa/inet. h>
Int inet_ton (int domain, const char * src_str, void * addrptr );
The IP address in string format is converted to the corresponding struct, and 0 is returned successfully.-1 error
Const char * inet_ntop (int domain, const void * addrptr,
Char * dst_str, size_t len );
The converted address struct is in the corresponding string format, and 0 is returned successfully.-1 error
# Include <sys/socket. h>
# Include <netdb. h>
Int getaddrinfo (const char * host, const char * service,
Const struct addrinfo * hints, struct addrinfo ** result );
Returns the IP address of the hostname or service name. The value 0 indicates success, and the value-1 indicates failure.
# Include <sys/socket. h>
# Include <netdb. h>
Void freeaddrinfo (struct addrinfo * result );
Release the above function to obtain IP address information and allocate memory
# Include <sys/socket. h>
Int shutdown (int sockfd, int how );
To disable a socket, follow these steps:
1. SHUT_RD close reading
2. SHUT_WR close write
If the return value is 0, the operation is successful. If the return value is-1, an error occurs.
# Include <sys/socket. h>
Int getsockname (int sockfd, struct sockaddr * addr, socklen_t * addrlen );
Int getpeername (int sockfd, struct sockaddr * addr, socklen_t * addrlen );
Returns the local/peer socket address information, returns 0 success,-1 Failure
# Include <sys/sendfile. h>
Ssize_t sendfile (int out_fd, int in_fd, off_t * offset, size_t count );
Transmits the content of a file through a socket, returns the number of transmitted bytes, and returns-1 error
Include <sys/socket. h>
Int getsockopt (int sockfd, int level, int optname,
Void * optval, socklen_t * optlen );
Int setsockopt (int sockfd, int level, int optname,
Const void * optval, socklen_t optlen );
Gets/sets the socket property, returns 0 success,-1 failed