So what is happening here is that Processing is trying to open a port for the debug connection on a port between 8000 - 9000:
int port = 8000 + (int) (Math.random() * 1000);
I suspect you are running some kind of service (at 8080 perhaps) locally that Processing will occasionally (1 in 1000) match with that service. There is no check if this port is in use and the program will hang
import processing.net.*;
//import java.net.ServerSocket;
//import java.io.IOException
void setup()
{
// Uncomment the method you want to run
portScan_2(); // Using ServerSocket directly
println();
delay(3000); // 3s delay to see output.
// Don't like to use delay() but ok here.
portScan_0(); // Processing server
println("Done scanning.");
}
void draw()
{
}
//**************************************************************************************************
// Method 0: Using Processing Server
// Produces SocketException messages when stopping the server
void portScan_0()
{
for (int port = 8000; port <= 9000; port++)
{
try
{
Server s = new Server(this, port);
//println("Opened port: " + port);
//println(s);
s.stop(); // immediately free it
}
catch (RuntimeException e)
{
//if (e.getMessage() != null && e.getMessage().contains("BindException"))
if (e.getMessage().contains("BindException"))
{
println("Port in use: " + port);
}
else
{
println("Error on port " + port + ": " + e.getMessage());
}
}
}
}
//**************************************************************************************************
// Method 2: Using ServerSocket directly
// Does NOT produce SocketException messages
void portScan_2()
{
for (int port = 8000; port <= 9000; port++)
{
try (java.net.ServerSocket s1 = new java.net.ServerSocket(port))
{
//println("Port free: " + port);
}
catch (java.net.BindException e)
{
println("Port in use: " + port);
}
catch (IOException e)
{
// ignore other IO errors
}
}
}