Checking a local TCP port is not open in Java - java

Is there a simple way to make sure that a local port is not already open. Some TCP socket servers (eg Grizzly) don't seem to do this check by default. When this check is missing, the server appears to start and respond, but the client code is just connecting to an old server that wasn't shutdown. This can be very bad!
Is there a simple line of Java code that could check to be sure that port isn't already used by another process?

I see two obvious ways to do it:
try to connect to that port on localhost, if you get accepted, the the port is being used
try to open a ServerSocket in listen mode in that port. If you get "already bound" exception, then the port is being used
Hope it helps.

Related

Change FTPClient port in order to send a command

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.

How Can I Share the Port of a Socket from a Server Program?

I have a simple TCP server which I wish to connect with a TCP client. This is all programmed in java. See Here.
I used the ServerSocket.accept() method to return a socket with a port allocated by java. (I set it to 0, thus randomly allocating the first available port). However, when I wish to connect to my server via client, I can not find the port, because it is not saved anywhere or displayed on the server page. How can I know what port to connect to?
And for future reference when I wish to connect multiple clients, or when I am unable to see the server running in a terminal, how can I view the display the port to the public?
Thank you!
If you need any more detail, please ask. I am new to all of this, and I find it hard to put these concepts into words when I am only fairly comfortable with them.

Port absolutely refuses to open, despite best efforts

So, I am trying to build a simple java chat application.
The application is supposed to establish a connection on port 5000. When I tried to give the jar to my girlfriend so she could test the client, she gets a connection refused, so I first though to check my ports. Port 5000 absolutely refuses to open.
I have tried everything I can think of (although I am definitely not IT savvy so I could've missed something);
Here are steps I have taken:
In command prompt (while the server is running) I have used netstat -an and found port 5000 is listed as listening. When the server is not running, this port is not listed.
I have disabled any and all antivirus software /firewalls on my rig, and in my router settings.
I have set up a static IP for my rig.
I have port forwarded port 5000 on my router and checked and double checked that I have done it correctly.
I even contacted my ISP, who swears up and down that they don't block ports.
I have tried other ports, not just 5000.
After the above steps, I have used several different sources to check whether port 5000 is open. That thing is more blocked than Michael Moore's arteries.
My question: Why does the internet hate me?
EDIT: over endless hours of chat we (myself and user roelofs) were able to get it working on port 21, (the ftp port), which isn't a problem because there wont be any conflicts, but it isn't right. If anyone can point me in the right direction, I'd much appreciate it. It seems to be some kind of windows specific problem? Maybe? Can confirm ports are correctly forwarded and any kind of security that I can think of is off.
'Connection refused' means that there was nothing listening at the IP:port you tried to connect to. So, either the IP or port was incorrect or your server wasn't running. If the IP and port were correct it also means your firewall is forwarding the port correctly.
'Connect timeout' to a current Ip:port on the other hand indicates that your firewall isn't set up correctly.
After some testing, it appears that windows might have problems with listening sockets above 1024. Running the code on a port lower than that (and setting up all port forwarding correctly) should get you to the point where you can debug.

Chat server stops working

I have created a normal chat program which has just a server and a client class. I run the server at my end. The chat clients are run from different machines. In my program, I've specified a random port number which all the clients use a socket connection to connect to the server that runs on my machine. The first issue is that I've to disable firewall to get this working (probably, the firewall blocks the port I give). How to specify a port number that firewall can accept? Do I HAVE to open a port myself?
Secondly, after disabling firewall, everything works but all of a sudden, the connection is lost. None of the clients can send messages. What could possible be the reason for this? Not sure if it is caused due to the port I select.
You need to set Socket connection timeout properly using this.socket.setSoTimeout(timeOut);
to prevent timeout which must be causing connection loss.
In order to allow socket comunication through firewall go through this document : http://windows.microsoft.com/en-US/windows7/Allow-a-program-to-communicate-through-Windows-Firewall

Java - How to keep a live socket connection

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".

Categories