I've built a simple little network system based on Server and Client. I can't always control when the parts start so I'm trying to make the connection process as smooth as possible.
I'd like to be able have the client start first and then cleanly pick up when the server starts. It needs to retry a connection, or at least handle the timeout cleanly. And I like to be able to stop the app early if I change my mind and decide to not to start.
The server side works fine with nothing special. The app is responsive works as expected while waiting for a client to connect. The client side seems to block until it either times out or connects. During the client's timeout period the app is not responsive and can't be quit early if the user decides to give up. Finally if it does timeout there isn't a direct way to detect that it timed out.
I eventually built a bit of code that does works to some level. I have a simple state machine in draw() that works through a couple startup states.
println("waiting for server");
// there's also a text() here expleining this to user.
boolean gotNetwork = true;
myClient = new Client(this, "127.0.0.1", 21015);
try {
String tmp = myClient.ip();
}
// it appears there is no direct way to detect a successful connection.
// However Client.ip() will throw a NullPointException when it's not connected.
catch (java.lang.NullPointerException e) {
gotNetwork = false;
}
if (gotNetwork) {
initStage++; // controls my draw statemachine. incrementing moves to next state
}
There's doesn't appear to be a direct way to notice that the connection failed so I do it by looking for a NullPointerException from .ip() . Also, this lets me handle the 'ESC' key when it times out and falls off the end of draw(). It's not instant but it works.
Any thoughts on how I'm pulling this off? Suggestions on a better a way to do either of these things?
I've been playing with the network library and found an interesting problem.
This happens with the
examples for Client and Server unmodified from the library documentation. When these apps are run from the editor everything works as expected. When I run exported versions the server doesn't accept connections.
I save and export both. When I run the server (as x32 or x64) clients very rarely connect and eventually timeout. This behavior is the same whether I'm using exported or editor run clients. They do occasionally connect, but it's much less than than 1 in 10.
If I run the server from the editor exported (and editor) clients will connect as expected.
My system is Win7 x64. I'm running Processing 2.0.1 x32. I run the x32 processing because I do a lot with Serial and it doesn't have an x64 version. This behavior was also happens in Processing 2.0b8 x32