How could one single server socket communicate with multiple client sockets - java

I've seen this post
http://docs.oracle.com/javase/tutorial/networking/sockets/definition.html
since it wrote:
If everything goes well, the server accepts the connection. Upon
acceptance, the server gets a new socket bound to the same local port
and also has its remote endpoint set to the address and port of the
client. It needs a new socket so that it can continue to listen to the
original socket for connection requests while tending to the needs of
the connected client.
So are there multiple server sockets which has the same port in the server side?

There is one ServerSocket. It accepts incoming connections through the accept() method. This returns a Socket which you use on the server side to handle the connection to a particular client.

Related

Socket Connection Issue

I know one socket connection are established by both Server Socket and Client Socket.
And I read some documents said one Server Socket could serve many Client Sockets, means one Server Port could server multi Client Ports.
1.But I wonder that does Server use random ports to server different Clients after connection under hood, or Server just uses the same port listening and serving many client's connections ?
2.If so, when I implement a Server and Client Socket Connection, could I random a new port to establish a new Server Socket and tell Client to reconnect to new Server Socket, and the listening Server Socket just keep listening other clients ? it means use different port to server different clients ?
3.And what is the advantage of using one Server Socket(port) to server many Client? and advantage of using multi Server Sockets(ports) to server different Clients?
Thank you
The two value that idenify each end point, ip address and port number often called socket.
A server socket listens on a single port. All established client connections on that server are associated with that same listening port on the server side of the connection.Multiple connections on the same server can share the same server-side IP/Port pair as long as they are associated with different client-side IP/Port pairs, and the server would be able to handle as many clients as available system resources allow it to.
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
server.listen(4200);
Here u can attach your http port with socket.io.
by using a random client-side port, in which case it is possible to run out of available ports if you make a lot of connections in a short amount of time.
for more detail visit this site

Java Client Server Application - Address already in use: connect

I am currently working on a simple multiplayer game where serveral clients need to connect to a server.
My server consits of a single serverSocket. This serverSocket accepts incoming connections and hands them over to connection object that starts a separate thread.
ServerSocket seso = new ServerSocket(12345);
while(true){
Socket toClient = seso.accept();
new Connection(toClient); //creates a thread that opens streams etc
}
Clients open a new Socket and connect to this server.
Socket toServer = new Socket();
toServer.setReuseAddress(true);
toServer.bind(new InetSocketAddress(65432)); //always using the same port
toServer.connect(new InetSocketAddress(serverIP,12345));
Now if i close the connection to the server using toServer.close(); and try to connect again to the server, i get an "address already in use: connect" exception.
Using TCPView i can see that the state of the client procress changes to TIME_WAIT. But shouldn't i be able to use this port again because of setReuseAddress(true)? Am i using it wrong or is it an server problem?
I do always call .close() on toClient and toServer. Nevertheless i always have to wait until the socket is completely closed (after TIME_WAIT) before this client can connect again to the server.
When i close the entire application, the socket is immediately closed (not in state TIME_WAIT) and this client can connect to my server. (And ofc there is a connection reset exception in my server)
How can I do that without always closing the application ?
Thanks for your help.
To expand on my comment, a client / server protocol requires the server to listen on a port known to or discoverable by the client -- that can be considered the definition of "server" -- but it does not ordinarily require clients to connect from a specific port. If you do not bind the client socket to a particular port, then the underlying system will choose an available (source) port automatically and transparently.
If the server depends for some reason on clients connecting from a particular port, then you should re-evaluate that aspect of your design. If it does not, then you are making your own trouble by having clients connect that way. This should be all you need to do:
Socket toServer = new Socket();
toServer.connect(new InetSocketAddress(serverIP, 12345));

Why Java ServerSocket accept() returns a socket with the same port as ServerSocket?

In the server side, i use this code :
ServerSocket server = new ServerSocket(1234);
Socket server_socket = server.accept();
I found the server is listening on port 1234.
When one or more client sockets are connected, they are all using the same port 1234 !
That is really confusing :
I remember that multi sockets can't use the same port, isn't it right ? Thanks.
A TCP connection is identified by four numbers:
client (or peer 1) IP
server (or peer 2) IP
client port
server port
A typical TCP connection is open as follows:
The client IP is given by the client's ISP or NAT.
The server IP is given by the user or looked up in a DNS.
The client chooses a port arbitrarily from the unassigned range (while avoiding duplicate quadruples)
The server port is given by the protocol or explicitly.
The port that you specify in the ServerSocket is the one the clients connect to. It's nothing more than a port number that the OS knows that belongs to your application and an object that passes the events from the OS to your application.
The ServerSocket#accept method returns a Socket. A Socket is an object that wraps a single TCP connection. That is, the client IP, the server IP, the client TCP port and the server TCP port (and some methods to pass the associated data around)
The first TCP packet that the client sends must contain the server port that your app listens on, otherwise the operating system wouldn't know what application the connection belongs to.
Further on, there is no incentive to switch the server TCP port to another number. It doesn't help the server machine OR the client machine, it needs some overhead to perform (you need to send the new and the old TCP port together), and there's additional overhead, since the server OS can no longer identify the application by a single port - it needs to associate the application with all server ports it uses (the clients still needs to do it, but a typical client has less connections than a typical server)
What you see is
two inbound connections, belonging to the server (local port:1234). Each has its own Socket in the server application.
two outbound connections, belonging to the client (remote port:1234). Each has its own Socket in the client application.
one listening connection, belonging to the server. This corresponds to the single ServerSocket that accepts connections.
Since they are loopback connections, you can see both endpoints mixed together on a single machine. You can also see two distinct client ports (52506 and 52511), both on the local side and on the remote side.

Java socket programming: Is it possible for the server socket to initiate a communication?

1, my server has a ServerSocket to accept client connection
2, my client create a socket and and send message to the server.
3, the server gets the message and reply to the client.
So it's always the client that initiate the communication.
Say some time later after initial client connection, the server wants to send another message to the client, is it possible to do so?
If you keep the socket connection from the client open on the server yes. The client would need to be always listening for data coming in.
Otherwise the client will have to poll in intervals to the server if the connection on the server is closed after a message is sent.
The client must always initiate the connection, that's pretty much the only distinction between a server and a client (a program can be both be a server and a client).
However once a connection is established, as long as it's not closed either side can send data to the other (assuming the other side is listening for data).

client server interaction question

I am trying to implement a minimal chat server in java over regular TCP protocol. The chat server will listen on a specific port. The question I have is if there are multiple clients sending messages to the same port, can the server distinguish between the clients and respond to each individually if the messages do not contain the IP address or destination name of the client?
to make my question a bit more clear, suppose the server gets a packet that contains only
"user: abc to-user:efg message:"Hello""
Can I find out in java the address of the client who sent the packet and respond back to the same address or will I need to include some identifier in the message itself like "sender-ip = 1.1.1.1"
Multiple clients will never send data over the same port. The only time your clients will talk over the same port is when they will connect to the server. In the server, whenever the ServerSocket receives a connection it returns a new Socket. This socket is a combination of the following : Server IP+ServerPort and Client IP+Client Port. The Server IP and the Server Port will be same for each socket; what differs is the client IP and Port. Usually this socket is passed to a new thread for further communication while the ServerSocket goes back to listen to incoming connections. Once you have a reference to the socket you can call socket..getInetAddress().getHostAddress() to get the remote IP and socket.getPort() to get the port of the respective client.
Yes, each connection will be separate - you'll have a different stream to read from for each connection. It's up to you to associate the relevant user information with the connection though.

Categories