Click here to Skip to main content
15,074,394 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

I did this many years ago in VB6 but as I recall an exception was thrown if they could not contact the machine name. However, if the machine is online but your app is not listening then what I did was implement a polling situation so if I did not get a response back in a certain amount of time it was assumed offline. Your other option is to use TCP.
This depends on the used communication.

When the other side sends packets in regular intervals, you can stop sending packets when no data are received for a specific time and resume sending upon the next receive. If no data are send in regular intervals, just send them. Such packets are called keep-alive packets. If necessary, send a packet at program start to indicate that the client / server is present and trigger the other side to start sending.

Using the above, you will have packets send until the time out occurs. But this is normally not a problem. If your communication protocol can be changed, you might send additional special packets indicating that a server or client is going down so that the other side can stop immediately.
You can set up your own handshake protocol on top of UDP. In other words, have the receiving machine send a response that the sender will process.
A simple implementation is as follows:

Have a background thread keep sending those messages and waiting for replies.
Upon receiving replies, you can populate some sort of data structure or a file with a list of alive devices.
Your other main thread (or threads) can have the following changes:

Before sending any data, check if the client you're going to send to is present in that file/data structure.
If not, skip this client.
Repeat the above for all remaining clients in the populated file/data structure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900