Is it always required to bind a socket?

前端 未结 2 854
我寻月下人不归
我寻月下人不归 2021-02-06 03:02

Well, my question may look like a basic stuff, but i am new to network programming side. I wish to know:

1) Is it always required to bind a socket in order to receive me

2条回答
  •  不要未来只要你来
    2021-02-06 03:59

    No, you don't need to bind().

    If you're using a TCP or UDP socket where you are planning to either connect() or send a packet to a destination with sendto(), the kernel will automatically bind the socket to a suitable port number when you try to connect or send. This is generally the preferred way. bind()ing client sockets is considered harmful.

    The same is also true of AF_UNIX sockets - the client side does not need to bind, and should not do so normally.

提交回复
热议问题