Sam YC
Sam YC

Reputation: 11617

Java the difference of Socket and ServerSocket in using port

At the server side, we use

Socket server = serverSocket.accept();

to create a socket. After the socket is created, we can create a new thread to handle the input/output stream of that socket. So we can go back to listening at the same port and create new socket if there are further connection requests come in. Since we already created ServerSocket at a specific port, of course we could not create another ServerSocket at that port again.

So from my understanding, can I conclude that, at server side, we can create multiple sockets under one port? (similar to what web server does)

Actually my question is, at client side, when we are creating a socket, we can specify the local port that we want to use. After we have successful created a client socket at that local port, can we reuse that port for other client socket? Does that port bind to the socket permenantly until the socket is closed (or port close)? Since there is no "Listening" concept at client side, are we able to do the same thing as ServerSocket does (refer to ServerSocket can create multiple socket under one port)?

I am seriously confused how client side handle the port and socket, because I am comparing ServerSocket with the client socket.

Please point me to the correct direction, I know that my thinking somehow is wrong. Thanks very much.

Upvotes: 5

Views: 4604

Answers (4)

Bob
Bob

Reputation: 81

In order for a TCP client/server to consistently and correctly identify it's conversation partner, said partner must be uniquely identifiable.

If it can be uniquely identified by it's IP address, this will be done.

However, what if the same machine runs two applications, both of which wish to communicate with the same partner about different topics?

In this case, the two applications use different port numbers to uniquely identify their traffic to the other machine.

There is not a lot of magic that distinguishes a server from a client at the fundamental level. Ultimately a server is a machine that makes connections with many other machines for a single purpose, whereas a client is a machine that makes connections to only one machine for a given purpose.

Upvotes: -1

user207421
user207421

Reputation: 310913

So from my understanding, can I conclude that, at server side, we can create multiple sockets under one port? (similar to what web server does)

You're confusing yourself with your terminology. ServerSocket.accept() accepts a connection, and wraps the endpoint in a Socket. The endpoint has the same local port number as the ServerSocket, by definition as per RFC 793, and therefore so does the wrapping Socket.

Actually my question is, at client side, when we are creating a socket, we can specify the local port that we want to use.

We can, but we rarely if ever do so.

After we have successful created a client socket at that local port, can we reuse that port for other client socket?

No.

Does that port bind to the socket permenantly until the socket is closed (or port close)?

Yes, or rather the other way round: the socket is bound to the port.

Since there is no "Listening" concept at client side, are we able to do the same thing as ServerSocket does (refer to ServerSocket can create multiple socket under one port)?

No.

Upvotes: 4

HazirBot
HazirBot

Reputation: 323

You should imagine a socket as a two-pair array of information:

  • {Self Port, Self Addr}
  • {Dest Port, Dest Addr}

therefore a single Server may have many connections connected to it that differ by their {Dest Port, Dest Addr}

example: Server port 10000 addr 10.0.0.1

Socket 1:

 - {10000,10.0.0.1}
 - {10001,10.0.0.2}

Socket 2:

 - {10000,10.0.0.1}
 - {10002,10.0.0.1} - address may seem the same but as a whole its a
   different destination

hope this helps.

Upvotes: 1

Joachim Isaksson
Joachim Isaksson

Reputation: 180917

A ServerSocket can simply be seen as a Socket factory for incoming connections. For every incoming client connection, the ServerSocket.accept() method returns a new Socket to communicate with that and only that client on.

In other words, any number of connections (limited only by the OS) can be made to the single ServerSocket, and each client connection will get a separate Socket to communicate on, all communicating using the same server side TCP port.

Upvotes: 4

Related Questions