C language socket related network programming functions summary

  • 2020-04-02 03:21:09
  • OfStack

C language socket() function: establish a socket communication
The header file:


 #include <sys/types.h>  #include <sys/socket.h>

Definition function:


int socket(int domain, int type, int protocol);

Function description: the socket () is used to create a new socket, which is registered to the system, the notification system to establish a communication port. What types of addresses, domain specifies the use complete definition in the/usr/include/bits/socket, h, is a common agreement under:
    PF_UNIX/PF_LOCAL/AF_UNIX/AF_LOCAL UNIX process communication protocol
    PF_INET & # 63; AF_INET Ipv4 network protocol
    PF_INET6/AF_INET6 Ipv6 network protocol
    PF_IPX/AF_IPX IPX - Novell agreement
    PF_NETLINK/AF_NETLINK core user interface device
    PF_X25/AF_X25 itu-t x.25 / iso-8208 protocol
    PF_AX25/AF_AX25 amateur wireless ax.25 protocol
    PF_ATMPVC/AF_ATMPVC accesses the original ATM PVCs
    PF_APPLETALK/AF_APPLETALK appletalk (DDP) protocol
    PF_PACKET/AF_PACKET primary packet interface

The parameter type has the following values:
1. SOCK_STREAM provides two-way continuous and reliable data flow, i.e., TCP. It supports the OOB mechanism.
2. SOCK_DGRAM USES discontinuous and unreliable packet connections
3. SOCK_SEQPACKET provides continuous and reliable packet connection
4. SOCK_RAW provides access to the original network protocol
5. SOCK_RDM provides reliable packet connections
6. SOCK_PACKET provides direct communication with the network driver. Protocol is used to specify the transport protocol number used by the socket.

Return value: returns the socket handling code on success and -1 on failure.

Error code:
1. The type specified by EPROTONOSUPPORT parameter domain does not support the protocol specified by parameter type or protocol
2. ENFILE core memory is insufficient to establish a new socket structure
3, EMFILE process file table overflow, unable to establish a new socket
4. Due to insufficient EACCESS, the protocol specified by type or protocol cannot be established
5. ENOBUFS/ENOMEM is out of memory
6. The EINVAL parameter domain/type/protocol is illegal

C language connect() function: establish socket connection
The header file:


#include <sys/types.h>  #include <sys/socket.h>

Definition function:


int connect(int sockfd, struct sockaddr * serv_addr, int addrlen);

Connect () is used to connect the socket of parameter sockfd to the network address specified by parameter serv_addr.

Return value: 0 on success, -1 on failure, error cause in errno.

Error code:
1. The EBADF parameter sockfd is illegal socket processing code
2. The EFAULT parameter serv_addr points to an inaccessible memory space
3. The ENOTSOCK parameter sockfd is a file descriptor, not a socket.
4. Socket of EISCONN parameter sockfd has been wired
5,     ETIMEDOUT failed to respond to an attempt to wire over the time limit.
6. ENETUNREACH cannot transmit data packets to the specified host.
7. The sa_family of EAFNOSUPPORT sockaddr structure is not correct.
8. The EALREADY socket is uninterruptible and the previous connection operation has not been completed.

C language accept() function: accept socket connection
The header file:


#include <sys/types.h>  #include <sys/socket.h>

Definition function:


int accept(int s, struct sockaddr * addr, int * addrlen);

Function description: The accept () is used to accept the s parameter of the socket connection. The s parameter should be the bind socket (), listen () function is processed, when there is attachment in the accept () will return a new socket processing code, data transmission and read back is processed by the new socket, and original parameter s socket can continue to use the accept () to accept the new attachment requirements. Attachment successfully, The structure referred to by the parameter addr will be filled into the address data of the remote host by the system, and the parameter addrlen is the structure length of scokaddr. Please refer to bind() for the definition of sockaddr.

Return value: new socket handling code is returned on success, -1 is returned on failure, and the reason for the error is in errno.

Error code:
1, EBADF parameter s illegal socket processing code.
2. EFAULT parameter addr points to an inaccessible memory space.
3. ENOTSOCK parameter s is a file descriptor, not a socket.
4. The socket specified by EOPNOTSUPP is not SOCK_STREAM.
5. EPERM firewall rejects this connection.
6. The buffer memory of ENOBUFS system is insufficient.
7. ENOMEM is out of core memory.


Related articles: