Linux network programming|socket programming

socket programming

1. Function description

Socket programming has the following basic functions:

  • socket(): used to create a socket, while specifying the protocol and type
  • bind(): Bind the address information stored in the corresponding address structure with the socket. Mainly used on the server side, the socket created by the client can be unbound
  • listen(): After the socket is established on the server side and the address is bound, the socket is set to listening mode (passive mode), ready to receive client connection requests
  • accept(): Wait and receive the client's connection request. After the TCP connection is established, the function will return a new connected socket
  • connect(): The client sends a connection request to the listening socket of the server through this function
  • send() and recv(): usually used for sending and receiving data in TCP communication, and can also be used in UDP
  • sendto() and recvfrom(): usually used for sending and receiving data in UDP communication

2. Function prototype

socket() function

/*****socket()*****/
函数原型:int socket(int family, int type, int protocol)
传 入 值:family 协议族
         -->AF_INET:IPv4协议
         -->AF_INET6:IPv6协议
         -->AF_LOCAL:UNIX域协议
         -->AF_ROUTE:路由套接字
         -->AF_KEY:密钥套接字
		 type 套接字类型
		 -->SOCK_STREAM 流式套接字
		 -->SOCK_DGRAM 数据报套接字
		 -->SOCK_RAW 原始套接字
		 protocol 0(原始套接字除外)
返 回 值:成功返回非负套接字描述符;失败返回-1

bind() function

/*****bind()*****/
函数原型:int bind(int sockfd, struct sockaddr *my_addr, int addrlen)
传 入 值:sockfd 套接字描述符
		 my_addr 绑定的地址
		 addrlen 地址长度
返 回 值:成功返回0;失败返回-1

listen() function

/*****listen()*****/
函数原型:int listen(int sockfd, int backlog)
传 入 值:sockfd 套接字描述符
		 backlog 请求队列中允许的最大请求数,大多数系统默认值为5
返 回 值:成功返回0;失败返回-1

accept() function

/*****accept()*****/
函数原型:int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
传 入 值:sockfd 套接字描述符
		 addr 用于保存客户端地址
		 addrlen 地址长度
返 回 值:成功返回建立好连接的套接字描述符;失败返回-1

connect() function

/*****connect()*****/
函数原型:int connect(int sockfd, struct sockaddr *serv_addr, int addrlen)
传 入 值:sockfd 套接字描述符
		 serv_addr 服务器端地址
		 addrlen 地址长度
返 回 值:成功返回0;失败返回-1

send() and recv() functions

/*****send()*****/
函数原型:int send(int sockfd, const void *buf, int len, int flags)
传 入 值:sockfd 套接字描述符
		 buf 发送缓冲区的地址
		 len 发送数据的长度
		 flags 一般为0
返 回 值:成功返回实际发送的字节数;失败返回-1

/*****recv()*****/
函数原型:int recv(int sockfd, const void *buf, int len, unsigned int flags)
传 入 值:sockfd 套接字描述符
		 buf 存放接收数据的缓冲区
		 len 接收数据的长度
		 flags 一般为0
返 回 值:成功返回实际接收到的字节数;失败返回-1

sendto() and recvfrom() functions

/*****sendto()*****/
函数原型:int sendto(int sockfd, const void *buf, int len, unsigned int flags, const struct sockaddr *to, int tolen)
传 入 值:sockfd 套接字描述符
		 buf 发送缓冲区的地址
		 len 发送数据的长度
		 flags 一般为0
		 to 接收方套接字的IP地址和端口号
		 tolen 地址长度
返 回 值:成功返回实际发送的字节数;失败返回-1

/*****recvfrom()*****/
函数原型:int recvfrom(int sockfd, const void *buf, int len, unsigned int flags, const struct sockaddr *from, int fromlen)
传 入 值:sockfd 套接字描述符
		 buf 存放接收数据的缓冲区
		 len 接收数据的长度
		 flags 一般为0
		 from 发送方的IP地址和端口号信息
		 fromlen 地址长度
返 回 值:成功返回实际接收到的字节数;失败返回-1

Guess you like

Origin blog.csdn.net/Chuangke_Andy/article/details/108422965