Ok this is the final hurdle I’m struggling with.
Im trying to allow a client to communicate with a single other client when they prefix the target clients name to their message.
For example if Ian wanted to communicate with bill he would type:
bill hi mate
Now, currently when a new client connects to the server their name is stored in an array list. As this uses data packets and byte buffers it means that when I store a name, it also stores that names length. By default on the server the data packets buffer size is 512, therefore each names length is 512.
This hasn’t been an issue until now. I have done the following to get who the client wants to communicate with:
else
{
System.out.println( "Client said: "+response+"\nPacket Size: "+packet.getLength()+"\nString Length: "+response.length() );
String [] usrInput = response.split( " " );
System.out.println( "Length of split string: "+usrInput.length );
}
It splits their input into an array and looks at the first position of the array for the name. So, from above, we would have bill in the first position.
Here is where I go wrong; I tried using:
- contains()
- equals()
- matches()
and so on but all fail; I’m guessing because of the length of the name stored. So how can I reduce the size of the name stored to just, for example, bill – 4?
Cheers
You are constructing the string fom the packet incorrectly. The DatagramPacket contains its own actual length. You should be using that, rather than the length of the entire byte array.