So you say you've managed to send data from the Server to the Client, and it works fine. The same principle applies when sending data from client to server.
Have a Listening thread, on the server side. Inside the packet, you can include the name, or the IP address, of the user sending the data. IE:
Data Recieved by Server: 'message, hello chris, sender, reciever'
Your server can then split up the packet into it's meaningful sections using a string tokenizer.
Action: 'message'
Message: 'hello chris'
Sender: 'sender'
Reciever: 'reciever'.
Then you have all the data necessary to continue.
This implementation means that all the data you need is contained within the packet that you have received, and you don't have to check where it came from. The extra few bytes of data won't slow you down as much as having to perform a search every time you receive a packet.
Edit In response to OP Edit
Alternatively, instead of implementing an Array, you can implement a HashMap<String,Player>
. That way, all you need to do is grab the username from the packet sent, using my description above, and pass it to the hashmap. The hashmap can return the User object that represents the client, and there is no need, again, for an index lookup.
You can find the documentation on the Java Hashmap here.
Finally, I noticed you've got a player
class. The Java convention is that all classes start with an upper case letter. That is:
player
should be Player
.
Final edit in response to your comment
Your problem is an issue of bad design, I'm afraid. The clients should all be communicating to the server via the same channel, in this context. That way all the server has to do is listen out for any messages, parse them, and move them on. Adjust your implementation so that your client's all talk to the server via the same stream, and that will eliminate your issue.