开发者

In which cases is calling bind() necessary? [duplicate]

This question already has answers here: Closed 10 years ago.

Possible Duplica开发者_高级运维te:

What client-side situations need bind()?

I saw the server program binding to a port. Why is this not necessary for a client program? After all client program will also be sending and receiving data.

Anyway, in which cases is calling bind() necessary? And W-H-Y?


Why is this not necessary for a client program?

Because it happens automatically when you connect the socket. Usually you don't care about which local outbound port you are using in client software, in fact the only times you do care are when your netadmin has mistakenly implemented an outbound port numbering rule, which just causes problems and doesn't solve anything.


  1. You need to perform binding regardless of socket family for server sockets.

  2. For client sockets it depends. For AF_INET type of sockets underlying stack (TCP) performs binding for client sockets. However for AF_UNIX type of sockets client needs to perform binding with code. So for clients sockets it depends on socket family type.


For TCP server socket you must specify the IP and port number: this enables the client to know the address of the host(via IP) and the process of the server(via port number),

after the server socket accepts a new connection from a client it creates a new socket dedicate for that client, and the server differentiate between the clients by these new sockets, so you can create a client/server application without binding the client socket to a port number

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜