This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Client identification problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Client identification problem" Watch "Client identification problem" New topic
Author

Client identification problem

supriya acharya
Greenhorn

Joined: Jan 26, 2009
Posts: 28
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()
socket=ss.accept() ;
new ConnectionHandler(socket,++id).start() ;

This is the second class which is ConnectionHandler Class.

and this is constructor of ConnectionHandler class

ConnectionHandler(Socket socket,int id)
{
conn=socket ;
idcon=id ;
}
public void run()
{
Map<Integer,Socket> sockets = null;
int id=1 ;
try
{

sockets.put(id, conn) ;
System.out.println("After sockets put") ;
System.out.println(conn=sockets.get(idcon)) ;

}
}
this is not the entire code but when i run my code it shows nothing just build successful....

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1474
    
    6



Swastik
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41630
    
  55
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.


Ping & DNS - my free Android networking tools app
supriya acharya
Greenhorn

Joined: Jan 26, 2009
Posts: 28
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

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41630
    
  55

catch(Exception e)
{}

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.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1474
    
    6

You were probably not initializing the map, there is a small change I made in ConnectionHandler class, see if it works

supriya acharya
Greenhorn

Joined: Jan 26, 2009
Posts: 28
Thanks it worked!!!
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1474
    
    6

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.



and we do not initialize inside the constructor



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Client identification problem