Stop and Wait Socket Programming with Udp

Stop and Wait Socket Programming with Udp

Looking to make a Java stop-and-wait UDP server and client but I'm running into some problems starting off. I've made a simple UDP client and server without the stop-and-wait part, but I would now like to learn how to change it. How can I send ACKs and implement timeouts using java sockets ?

Could someone please post up some examples for me to use in my implementation ?

8

1 Answer

If you're implementing this in UDP, sending and receiving acknowledgements is up to you. This seems to be what you want for this stop and wait protocol. In terms of pseudocode, you would want something like:

int Send(msg)
{
    char rcvBuf[];
    sentBytes = sock.send(msg);
    sock.rcv(rcvBuf);
    return sentBytes;
}

int Recv(rcvBuf)
{
    String ackMsg = "ACK";
    length = sock.rcv(rcvBuf);
    sock.send(ackMsg);
    return length;
}

After every send, you wait for an acknowledgement message to come in, and every time you receive, you send an acknowledgement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.