This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I have a server and it has to identify clients. Suppose if i have 2 clients A and B how will my server identify them? and if server has to send some string to A and some string to B how will the server send it?
I used Map from util package but it is not working.
This is the server code
i have 2 classes one is the one which has the main()
new ConnectionHandler(socket,++id).start() ;
This is the second class which is ConnectionHandler Class.
and this is constructor of ConnectionHandler class
I'm not quite clear on how the code is supposed to work, but I think that "sockets.put(id, conn)" should read "sockets.put(idcon, conn)" because "id" always has the value "1" at that location.
If you need to identify clients, I'd suggest that each client sends a username/identifier at the beginning of the connection. Make sure it's unique on the server, and let the client know if the server chose a different one if it was in use already.
Might have worked, but there is still a minor bug in the code. If two clients are connected to the server, your map size should be 2, but as per your code it will show one because each time the ConnectionHandler object is created a new map object is created as well means the previous connection references stored in the map are lost. So the solution might be is to declare the map reference as static.