What client-side situations need bind()?

17,817

Solution 1

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.

Solution 2

An example would be the data connection of an active FTP connection. In this case, the server connects from its port 20 to the IP and port specified by a PORT or EPRT command.

Solution 3

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.

Solution 4

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.

Share:
17,817

Related videos on Youtube

Author by

friends

Updated on May 08, 2022

Comments

  • friends 11 days

    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?

  • Hasturkun
    Hasturkun over 11 years
    I suggest you s/socket number/port/ in your answer
  • ninjalj
    ninjalj over 11 years
    @Alexandre Jasmin: no, read it again, in active FTP the FTP server acts as a client for the data channel, binding to port 20 and connecting to a port on the FTP client.
  • Hasturkun
    Hasturkun over 11 years
    You do not have to bind a socket to use connect
  • Alex Jasmin
    Alex Jasmin over 11 years
    There are a few protocols in the Unix world that expect clients to connect from a privilege port. This is supposed to ensure the connection comes from a privileged process on the client machine. This is one of the few real use case for bind() on a cient socket though your system may have a dedicated rresvport() function for this.
  • Hasturkun
    Hasturkun over 11 years
    You don't have to bind for send and recv to work either, you just need a connected socket.
  • Edward83
    Edward83 over 11 years
    ok) last time i worked with sockets maybe 7 years ago, lol, now i checked the book and i see that you right;) sorry guys;)
  • wvxvw
    wvxvw over 5 years
    To give more concrete example of what @AlexandreJasmin is talking about: NFS (network file-system) server which comes bundled with Linux kernel requires that the client use privileged port to communicate (though it's configurable on the server side, but the default is to require only low-numbered ports).