TCP socket functions

1. socket function

#include <sys/socket.h>
int socket(int family, int type, int protocol);
//若成功则返回非负描述符
//若出错返回-1

2.connect function

#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr* servaddr, socklen_t addrlen);
//若成功则返回0,出错返回-1
  • The second parameter is a pointer to the address structure of the socket.

  • Former clients and then call the function do not have to call bind connect function.

  • The excitation function call to connect TCP three-way handshake, but only inWhen the connection is established successfully or errorBefore returning.

    出错情况:
    (1)TCP客户端没有收到SYN分节的响应,返回ETIMEDOUT
    (2)若对SYN的响应为RST,表明服务器在指定端口没有进程在等待连接。
    (3)SYN在中间某个路由器引发“目的地不可达”的ICMP错误。
    
  • If the connect fails then the socket is no longer available, it must be closed.

3.bind function

#include <sys/socket.h>
int bind(int sockfd, const struct sockaddr *myaddr, aocklen_t addrlen);
//若成功则为0,出错返回-1
  • Select a temporary port if the specified port number 0, the kernel is called bind.
  • If the specified IP address wildcard address, the kernel will have to wait until the socket connection (TCP) when selecting an IP address.
  • For IPv4, the wildcard address constant INADDR_ANY specified, its value is typically 0.
struct sockaddr_in servaddr;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  • If the kernel chooses a temporary port number for the socket, bind does not return the selected value. In order to obtain a temporary port values ​​of the selected kernel must call getsockname () function.

4.listen function

#include <sys/socket.h>
int listen(int sockfd, int backlog);
//若成功则为0,出错返回-1
  • The second parameter specifies the maximum number of cores to be connected to the corresponding socket queue.

    内核为任何一个给定的监听套接字维护两个队列
    (1)未完成连接队列,已由客户发出SYN并到达服务器,而服务器正等待完成相应的三路握手。
    (2)已完成连接队列,每个已完成三路握手对应其中一项。
    (3)两队列之和不超过backlog。
    

Here Insert Picture Description

  • When a process calls accept, has completed the connection head item in the queue will be returned to the process.
  • Specify a value capable of supporting larger than the core of the backlog is also acceptable, the kernel will be truncated accordingly.
  • When a client SYN arrives, if the queue is full, TCP ignores this section, does not send RST.

5.accept function

#include <sys/socket.h>
int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);
  • The first function is a socket listening socket, the socket connector whose return value is.

6.close function

#include <sys/socket.h>
int close(int sockfd);
//成功则返回0,出错返回-1
  • This function is called FIN section will occur, followed by a normal TCP connection termination sequence.

  • TCP will try to send any queued data waiting to be sent to the terminal, after completion of the transmission is normal TCP connection termination sequence.

    父进程关闭已连接套接字只是导致相应描述符的引用计数值减1,若我们确实想在某个TCP连接上发送一个FIN,可以调用shutdown函数。
    

7.getsockname()和getpeername()

#include <sys/socket.h>
int getsockname(int sockfd, struct sockaddr *localaddr, socklen_t *addrlen);
int getpeername(int sockfd, struct sockaddr *peeraddr, socklen_t *addrlen);
  • In a no call to bind the TCP client,connect after a successful return, Getsockname returned by the kernel for imparting local IP address and local port number of the connection.

  • After the call to bind to port number 0, getsockname return to the local port number given by the kernel.

  • In order to pass a server with the IP address of the call bind,Connected to a customer once established (accept return), Getsockname can be used to return the core to impart the local IP address of the connection.

Released two original articles · won praise 0 · Views 18

Guess you like

Origin blog.csdn.net/m0_46390964/article/details/104510246