Ayse
Ayse

Reputation: 2754

how does the server handles if the client goes down in udp client/server application

I wanted two functionalities to be implemented on my udp server application.

  1. Creating thread that continuously receives data coming from any client.

  2. Creating a thread that continuously sends data on server socket after specific time period and waits for reply from client (handshake mechanism). (I implemented this to make aure that whenever any client goes down, the client does not send back the data and server comes to know that client is down.)

I have created two Sokets on Server side for this purpose. First Socket is bind()ed to INADDR_ANY.

Second Socket is bind()ed to my Machines IP Address.

All sends and receives from client are done on first socket.

The Handshake mechanism to ensure that the client has not gone down is done on the second socket.

Is this approach correct? Please do let me know, so that I may proceed with my work. Thank you in advance.

Upvotes: 0

Views: 333

Answers (1)

FrankieTheKneeMan
FrankieTheKneeMan

Reputation: 6810

I mean, your solution will work, but I don't see a reason why you can't use slightly more in-band control mechanisms. Try this, with only one socket on the server:

List connections contains pairs (client, lastPacketTime)
Socket is a single UDP Socket

Thread 1 Loop:
    Get new UDP Datagram P From Socket
    If P.client in List: 
        update lastPacketTime
    Else:
        add P.client to list
    If P is not Ping datagram:
        Do other operations on P

Thread 2 Loop:
    For client in List with lastPacket time 3-10 Seconds ago:
        send ping request via Socket
    For client in List with lastPacket time > 10 Seconds ago:
        Mark client as down.
    Sleep for some time

And this on the client:

Loop:
    If I have data to send:
        Send it!
    Else if I have a ping request in my UDP Socket:
        Send Ping datagram
    Else
        Sleep for some time.

If you need a more reliable handshake than that, you might consider just opening a TCP connection to the Server alongside the UDP stuff. Then the server can use the TCP connection to check liveness. I have to be honest, the hair on the back of my neck stands up when people try to do connection oriented UDP. If you're worried about all the processing and updating of ping times, you can add a third thread:

List contains pairs (client, lastPacketTime)
Queue contains Datagrams with time updates.
Socket is a single UDP Socket

Thread 1 Loop:
    Get new UDP Datagram P from Socket
    Add P to Queue
    If P is not Ping datagram:
        Do other operations on P

Thread 2 Loop:
    Get new Datagram P from Queue
    If P.client in List: 
        update lastPacketTime
    Else:
        add P.client to list

Thread 3 Loop:
    For client in List with lastPacket time 3-10 Seconds ago:
        send ping request via Socket
    For client in List with lastPacket time > 10 Seconds ago:
        Mark client as down.
    Sleep for some time

Upvotes: 1

Related Questions