What client-side situations need bind()?

2020-03-01 05:39发布

I'm learning C socket programming. When would you use bind() on the client-side? What types of program will need it and why? Where can I find an example?

7条回答
祖国的老花朵
2楼-- · 2020-03-01 06:09

I suppose you should bind() in the case of UDP sockets.

查看更多
够拽才男人
3楼-- · 2020-03-01 06:11

bind function is one of "key" functions. It associates your socket (server or client) with address (ip + port). As for Windows you must use bind for WinSockets. There is good book about it "Network Programming for Microsoft Windows" by Anthony Jones and Jim Ohlund.

查看更多
SAY GOODBYE
4楼-- · 2020-03-01 06:14

Bind can be used to attach names to a sockets. Thus, say you create a software that uses a particular TCP port, you need to bind it and then, you will know the TCP port it is using.

查看更多
该账号已被封号
5楼-- · 2020-03-01 06:16

A good situation would be in a p2p case, you’re communicating with a STUN Server with a bound socket, and the STUN Server tells you the port on which he is receiving messages from your socket (that can be different from the one you specified when you bound your socket depending on your network and more specifically on your NAT type). This will allow you to be aware of the real port translation that your NAT is doing, and you’ll be able to give this information to potential peers that want to connect to you. Binding the socket is useful as some NATs are dynamically giving you ports (binding on port x twice might not give you the same “real” port). So you’ll be able to directly use the socket you bound to listen on the port.

查看更多
对你真心纯属浪费
6楼-- · 2020-03-01 06:19

A classic example of a client program using bind() is the (obsolete) rlogin / rsh family of network clients. These clients were intended to be used within networks with strong trust relationships - in some cases the server machine trusts the client machine to tell it the username of the user that is connecting. This required that the client program connect from a low port (a port less than 1024), because such ports are restricted to the root user and thus (in theory) prove that the client being used is authorised by the system administrator.

The NFS protocol has similar trust relationships, and similarly the client makes connections from a low port number, using bind().

Another example is IRC clients that allow the user to specify a particular source IP address to connect from. This is to accomodate users with many IP addresses assigned to their machine, each with a different "vanity" domain name assigned to it. Choosing which IP to connect from (with bind()) allows the user to choose which domain name to appear as on IRC.

查看更多
做自己的国王
7楼-- · 2020-03-01 06:26

On the client side, you would only use bind if you want to use a specific client-side port, which is rare. Usually on the client, you specify the IP address and port of the server machine, and the OS will pick which port you will use. Generally you don't care, but in some cases, there may be a firewall on the client that only allows outgoing connections on certain port. In that case, you will need to bind to a specific port before the connection attempt will work.

查看更多
登录 后发表回答