Reputation: 2754
I am developing a windows application for Client Server communication using UDP, but since UDP is connectionless, whenever a Client goes down, the Server does not know that Client is off and keeps sending the data. Similar is the case when a Server is down. How can I cater this condition that whenever any of the Client or Server is down, the other party must know it and can handle it. Waiting for reply.
Upvotes: 1
Views: 1442
Reputation: 2351
What you are asking is beyond the scope of UDP. You'd need to implement your own protocol, over UDP, to achieve this.
One simple idea could be to periodically send keepalive messages (TCP on the other hand has this feature).
You can have a simple implementation as follows:
Your other main thread (or threads) can have the following changes:
One problem I can see in the above implementation is analogous to the RAW hazard from the main thread's perspective.
Use the following analogy instead of the mentioned example for the RAW hazard,
i1
= Your background thread which sends the keepalive messages.i2
= Your main thread (or threads) which send/receive data and do your other tasks.i2
tries to read the data structure/file which is populated by i1
before i1
has updated it.i2
will not get the updated list and it can miss out a few clients this way.i1
will signal i2
when it completes any-ongoing writing. You just need to send a very lightweight message (usually has no data. Just the header information). Make sure this message is unique. You do not want another message being interpreted as a keepalive message.
You can send this message using a sendto()
call to a broadcast address. After you finish sending, wait for replies for a certain timeout using recv()
.
Log every reply in a data structure/file. After the timeout expires, have the thread go to sleep for some time. When that time expires, repeat the above process.
To help you get started writing good, robust networking code, please go through Beej's Guide to Network Programming. It is absolutely wonderful. It explains many concepts.
Upvotes: 4