Using Threads to Handle Sockets

Posted by user340468 on Stack Overflow See other posts from Stack Overflow or by user340468
Published on 2010-05-13T16:20:01Z Indexed on 2010/05/13 16:24 UTC
Read the original article Hit count: 148

Filed under:
|
|

I am working on a java program that is essentially a chat room. This is an assignment for class so no code please, I am just having some issues determining the most feasible way to handle what I need to do. I have a server program already setup for a single client using threads to get the data input stream and a thread to handle sending on the data output stream. What I need to do now is create a new thread for each incoming request.

My thought is to create a linked list to contain either the client sockets, or possibly the thread. Where I am stumbling is figuring out how to handle sending the messages out to all the clients. If I have a thread for each incoming message how can I then turn around and send that out to each client socket.

I'm thinking that if I had a linkedlist of the clientsockets I could then traverse the list and send it out to each one, but then I would have to create a dataoutputstream each time. Could I create a linkedlist of dataoutputstreams? Sorry if it sounds like I'm rambling but I don't want to just start coding this, it could get messy without a good plan. Thanks!

© Stack Overflow or respective owner

Related posts about java

Related posts about threads