Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 251
Sam YC Avatar asked Jul 18 '13 17:07

Sam YC


People also ask

How does socket differ from ServerSocket in java?

The Key Difference between Socket and ServerSocket Class is that Socket is used at the client side whereas ServerSocket is used at the server side.

What is difference between socket & ServerSocket?

A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester. This class implements client sockets (also called just "sockets"). A socket is an endpoint for communication between two machines.

What is the use of socket and ServerSocket?

The Socket class is used to communicate client and server. Through this class, we can read and write message. The ServerSocket class is used at server-side. The accept() method of ServerSocket class blocks the console until the client is connected.

What is the use of socket and ServerSocket in java networking?

Socket class represents a socket, and the java. net. ServerSocket class provides a mechanism for the server program to listen for clients and establish connections with them. The server instantiates a ServerSocket object, denoting which port number communication is to occur on.


Video Answer


2 Answers

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.

like image 178
user207421 Avatar answered Sep 18 '22 05:09

user207421


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.

like image 21
Joachim Isaksson Avatar answered Sep 18 '22 05:09

Joachim Isaksson