Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a list of clients connected to server?  RSS feed

 
Henry Porter
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
Im new to this Java ranch, so if this is repeating something I havent seen, I apologise!
Ok, my question is, can anyone tell me how to create a list of clients port and InetAddress details as they connect to the server socket.
I need to do this so that I can use the details to send messages back to all the clients.
(Im writing a client server chat program where one message from one client is displayed to all the other clients)
I have a thread that generates a new socket and I try to push these into a stack, which i then pop off to send a message back.
It works, but it seems I can only push one port on, ie a new stack is also generated everytime a socket is..
pleeeease help. Its driving me nuts! (and my family...!)
Thank you for any help offered
Henry
 
prabhat gupta
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what you can do is make a Vector object and keep on adding the chatclient object containing all relevant information about its socket and then send the message that arrives on the server to all the clients connected, by first getting an enumeration object from the vector and using while loop as follows
while(enum.hasMoreElements())
{
Socket client=(Socket)enum.nextElement();
PrintStream ps=new PrintStream(client.getOutputStream());
ps.println(message);
}
the complete set of classes as per my chat program should be as follows.
ChatClient()
ChatServer()//multithreaded, so that it runs a seperate thread for each client logged in.
SocketClient() extends Socket
//this class must also store clients name and if necessary its serversocket if your chat program is also capable of sending a personal message.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!