UNIX Network programming Socket API

Source: Internet
Author: User

int socket (int family, int type, int protocol), this interface creates a socketFamily specifies the protocol family (usually af_inet), and the type parameter specifies the types of sockets: Sock_stream, SOCK_DGRAM, and Sock_raw;protocol are usually assigned the value "0".int bind (int sockfd, struct sockaddr *my_addr, int addrlen); Bind socket to IP address and portstruct SOCKADDR_IN
{
Short sin_family;
U_short Sin_port;
struct IN_ADDR sin_addr;
Char Sin_zero[8];
};an instance of a binding:struct sockaddr_in saddr;
memset (void *) &saddr,0,sizeof (SADDR));
saddr.sin_family = af_inet;
Saddr.sin_port = htons (8888); port number greater than 1024
SADDR.SIN_ADDR.S_ADDR = htonl (Inaddr_any); Bind to local IP
SADDR.SIN_ADDR.S_ADDR = inet_addr ("192.168.22.5"); Binding fixed IP
Bind (Listensocket, (struct sockaddr *) &saddr,sizeof (SADDR));int Listen (int sockfd, int backlog);SOCKFD is the server-side socket descriptor returned by the socket system call, and the backlog specifies the maximum number of requests allowed in the request queueint Accept (int sockfd, struct sockaddr *addr, int *addrlen);SOCKFD is a server socket descriptor that is being monitored, and addr is usually a pointer to a sockaddr_in variable that holds the client address that made the connection request the Connect () function is used to establish a TCP connection to the remote server with its function prototype:
int Connect (int sockfd, struct sockaddr *serv_addr, int addrlen); int Send (int sockfd, const void *msg, int len, int flags); int recv (int sockfd,void *buf,int len,unsigned int flags);int sendto (int sockfd, const void *msg,int len,unsigned int flags,const struct sockaddr *to,int tolen);Used for data transfer on non-connected sockets (Sock_dgram/sock_raw) int recvfrom (int sockfd,void *buf,int len,unsigned int flags,struct sockaddr *from,int *fromlen);Close (SOCKFD);Close () is an operation on a socket that is closed after the process cannot access the socket.IP, DNS, and other related functionsin_addr_t inet_addr (const char * strptr); Converts the string IP address to the IPV4 address structure in_addr value char * INET_NTOA (struct in_addr * addrptr); Converts the IPV4 address structure in_addr value to a string IP struct hostent *gethostbyname (const char *name); Conversion of domain names and IP addresses:

UNIX Network programming Socket API

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.