Click here to Skip to main content
11,496,146 members (801 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
See more: C Windows Win32 udp
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.
Posted 27-Mar-13 2:02am
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)

  Print Answers RSS
0 Dnyaneshwar@Pune 692
1 RyanDev 230
2 CHill60 163
3 Sascha Lefèvre 145
4 Sergey Alexandrovich Kryukov 124
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 28 Mar 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100