Listen () function
功能用于面向连接服务器,表明愿意接收连接#include <sys/socket.h>函数原型intlisten(intsint backlog);参数说明sockfd:调用socket返回的文件描述符backlog:accept()应答之前,允许在进入队列中等待的连接数目,出错时返回-1返回值成功时,返回0失败时,返回-1说明在使用listen()之前,需要调用bind()绑定到需要的端口,否则系统内核将会监听一个随机端口
Accept () function
function to establish a socket connection to process a single connection request (such as Send/Receive data) # include <sys/socket.h> function prototype int accept (int sockfd, struct void*addr , Socklen _t *addrlen ); parameter description SOCKFD: Socket file descriptor for listening port addr: Pointer to local data structure sockaddr_in call connect () is stored in the structure Addrlen: When set to sizeof (struct sockaddr_in) The return value succeeds, returns a socket Port failed, return-1
int main() { int sockfd, client_fd; struct sockaddr_in remote_addr; /* 客户端地址信息 */ …… while(1) { sizeof(struct sockaddr_in); if ((client_fd = accept(sockfd, (struct sockaddr *) &remote_addr, &sin_size)) == -1){ perror("accept"continue; } printf("from %s\n", inet_ntoa(remote_addr.sin_addr)); …… }
Send () function
功能通过socket发送数据#include <sys/types.h>#include <sys/socket.h>函数原型ssize_t send(intconstvoidint flags);参数说明sockfd:发送数据的套接字描述符buf:指向发送数据的指针len:数据长度 flags:一般设置为0返回值成功时,返回实际发送的数据的字节数失败时,返回-1
Recv () function
functions receive data via socket #include < Sys/types.h> #include <sys/socket.h> function prototype ssize_t recv (int sockfd, void *buf, size_t len, int Flags"); parameter description SOCKFD: The socket descriptor to read buf: Buffer for the information to read Len: Maximum buffer length flags: generally set to 0 return value succeeds, returns the number of bytes actually received when the data fails, returning-1
MSG_DONTROUTEsend()的使用标志,不查找路由表,表示目的主机在本地网络MSG_OOB接收或者发送带外数据MSG_PEEKrecv()的使用标志,查看数据但不从系统缓冲区移走数据MSG_WAITALLrecv()的使用标志,等待所有数据,阻塞式接收,直到满足条件或发生错误读到指定字节时,正常返回,返回值等于len读到文件尾,正常返回,返回值小于len操作错误时,返回-1
Out-of-band data
The Transport Layer protocol uses out-of-band data (Out-of-band, OOB) to send some important data
If the communication party has important data to notify the other side, the protocol can quickly send this data to the other side
To send this data, the protocol generally does not use the same channel as normal data, but instead uses a different channel
The Linux socket mechanism supports low-level protocol send/Receive out-of-band data, but the TCP protocol has no real out-of-band data
TCP provides an emergency mode mechanism for sending important protocols
The TCP protocol sets the Urg bit in the data segment, indicating that it enters emergency mode
The receiver can take special care of emergency mode
Data is not easily blocked in this way and can be received in a timely manner by capturing Sigurg signals in the server-side program.
The TCP protocol can only send and receive one byte of out-of-band data at a time
Processing mechanism of TCP sending out-of-band data
TCP always treats the last byte as OOB data, and the rest as normal bytes
Regardless of how many bytes of OOB data are sent through the sendxxxx () function with the MSG_OOB flag, the sender only treats the last byte as OOB data
The receiving end can also receive only one byte of OOB data
Example: Send (SENDFD, "ABC", 3, Msg_oob)
TCP resets the emergency mode Urg
The emergency pointer locates the third byte ("C")
The first two bytes ("AB") are sent as normal bytes
TCP provides two processing modes
Non-oobinline mode: default mode for sockets
The OOB bytes are stored separately from the normal bytes, and the Oob bytes are stored in an OOB buffer
Oobinline mode:
Oob bytes and normal bytes are stored together
recvxxxx function in Msg_oob mode, will look for data in the OOB buffer
If the send side does not send the OOB bytes, it returns an error
If the send side sends an OOB byte
For non-oobinline mode, returns 1 bytes of OOB data
For Oobinline mode, an error is returned because the OOB byte is not placed in the OOB buffer
SendTo () function
功能用于数据报套接字的通信#include <sys/types.h>#include <sys/socket.h>函数原型int sendto(intconstvoidintunsignedintconststructint tolen); 参数说明to:目地机的IP地址和端口号信息tolen:常被赋值为sizeof (struct sockaddr) 返回值成功时,返回实际发送的数据的字节数失败时,返回-1
Recvfrom () function
功能用于数据报套接字的通信#include <sys/types.h>#include <sys/socket.h>函数原型int recvfrom(int sockfd,voidintunsignedintstructint *fromlen); 参数说明from:保存源机的IP地址及端口号fromlen:常常被赋值为sizeof (struct sockaddr) 返回值成功时,返回实际接收到的数据的字节数失败时,返回-1
Write () function
函数原型ssize_t write(intconstvoid *buf, size_t nbytes) 功能将buf中的nbytes字节内容写入文件描述符fd返回值成功时返回写的字节数失败时返回-1,并设置errno变量说明返回值大于0,表示写入部分或全部数据返回值小于0,表示出现错误如果错误为EINTR ,表示在写的时候出现中断错误如果为EPIPE,表示网络连接出现问题
Read () function
read(int*buf, size_t nbyte)功能从fd中读取内容返回值读成功时,返回实际所读的字节数如果返回的值是0 表示已经读到文件的结束,出错时,返回值小于0如果错误为EINTR,说明读是由中断引起的如果是ECONNREST表示网络连接出了问题
Close ()/shutdown () function
功能关闭通讯函数原型intclose(int sockfd);intshutdown(intint how);参数说明sockfd:要关闭的套接字描述符 how:0 :不允许接收1 :不允许发送2 :不允许发送和接收(和close() 一样)返回值成功时,返回0失败时,返回-1
Getpeername ()/gethostname () function
int getpeername(intstructint* addrlen);获取socket连接另一端的主机信息sockfd:是连接的数据流套接口文件描述符addr:保存另一端主机信息addrlen:设置为sizeof(struct sockaddr)int gethostname(char *hostname, size_t size);返回程序正在运行的计算机的名字返回值如果成功,返回0如果失败,返回-1
IP address and Domain name acquisition
function prototype #include <netdb.h> struct hostent *gethostbyname (const Span class= "Hljs-keyword" >char *name); struct hostent *gethostbyaddr (const char *addr, size_t len, int type);
struct hostent{ char *h_name; /*主机的正式名称*/ char **h_aliases; /*主机的别名*/ int h_addrtype; /*主机的地址类型 AF_INET */ int h_length; /*主机的地址长度 对于IP4 是4字节32位*/ char **h_addr_list; /*主机的IP地址列表*/ #define h_addr h_addr_list[0] /*主机的第一个IP地址 */};
Linux-common functions for programming