I am using Linux Mint 14 KDE. I've successfully created Java tcp server and client, server IP:port is 10.10.1.10:9900 and my client IP:port 10.10.1.10:9901. When i connect my client for the first time everything is OK. The client connects and sends some data and disconnects. When i try connecting the client with the server right after disconnecting i get the next Exception.
java.net.BindException: Address already in use
If i wait more than 1 minute everything is OK. On other forums i read that after closing the socket there is a tcp time_wait that is 60 seconds (1 minute). After changing it to 0 everything worked (If someone want to change it just add this line net.ipv4.tcp_fin_timeout=0 in /etc/sysctl.conf and execute this line in your terminal as root service networking restart). The next day i tried to do one more test and i got the same exception. I checked if the time is still 0 (U can check it with writing this in command line cat /proc/sys/net/ipv4/tcp_fin_timeout as root) and it was 0. I still had to wait 1 minute before reconnecting the client.
Can someone help me how to configure that way so i don't get this exception. Waiting 1 minute is too much for real-time application. I know i can do this with letting the OS dynamically assign port number that is free, but that is not what my application needs are.
After creating your client socket, but before binding it or connecting to the remote server, you should call Socket.setReuseAddress(true) to enable reusing the client port.
Don't specify a fixed client port, use zero. That way you will get a fresh one every time, or until you run out, which means you should be doing connection pooling at the client.
Related
I have a Java WebSocket server that uses Netty to establish WebSocket connections. This is basically a chat application where a few agents will be interacting with many players.
The agents connect to chat server from UI running on their browser. The agents can pick players from UI and chat with them, where as the players connect to server from the game websites.
The agents send pings every 5 seconds to keep connection alive which was OK until there was a Chrome update which introduced throttling, which was terminating socket connections when the agent tab goes idle for more than 5 minutes. To tackle this, I have increased the timeout for agents to 75 seconds on server side (earlier 30 seconds) and observed that pings were happening but every 1 minute even in throttle, which was sufficient to keep connection alive. What I want is that the agents should never disconnect, unless the person logs out or the internet disconnects.
There are about 10 agents and all of them connect to a remote machine via a VPN, which has very good configuration and highly available internet.
The problem I am facing is that some agents are experiencing random WebSocket disconnections, which I can confirm from the server logs.
I have an account on production and tested this myself, stayed idle for more than 2 hours (minimized browser), there was no disconnection, I am trying to figure out what else can cause disconnection, is my server terminating it or is it from the client.
I am trying to check socket state on server side using ss -apn as learned if I see a CLOSE_WAIT it means that termination request came from agent UI side, and a TIME_WAIT would indicate that my server is terminating connection, knowing upon which I can start debugging on the right side.
An example of the output of the command:
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp TIME-WAIT 0 0 [::ffff:127.0.0.1]:1234 [::ffff:127.0.0.1]:45962
tcp ESTAB 0 0 [::ffff:127.0.0.1]:1234 [::ffff:127.0.0.1]:26670 users:(("java",pid=7571,fd=2096))
How do I capture the exact moment when a state changes from ESTABLISHED TO a WAIT state? I wrote a script that would print the above output every 1 second with date, which I think is not right as the WAIT state may be cleared even before a second time.
In my server logs I know the exact time when the agent got disconnected, I would then like to check the socket state at that time.
Any thoughts please?
I'm connecting and logging in to a server on port 21. What I want to do next is to send a command string, however I have been instructed to send this command through port 50.
How do I change the port to 50 whilst connected on port 21?
I have tried connecting to the server on port 50 intially, and the onnection is refused. I have tried sending the command whilst connected on port 21 using ftpClient.sendCommand and I get a reply code 500 Unknown command
You should go back to the one who told you this requirement and ask about specifics because - to be blunt - the whole thing doesn't make real sense:
FTP works with two connections, control and data. The control channel is the one you open on port 21 and there is only this one, you can't change it back to another port afterwards. So "send a command on port 50 but connect on port 21" is not a thing with FTP.
A data connection on a port < 1024 is not usual, so it's hard to believe that the one with that requirement meant that, either. For one that would require active FTP connections that everybody nowadays tries to avoid because they are a pain to configure. Opening a listener on a port < 1024 would require root-privileges on Unix-systems which is unlikely you will get just for doing FTP-transfers.
You also said that the command you actually try to send to the server is confidential. I find that hard to believe but if it really is that would mean that we're talking about a non-standard FTP-server working with its own set of FTP-commands, i.e. regular FTP-clients might not be able to do what you need at all.
So go back to the one who gave you this requirement and ask, what the heck he's talking about, ideally with an example how to do it, let's say using the FTP-client that comes with the Windows Command-Shell.
Typically when you connect, the server controls the port assignment. So when you issue a pasv command (passive mode), it typically sends you back an IP / Port to connect to for the data connection. I'm not aware of anything that will allow you to do what you want. The server can restrict the port range for these types of things. Here's a nice explanation that goes over this.
Active vs Passive FTP
You may be able to send a PORT command to connect to port 50 specifically, but it's really unusual to do something like this.
** Edit **
There's two things you can try, I've never used either, so YMMV.
In Active mode, the client gives the server a port to connect to.
Try setting the default port to 50 and turn on Active mode. Hopefully this is on your internal networks, because this would never get past security for a firewall request.
client.setDefaultPort(50);
client.enterLocalActiveMode(); // Apache FTPClient
For Passive mode, you can try to set the active port range.
client.setActivePortRange(50, 50)
client.enterLocalPassiveMode();
You should ask what mode the server is expecting.
I wrote a Server and a Client with GUI to transfer text between them and they work well. I searched Google "how to send files through a network" then I found this beautiful solution (see Andrey Kozhanov reply) . I moved the server class in the link to my server package and the client class in the link to my client package to try and they work well. Now I can send instant text messages, and instant files.
I wrote every thing necessary to make communication work smoothly to meet users expectations.
The problem is The problem occur at a specific scenario.
The two servers have different port numbers; (mine for text and the one in the link for files)
Stage 1
1- the client request the server to send a file.
2- the server reply with (Yes) (dialogue yes-no-options).
3- the client navigate the File Chooser's List.
4- the client cancel the File Chooser (didn't send a file).
5- the server informed about the client cancellation.
Stage 2
- again
1- the client request the server to send a file.
2- the server reply with (Yes). error occur at the server side;
the first line of the error is:
java.net.BindException: Address already in use: JVM_Bind
I begin the receiving thread at the Server package like this
recieverThread = new Recieving();
recieverThread.setDaemon(true);
recieverThread.start();
The problem traced back to stage 1 number 5 (5- the server informed about the client cancellation.). since the server thread for receiving files from client start, then at point 5 I must stop it. to stop this thread I use combination of
recieverThread.interrupt();
recieverThread = null;
But when it started again at stage 2, it seems that it still running and is not stopped. I want to kill it so that when I want to start a new thread no error occur.
That is not a threading problem, but a server socket problem. Reusing a Socket immediately is disallowed by TCP/IP because the reuser could see packets targeted at the old user.
See this question on how to reuse sockets.
I am making a C# client / Java server chatroom, and everything works fine now, except for one thing:
After some time (an hour or so) of not using the application (or using it, I don't know) it gives me a SocketException at the C# client at the socket.EndReceive() function:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
Do Java or C# socket connections closes after some time of idling? Or is it just the tcp protocol?
What would be the best method to fix it?
Thanks all!
Bas
Do Java or C# socket connections closes after some time of idling?
No.
However, firewalls and especially NAT gateways do, often silently.
What would be the best method to fix it?
Implement a heartbeat procedure. i.e. the client and/or server periodically sends (e.g. every 10 or 30 or so seconds) a special message that's just used to keep the connection alive and to faster detect a failed peer.
Ok guys im not that experienced so take it easy on me.
Ok so i have 2 programs, one for the server (my pc) and one for the client(other pcs)
and this is the setup
server listens/accepts > client connects > server sends command to client > client executes.
and thats it, after that it disconnects BUT i need the client to stay connected so the server can keep sending commands as needed.
How can i achieve this?
I can provide more info if need just ask!
thanks for all the help guys.
The server will receive a socket when accept() returns, and as long as that socket does not get closed then the connection remains open. The client will then run in a loop of read/execute-command until the server closes the connection. I can provide more details if needed.
There's a few official examples to get you started with Java sockets. Plus since its Java, getting this setup is pretty easy.
As Kevin already said, you'll open up a ServerSocket on some open port. Then call accept() which will return when a client connects with a Socket on the same port as the ServerSocket.
Also, make sure you pay attention to the specified host if you want the client program to connect from a computer different from the host. You might run into trouble with the loopback interface if you specify the host as "null" or "127.0.0.1".