• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

reserved and non reserved port nums

JoeCDaMan

Senior member
Hi, I'm writing a network app in Java and am having a problem reserving port numbers for my program. Curently I am able to use port 5000, but that is it, I am unable to specify another specific port num. If I do not specify a port number the OS will designate one for me. But for the purpose of comunication between clients I would like to have another static port number. I understand that numbers less than 1024 approx are reserved by the OS so I have not attempted to obtain one of those. I have even tried to reserve a port number that the OS designated for my use during a prior execution alas to no avail. Anyway, does anyone have any idea why I can only reserve port 5000? and any other port nums I could try would be appreciated. Thanks
 
Port numbers below 1024 are generally only allowed to be listen()'d on by root, the idea was that they were assigned to specific services and no user could start one of those servers without talking to the admin of the box.

What you're having seems to me to either be Java or OS specific, I know I'd written apps that use arbitrary ports using C and had no problems.
 
Thanks for your help, but does anyone have any specific ports that you used in an application and got them to be static. I really am desparate and don't understand why I can only request port 5000, thanks again.
 
I've written a few java socket listeners and except for the "only root can open ports below 1024 issue" I've been able to specify whatever port I feel like using.

Can you leave us some sample code to look at? What JVM version? What OS?
 
When you say that you cannot request a port other than 5000, what are you trying? Are you trying to open port 5000, then open another port after that? Or, are you taking your working port 5000 code, and changing the code to try a different port instead? It's possible that if you are doing the first, that there is a bug in your code that is causing a conflict between you opening your first port and then another one after that.
 
ok here is the code that works w/ port 5000

private DatagramSocket socket;
try
{
socket = new DatagramSocket(5000);
}
catch(SocketException se)
{
se.printStackTrace();
System.exit(1);
}

I'm not sure about the VM version but I'm using winXP pro and that's about all she wrote, I know that this really isn't much code, but this is all that pertains to choosing a port.
 
Then you replace the 5000 with another port and the code fails? What error does it report?
 
Sample code I used for testing:

private static DatagramSocket socket;

public static void main(String[] args) {

try {
for (int i = 5000; i < 9000; i += 500) {
socket = new DatagramSocket(i);
System.out.println("Listening for connections on port "
+ socket.getLocalPort());
System.out.println("IsBound=" + socket.isBound());
System.out.println("Bound to this local address is: " + socket.getLocalAddress());
socket.close();
}
}
catch (Exception e) {
e.printStackTrace();
}

}

It correctly binds to ports 5000, 5500, 6000, 6500, 7000, 7500, 8000, 8500.

If its not working for you we're going to need any error messages given or a larger code sample to try and find the error.
 
ok, well let me give you some additional information about my program, right now it is a client / server based application where the user first has to gain access to the system by verifying itself with the server. After verification the server will then give a unique ID where the client can now show this ID and speak to other clients. The server needs to be hard coded so that all clients are able to reach it, so I would like to keep it at port 5000. I would also like to have a static port for the clients as well because one one client per computer is needed and I would like to make my life simpler instead of dealing with random port numbers and having to keep track.

Kilrsat - when I tried your code I recieved an error that the datagramsocket class doesn't have the method .isBound() I also went to class socket which doesn't have it either. so I used the method .getLocalAddress() which will return the local address to which the socket is bound. the results were all 0.0.0.0/0.0.0.0 I'm not sure if it is the proper method to use but everything came out null.

and unfortunately I don't recieve any error messages, the program just doesn't do it. When the server verifies a client I have it print out the address and the port number of the packet it just received and the port numbers are all random and all over the place I really just don't understand.

Here is the code that I use to verify a user with the server

if(e.getSource()==name)
{
try{
String s = e.getActionCommand();
byte data[] = s.getBytes();
if(data[0]!= '~'&& data[0]!='-'&& data[0]!= '+' && data[0]!='*')
{
buddies.append( "\nSent: "+ e.getActionCommand() + "\n");
sendPacket = new DatagramPacket (data, data.length,InetAddress.getLocalHost(), 5000);
socket.send(sendPacket);
}
catch (IOException exception){
display.append(exception.toString() + "\n");
exception.printStackTrace();
}

I hope this sheds some light on the situation. Ohh and perhaps I am not including a necessary library?
here are the four that I use

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

thanks for your help
 
Kilrsat - when I tried your code I recieved an error that the datagramsocket class doesn't have the method .isBound() I also went to class socket which doesn't have it either. so I used the method .getLocalAddress() which will return the local address to which the socket is bound. the results were all 0.0.0.0/0.0.0.0 I'm not sure if it is the proper method to use but everything came out null.

You're using an older version of the JDK then. isBound() was added in 1.4. To find out what version you are using:
Start -> run -> cmd -> java -version

For best development, figure out what JRE version the clients are using, and develop on that. As for the local address, yes they should have been all 0's, since we never bound it to a specific address.

I'm at work at the moment, so I can't do any further code testing, but I'll check a few things out when I get home in a few hours.

One other thing, is there a reason you're opting for UDP instead of TCP?
 
I tried finding out what version of java I was running, but the window closes way to fast is there a way to pause the screen other than the pause button?

Anyway I went to sun's website and downloaded j2sdk version 4.2 so I will install that shortly hopefully that will fix my problem.

The reason that I am doing UDP is because the server was already written that way for a different client program so I made as few changes as necessary to accommodate my program.
 
Originally posted by: JoeCDaMan
I tried finding out what version of java I was running, but the window closes way to fast is there a way to pause the screen other than the pause button?

Open a command prompt (on Windows XP Start->Run and type cmd). You will then be able to check the Java version number.


 
Sorry for the slight delay,

sendPacket = new DatagramPacket (data, data.length,InetAddress.getLocalHost(), 5000);

That's the line that starts to make me wonder. Are you really just attempting to send a packet from a machine to itself on port 5000?

and unfortunately I don't recieve any error messages, the program just doesn't do it. When the server verifies a client I have it print out the address and the port number of the packet it just received and the port numbers are all random and all over the place I really just don't understand.

I don't see the code you're using to verify what port packets were received on, so I'll have to be general on that topic. When a client sends a packet to the server, it can come from any port. The only thing set in stone is the port the server is listening on.

Now if the first line I referenced above is indeed sending a packet, it is sending it to port 5000, what port it is sending from is up to the system to figure out. Its quite possibly that you're checking the originating port instead of the received on port, and hence why you're getting "random" numbers.

I can't do much more beyond this with the basic idea I have of the program.
 
Back
Top