socket connect() vs bind()

后端 未结 6 786
臣服心动
臣服心动 2020-12-02 03:35

Both connect() and bind() system calls \'associate\' the socket file descriptor to an address (typically an ip/port combination). Their prototypes

6条回答
  •  伪装坚强ぢ
    2020-12-02 04:18

    The one liner : bind() to own address, connect() to remote address.

    Quoting from the man page of bind()

    bind() assigns the address specified by addr to the socket referred to by the file descriptor sockfd. addrlen specifies the size, in bytes, of the address structure pointed to by addr. Traditionally, this operation is called "assigning a name to a socket".

    and, from the same for connect()

    The connect() system call connects the socket referred to by the file descriptor sockfd to the address specified by addr.

    To clarify,

    • bind() associates the socket with its local address [that's why server side binds, so that clients can use that address to connect to server.]
    • connect() is used to connect to a remote [server] address, that's why is client side, connect [read as: connect to server] is used.

提交回复
热议问题