views:

206

answers:

3

hello everyone

well, i am developing a single server multiple-client program in java. My problem is can i use single stream for all the clients or do i have to create a seperate stream for each client?

please help thank you

+2  A: 

Typically you'd need a stream per client. In some cases you can get away with UDP and multicasting, but it doesn't sound like a great idea for a chat server.

Usually it's easy to get a stream per client with no extra work, because each client will connect to the server anyway, and a stream can easily be set up over that connection.

Jon Skeet
+1  A: 

Yes, you can but I think it would be harder.

If you're using java.net.ServerSocket then each client accepted through:

Socket client = server.accept();

Will have it's own stream so you don't have to do anything else.

Is there a real need for a single stream for all clients or is just something you think it would help.

For the later it could cause more problems than those is solve.

+1  A: 

Can you do it? Yes, as Jon Skeet said, you can use multicasting.

Should you do it? That depends on what you are using the streams for.

For most client server applications, you will need a stream per client to maintain independent communications. Of course, there are applications where using multicasting is the right approach, such as live video streaming. In such a case, you would not want to overwhelm your network while streaming the same data to multiple clients. Of course, even in this case there will typically be a single control channel of some sort between each client and server.

Robin