We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I am trying to get to work a simple server-client communication using Processing's Network library.
I want that my server refuses new clients past a certain amount of already connected clients.
Here is my serverEvent method :
void serverEvent(Server _S, Client _C) // is run when a client connects
{
if(idCount > maxClients)
{
_S.disconnect(_C);
println("Client "+idCount+" refused.");
idCount ++;
}
else {
S.write(_C.ip()+" "+Integer.toString(idCount));
println("Client "+idCount+" connected.");
idCount ++;
}
}
The else part works fine. But when the program executes the if part, it throws this exception :
java.lang.NullPointerException
at processing.net.Client.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
However the exception is thrown outside the if brackets : I can print something after the if/else statement before the exception is thrown.
Any hint to solve this ?
Answers
What do you mean by "certain amount of already connected clients"? What is the number?
I've set
maxClients = 10
. I don't intend to set it to a much larger value.Can you please post a more complete example? It's pretty hard to help you without seeing what's going on. Try narrowing your problem down to a MCVE.
Sure !
To reproduce the problem :
- Create one sketch for the server and one for the client
- Run the server, then the client
- The server throws the exception
.
after line 24
I ran it a first time. The server didn't throw the exception when I ran the client sketch. I closed the client and ran it again, and then the server threw the exception :
I closed the server and started it again and this time it threw the exception on the first client connection.