Java Client Server Application - Address already in use: connect - java

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));

Related

Socket.IO Restoring a broken connection

In a client-server application, I send a string to the server in a separate thread:
dataOutputStream.writeUTF(_dos);
dataOutputStream.flush();
But I get in another thread java.net.SocketException: Connection reset in the line:
mes = dataInputStream.readUTF();
At the same time, the server does not receive anything (I checked this in wireshark) and continues to listen to the socket.
The question is: is it possible, if the client socket is damaged, but the server socket is working, to restore the operation of the client socket and thus restore the client-server connection, without restarting both?
The connection is gone. On the client side, you will need to connect again.
Generally speaking, TCP implementations do not allow you to issue a connect on a socket that has become disconnected. You can try, but it's safest to make a new socket object. Even if it works for you, the result may be non-portable.
The server of course also has a socket on a defunct connection, so it needs to close its end. A new socket will be created on 'accepting' the new connection from the client.

Application java with socket

I have a problem with the socket in java: I have a server on the desktop PC that I've a ServerSocket object Instantiated such:
ServerSocket socket = new ServerSocket(port);
where "port" is a number of port.
This serverSocket accept a client. This client is another application java that runs on another desktop PC that connected to the same net of the server PC. In this application java, I Instantiated a Socket object which connects with local IP of server PC (example: 192.168.1.11) and port of serverSocket. When I run the server and the client, the client does not connect to the server. If I run the server and the client on the same desktop PC, the client manages to connect to the server.
I was thinking of a problem with the firewall configuration but I did not manage to solve it. Help me please.
This is code of server:
//I will accept one client
ServerSocket server = new ServerSocket(19999);
Socket client = server.accept();
new Thread(new ManagerConnectionThread(client)).start(); //this thread manage the requests
ManagerConnectionThread is a class that manage the communication between server and client:
//This code is on the server
OutputStreamWriter out = new OutputStreamWriter(client.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
This is code of client:
public static void main(String[] args){
Socket client = new Socket(InetAddress.getByName("192.168.1.11"), 19999); //192.168.1.11 is IP of "server" pc
OutputStreamWriter out = new OutputStreamWriter(client.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
}
do you have a router involved? if yes you will need to configure some port-forwarding and try to access via the external address, meaning that you will try to connect through the router that will point to your machine.
Also, you might need to do some tweaking with the firewall, at some point I was doing something similar for school, but as a server, I was using a Linux based OS so the procedure might be different, but I definitely needed to allow in/out for the port that I was using.
you might find some answers in this post which is similar
Java Chat with TCP/IP over LAN
Edit: I found my old homework, and I have definitely used the external address (especially because I wanted to access it from school, while the server was home) so I would connect to my external address of the laptop and then in the router's settings I would forward the port that I was using to the server, in the server allowing incoming traffic for the specific port.

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

How could one single server socket communicate with multiple client sockets

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.

Can I close and reopen a socket?

I learned an example of usage of sockets. In this example a client sends a request to a server to open a socket and then the server (listening to a specific port) opens a socket and everything is fine, socket is "opened" from both sides (client and server).
But it is still not clear to me how flexible is this stuff. For example, is it possible for the client to close an opened (from both ends) socket and to reopen it again (under condition that the server keeps the socket opened).
Is it possible for the server to "know" that a socket was closed on the client side? Is it possible for the client to know that a socket was closed on the server side?
ADDED:
One more important thing to me. What happens if a application (no mater server or client) crashes, abnormally terminated, killed? Will it close all sockets opened on the side of the application?
ADDED 2:
What if an application on one side of the socket is switched off (killed, closed, terminated) and then it is switched on again (on the same IP address and the same port). Should we create a new socket between the two applications or we can use the old socket (created before the crash).
A socket can be used for a lot of things for which the answers to these questions would change, but I'll assume you're talking about TCP.
For example, is it possible for the client to close an opened (from both ends) socket and to reopen it again (under condition that the server keeps the socket opened).
No, because TCP will perform a goodbye and you can't pick up the connection from there again. You'd have to do the three-way handshake again, and that starts a brand new connection.
Is it possible for the server to "know" that a socket was closed on the client side? Is it possible for the client to know that a socket was closed on the server side?
Yes. TCP can send out a goodbye packet or one side can time out and it's entirely possible to detect these scenarios in both cases.
Is it possible for the server to
"know" that a socket was closed on the
client side?
When server tries to send some data to that client a correspondent exception will be thrown.
One more important thing to me. What
happens if a application (no mater
server or client) crashes, abnormally
terminated, killed? Will it close all
sockets opened on the side of the
application?
Exceptions are created for handling these abnormal cases. If there is a black out and a client (or server) is turned off then other side will get an exception as soon as it try to interact with turned off side.
UPD:
What if an application on one side of
the socket is switched off (killed,
closed, terminated) and then it is
switched on again (on the same IP
address and the same port). Should we
create a new socket between the two
applications or we can use the old
socket (created before the crash).
Create new socket.
Q1->Is it possible for the server to "know" that a socket was closed on the client side? Is it possible for the client to know that a socket was closed on the server side?
A1 -> The sever will know, if client closes socket and vice versa. Actually FIN will be sent from the end, which is initiating to close connection.
Q2-> What if an application on one side of the socket is switched off (killed, closed, terminated) and then it is switched on again (on the same IP address and the same port). Should we create a new socket between the two applications or we can use the old socket (created before the crash).
A2->If socket is created, fd number is bind with ip-addr & port, so socket on server side has server-ip & some port and socket on client side has client-ip & some port. if crash happened at client the all fd are freed and it can not be reused.if we use the same fd, system will treat it as normal file fd.

Categories