A summary of the programming functions of socket-related network in C language _c language

Source: Internet
Author: User

C language Socket () function: Establish a socket communication
header file:

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

To define a function:

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

Function Description: Socket () used to create a new socket, that is, register to the system, notify the system to establish a communication port. Parameter domain specifies what type of address to use, the complete definition is within/usr/include/bits/socket.h, and the following are common protocols:
Pf_unix/pf_local/af_unix/af_local UNIX Process Communication protocol
Pf_inet?af_inet Ipv4 Network protocol
PF_INET6/AF_INET6 Ipv6 Network protocol
PF_IPX/AF_IPX Ipx-novell Protocol
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 access to the original ATM PVCs
Pf_appletalk/af_appletalk APPLETALK (DDP) agreement
Pf_packet/af_packet Primary Envelope interface

The parameter type has the following values:
1, SOCK_STREAM provides two-way continuous and reliable data flow, that is, TCP. Supports the OOB mechanism and must use connect () to establish the connection status before all data transfer.
2, Sock_dgram use of discontinuous unreliable packet connection
3, Sock_seqpacket to provide continuous reliable packet connection
4, Sock_raw provide the original network protocol access
5, SOCK_RDM to provide reliable data packet connection
6, Sock_packet provides and network driver direct communication. Protocol is used to specify the transport protocol number used by the socket, which is usually not required for this reference, set to 0.

Return value: Success returns the socket processing code, failure returns-1.

Error code:
1, eprotonosupport parameter domain specified type does not support the parameter type or protocol specified protocol
2, Enfile core memory is not enough to establish a new socket structure
3, emfile process file table overflow, can no longer create a new socket
4, eaccess permission is insufficient, cannot establish the type or protocol specified protocol
5, Enobufs/enomem low memory
6, einval parameter Domain/type/protocol illegal

C-language Connect () function: Establish socket connection
header file:

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

To define a function:

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

Function Description: Connect () is used to connect the parameter SOCKFD socket to the parameter serv_addr the specified network address. Structure sockaddr refer to bind (). The parameter addrlen is the structure length of the sockaddr.

Return value: Success returns 0, failure returns 1, and the reason for the error is in errno.

Error code:
1. EBADF parameter SOCKFD non-legal socket processing code
2, efault parameter serv_addr pointer to inaccessible memory space
3, Enotsock parameter sockfd is a document descriptive word, not socket.
4, the Eisconn parameter SOCKFD socket is already the connection state
5, Etimedout attempt to connect the operation over the limited time has not yet responded.
6, Enetunreach cannot transfer packets to the specified host.
7, Eafnosupport sockaddr structure of the sa_family is not correct.
8, Ealready socket is not blocked and the previous wiring operation has not been completed.

C language Accept () function: Accept socket connection
header file:

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

To define a function:

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

Function Description: Accept () is used to accept the socket connection of the parameter S. The socket of the parameter s must be processed by the bind (), listen () function First, when the connection comes in, accept () will return a new socket processing code, then the data transmission and reading is through the new socket processing, and the original parameter s socket Can continue to use accept () to accept new wiring requirements. When the connection succeeds, the structure of the parameter addr is populated by the system into the address data of the remote host, and the parameter addrlen is the SCOKADDR structure length. Refer to bind () for the definition of the sockaddr of a mechanism.

Return value: Succeeds returns the new socket processing code, the failure returns-1, the reason for the error is stored in the errno.

Error code:
1, EBADF parameter s is not legitimate socket processing code.
2. The Efault parameter addr pointer points to an inaccessible memory space.
3, Enotsock parameter S is a document description word, not socket.
4, Eopnotsupp specified socket is not a sock_stream.
5. The eperm firewall rejects this connection.
6, ENOBUFS system buffer memory is insufficient.
7, Enomem core memory is not enough.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.