This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori 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.
Joined: Jan 26, 2009
Now it is getting the client input but its is not showing its id..ie map is still not working..though i pasted it above the run method
Joined: Mar 22, 2005
That a big no-no! You must put in place proper error handling for I/O code. At least print a message to the console/log file.
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.