Tim
Tim

Reputation: 99428

address questions in Socket programming

From an introduction to Berkeley Sockets API in Tanenbaum's Computer Networks,

  1. On the server side:

    Newly created sockets by SOCKET primitive do not have network addresses. These are assigned using the BIND primitive. Once a server has bound an address to a socket, remote clients can connect to it.

    The reason for not having the SOCKET call create an address directly is that some processes care about their addresses (e.g., they have been using the same address for years and everyone knows this address), whereas others do not.

    Why is not having the SOCKET call create an address directly, because some processes care about their addresses, whereas others don't?

  2. On the client side:

    Now let us look at the client side. Here, too, a socket must first be created using the SOCKET primitive, but BIND is not required since the address used does not matter to the server.

    why is BIND not required because the address used does not matter to the server?

Upvotes: 0

Views: 221

Answers (2)

Joel C
Joel C

Reputation: 3158

Why is not having the SOCKET call create an address directly, because some processes care about their addresses, whereas others don't?

Basically, because a strict client (eg. web browser) does not care what its local IP address/port is. By not requiring the SOCKET call to create/allocate an address, it can allow for the BIND call to actually never happen in the client context (where it often doesn't matter) but happen in the server context (where it greatly matters).

why is BIND not required because the address used does not matter to the server?

This is related to the first part of the answer above. A socket only needs to be bound to a specific address and port in order for something to reach out and connect to it. In many cases (such as a web browser), there is no reason for anything to be able to connect to it - it only needs to be able to connect to other systems (servers).

To make it a general principle, you only need to BIND a socket to an address when something else will need to connect up to it. You do not need to BIND a socket if it will only be connecting to something else.

Upvotes: 1

user207421
user207421

Reputation: 310956

Why is not having the SOCKET call create an address directly, because some processes care about their addresses, whereas others don't?

Because that's the way they designed it. Listening sockets don't need a target address, and in most cases they don't need a source address either (INADDR_ANY); and most client sockets don't need a specific source address either. Clients never need to call bind() at all in most cases. So putting the address into the socket creation API would be pointless.

The reason for not having the SOCKET call create an address directly is that some processes care about their addresses (e.g., they have been using the same address for years and everyone knows this address), whereas others do not.

I don't agree with this strange statement.

why is BIND not required because the address used does not matter to the server?

It's another strangely expressed statement. The source address of a connected socket can be determined automatically via the static IP routing tables. As long as the client can connect to the server at all, symmetry guarantess that the server can send back to the client on the same connection, so the actual source-address of the client doesn't matter specifically to either the cleint or the server application. It matters to TCP of course, otherwise it wouldn't exist.

Upvotes: 1

Related Questions