Storage / Retriving Sockets From Vector

Hello...

I'm developing a network application. I need to store the sockets accepted from my ServerSocket object in a Vector for temporary usage. Is there any way to get the particular Socket that is connected to a particular client, from the Vector, for communication with that Client?

Please help me..

Thanks in advance...

R. Kaja Mohideen

[372 byte] By [Kaja.Mohideena] at [2007-11-27 0:01:40]
# 1
Yes. You can use a MapSomething likeMap<Client, Socket> m = ...depending on what class you are using to represent your client (String, etc) you can build a map to associate the client with the socket
tjacobs01a at 2007-7-11 15:53:12 > top of Java-index,Java Essentials,Java Programming...
# 2
Or IOW - don't use a Vector - use a Map, and then you can use methods: Objectput(Object key, Object value) -and-Objectget(Object key)
abillconsla at 2007-7-11 15:53:12 > top of Java-index,Java Essentials,Java Programming...
# 3
I'm a beginner in Java...So, anyone can provide a example on how to use Map ?I'll be thankful to you...
Kaja.Mohideena at 2007-7-11 15:53:12 > top of Java-index,Java Essentials,Java Programming...
# 4

> I'm a beginner in Java...

> So, anyone can provide a example on how to use Map ?

> I'll be thankful to you...

It's not much more difficult that using a Vector. Two of the methods you can use I just mentioned above. Use the API doco and read the [url http://java.sun.com/developer/JDCTechTips/] TTips [/url] and searches.

If you have code using Vector, post it and perhaps someone will help you convert it or show you where in the code to make the changes.

abillconsla at 2007-7-11 15:53:12 > top of Java-index,Java Essentials,Java Programming...
# 5
use google http://www.google.com/search?q=java+using+maps&rls=com.microsoft:en-us:IE-SearchBox&ie=UTF-8&oe=UTF-8&sourceid=ie7&rlz=1I7GGLF
tjacobs01a at 2007-7-11 15:53:12 > top of Java-index,Java Essentials,Java Programming...
# 6

You probably don't want to do this at all. You probably want to have a Runnable object per client which contains its socket, its input and output streams, and anything else you need to know about the client, and whose run() method handles the client session, and you want to start a a thread per client to execute that Runnable.

The occasions when you need a single data structure containing only accepted Sockets are few indeed, in fact I don't think I've ever used one.

ejpa at 2007-7-11 15:53:12 > top of Java-index,Java Essentials,Java Programming...