Haifeng Zhang
Haifeng Zhang

Reputation: 31905

What if one client is lost connection, how to send data from server to other alive clients

I have one server and 3 clients (TCP connection) using executorservice.

I'm trying send data from server S to C1,C2,and C3.Total data are 3000 lines.

If all 3 clients are alive, those 3 clients can get 3000 lines in total.

Now my problem is if 1 client is dead(lost connection), how to make the other two clients get

all the rest data?

For example, C1 has received 200 lines, I shut it down. how to make C2+C3 receive 2800 lines?

Upvotes: 0

Views: 154

Answers (1)

Brian White
Brian White

Reputation: 8756

All TCP sessions are independent so you simply need to write the remaining 2800 lines to the remaining two sockets.

Using select(...) will tell you when connections can accept more data, have data to read, or have closed (ready to read but 0 bytes available).

Once "select" tells you a connection has closed, simply remove it from the list of polled file descriptors and continue to write to the others. Each will need its own state about how many bytes you have written to it because they may flow at different rates.

Upvotes: 0

Related Questions