Question

I am presently running jboss on 8090 port and my client socket is running in the jboss. I send request to server socket at scheduled intervals to fetch data. Now my question is

  1. As 8090 port is already in use is it possible to for the client socket to open on that port and connect the server?

  2. Is it possible to run two applications on a single port?

  3. How to skip a port if it is already in use. My Client socket opens at a time 5,000 requests so it opens that many local ports simultaneously. So if there is any port already in use I need to skip it.

Was it helpful?

Solution

For TCP/IP, no. You can only have one application listening on a single port at one time. Now if you had 2 network cards, you could have one application listen on the first IP and the second one on the second IP using the same port number.

To skip it, you would require a simple check to see if it is in use:

boolean portTaken = false;
ServerSocket socket = null;
try {
    socket = new ServerSocket(portNumber);
    } 
catch (IOException e) {
    portTaken = true;
} 

Then for each port check the boolean to see if it is in use until you find one available.

OTHER TIPS

You don't need to worry about the client socket's local port. The OS will find one for you, unless you are using too many, which is a distinct possibility if you have 5,000 at a time. Just don't specify any local port information when opening the Socket.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top