Skip to content

Instantly share code, notes, and snippets.

@ecki
Last active July 10, 2017 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecki/e69bbca3826c838d51d6239901cb681f to your computer and use it in GitHub Desktop.
Save ecki/e69bbca3826c838d51d6239901cb681f to your computer and use it in GitHub Desktop.
Testing Listening Port
package net.eckenfels.test.javasystemtest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.channels.Pipe;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* Opens a number of Pipes and waits for you to look up the used ports.
* Enter one used port at a time, the program will tell you if it can bind.
* Press enter to terminate.
*/
public class PipeTest
{
static ExecutorService e = Executors.newFixedThreadPool(10);
public static void main(String[] args) throws IOException
{
Pipe[] pipes = new Pipe[5];
for(int i = 0;i < pipes.length; i++) {
pipes[i] = Pipe.open();
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
do {
String l = br.readLine();
int p = Integer.parseInt(l);
ServerSocket s = null;
try {
if (s != null) s.close();
s = new ServerSocket();
s.setReuseAddress(true); // same problem with and without
s.bind(new InetSocketAddress(/*"127.0.0.1", */p)); // works with 127.0.0.1
accept(s);
} catch (Exception ex) {
System.out.println("Failed " + p + ": " +ex );
}
} while(true);
}
private static void accept(final ServerSocket s) throws InterruptedException, ExecutionException, TimeoutException, IOException
{
Future< ? > r = e.submit(() -> {
System.out.print("accepting ... " + s);
try
{
s.accept();
} catch (Exception e) {
String m = e.toString();
if (m.contains("socket closed"))
System.out.println(" done");
else
System.out.println("" + e);
}
});
try { r.get(4, TimeUnit.SECONDS); } catch(TimeoutException ex) { /* ignored */ }
try { s.close(); } catch (Exception e) { System.out.println(" close " + s + " failed: " + e); }
}
}
> netstat -nao | findstr 5640
TCP 127.0.0.1:51276 127.0.0.1:51277 HERGESTELLT 5640
TCP 127.0.0.1:51277 127.0.0.1:51276 HERGESTELLT 5640
TCP 127.0.0.1:51278 127.0.0.1:51279 HERGESTELLT 5640
TCP 127.0.0.1:51279 127.0.0.1:51278 HERGESTELLT 5640
TCP 127.0.0.1:51280 127.0.0.1:51281 HERGESTELLT 5640
TCP 127.0.0.1:51281 127.0.0.1:51280 HERGESTELLT 5640
TCP 127.0.0.1:51282 127.0.0.1:51283 HERGESTELLT 5640
TCP 127.0.0.1:51283 127.0.0.1:51282 HERGESTELLT 5640
TCP 127.0.0.1:51284 127.0.0.1:51285 HERGESTELLT 5640
TCP 127.0.0.1:51285 127.0.0.1:51284 HERGESTELLT 5640
51276
accepting ... ServerSocket[addr=/127.0.0.1,localport=51276] done
51277
accepting ... ServerSocket[addr=/127.0.0.1,localport=51277] done
51278
accepting ... ServerSocket[addr=/127.0.0.1,localport=51278] done
51279
accepting ... ServerSocket[addr=/127.0.0.1,localport=51279] done
> netstat -nao | findstr 7316
TCP 127.0.0.1:51304 127.0.0.1:51305 HERGESTELLT 7316
TCP 127.0.0.1:51305 127.0.0.1:51304 HERGESTELLT 7316
TCP 127.0.0.1:51306 127.0.0.1:51307 HERGESTELLT 7316
TCP 127.0.0.1:51307 127.0.0.1:51306 HERGESTELLT 7316
TCP 127.0.0.1:51308 127.0.0.1:51309 HERGESTELLT 7316
TCP 127.0.0.1:51309 127.0.0.1:51308 HERGESTELLT 7316
TCP 127.0.0.1:51310 127.0.0.1:51311 HERGESTELLT 7316
TCP 127.0.0.1:51311 127.0.0.1:51310 HERGESTELLT 7316
TCP 127.0.0.1:51312 127.0.0.1:51313 HERGESTELLT 7316
TCP 127.0.0.1:51313 127.0.0.1:51312 HERGESTELLT 7316
51304
accepting ... ServerSocket[addr=0.0.0.0/0.0.0.0,localport=51304] done
51305
Failed 51305: java.net.BindException: Address already in use: JVM_Bind
51306
accepting ... ServerSocket[addr=0.0.0.0/0.0.0.0,localport=51306] done
51307
Failed 51307: java.net.BindException: Address already in use: JVM_Bind
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment