0

I have the following application which get launched as below:

Runtime.getRuntime().exec("java -cp /var/dist/Test.jar System.V PDP-11");

It has 10 to 20 second of task also it contain TCP/UDP server on port 1965. Once that task is complete, my another hand sends an TCP byte to shutdown the System.V

Runtime.getRuntime().exec("java -cp /var/dist/Test.jar System.V PDP-11/20");

Within less then 1 seconds another action executes as above, but that fails because port 1965 is already in use. But it was killed using System.exit(0)

How do i resolve that? So that it does not say "already in use port"? It seems like it takes lot of time to de-initialize the TCP or UDP ports that was listening via Java. (where nc -l port get lot faster killed).

Follow up:

i tried following but still it takes too while to get that port available (Linux OS).

byte[] buf = new byte[50];

sock.setSoTimeout(100);

1
  • @ajozwik: java -cp /var/dist/Test.jar System.V here 1965 is listening and less then 1 seconds it does System.exit(0) ; and start itself. It fails because 1965 is not killed yet.
    – user285594
    Mar 8, 2012 at 22:18

1 Answer 1

0

It takes some time for OS to "notice" that port can be used again - even if you close the socket and kill the application. Problem can be solved with following code:

ServerSocket ss = ... //initialization of socket listening on port 1965
ss.setReuseSocket(true);

Please remember that code above is only a hint for OS that is should free port as fast as possible. Some exotic implementations of TCP stack might not honour this.

4
  • Did not resolved. I also tried ss.setTimeout(200); // miliscond but same.
    – user285594
    Mar 8, 2012 at 22:08
  • I can only help you with one more hint: Mar 8, 2012 at 22:36
  • I can only help you with one more hint: netstat -tvol This command (hopefully you run linux) should show you all listening sockets with some information on when they would be closed by OS. Maybe this will help you to debug the problem. Good luck :) Mar 8, 2012 at 22:41
  • 1
    @DennisRitchie ss.setReuseAddress(true); I would have expected you to check the API rather than just post 'did not resolve' here frankly.
    – user207421
    Mar 8, 2012 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.