Unix sockets: when to use bind() function?

前端 未结 5 448
暖寄归人
暖寄归人 2021-02-04 12:21

I\'ve not a clear idea about when I have to use the bind() function. I guess it should be used whenever I need to receive data (i.e. recv() or recvfrom() functions) whether I\'m

5条回答
  •  梦谈多话
    2021-02-04 12:28

    bind() is useful when you are writing a server which awaits data from clients by "listening" to a known port. With bind() you are able to set the port on which you will listen() with the same socket.

    If you are writing the client, it is not needed for you to call bind() -- you can simply call recv() to obtain the data sent from the server. Your local port will be set to an "ephemeral" value when the TCP connection is established.

提交回复
热议问题