Purpose: A small fix-up for the server class.
About: This will pretty much stop non-client connections.(And SYI)
First off go into your run method in the client class and remove ...
Code:
if(inStream.readUnsignedByte() != 14) {
disconnected = true;
PlayerHandler.players[playerId] = null;
return;
}
This is what causes most nullers to well ... null.
Then the server fix up editing the run method ...
What the code above should do is check for byte 14 to see if it's an actual client connecting.
But instead of checking if it's an actual client or not AFTER everything is already set up, we check it at the socket level. We're using a switch because it's a lot neater than a bunch of if statements. What the below code will do is if a client is connecting, checks for a value of 14, it creates a new player, if a non-client is connecting, value isn't 14, it will close the socket. Look at these two images ...
What we SHOULDN'T be doing ...

What we SHOULD be doing ...

To make this work just add this where you deleted the if statement.
Code:
while(true) {
java.net.Socket s = clientListener.accept();
s.setTcpNoDelay(true);
String connectingHost = s.getInetAddress().getHostName();
if(clientListener != null) {
InputStream input_ = s.getInputStream();
switch(input_.read()){ // switches the InputStream
/* Client connecting */
case 14: // what it should be
playerHandler.newPlayerClient(s, connectingHost); // connected
break;
/* Non-client connecting or general error */
default: // catches anything that shouldn't be happening
s.close(); // doesn't let it connect
break;
}
} else {
s.close();
}
}
That pretty much wraps it up.
For any questions ask me.
This is a member of my forums work.
He gave me permission