UDP/IP Sockets Demo 4

Demo 4: Sending messages and response messages

This sample program demonstrates how a client sends a bunch of messages to a server and how the server listens for them. The server loops on recvfrom, waiting for messages. When it gets a message, it prints it to the standard output, sends an acknowledgement message back to the sender, and waits for the next message.

To run this program, download the demo file and unzip it to create the demo-udp-04 directory. If you are using a shared machine, you may want to change the port number defined in port.h. If you want to run the server on a different machine from the client, you will need to change the IP address that is defined in the client (udp-send.c). Then compile the file by running make or manually with:

cc -o udp-send udp-send.c cc -o udp-recv udp-recv.c

Then run the server in one window:

./udp-recv

This is the server and you will see a message along the lines of:

waiting on port 21234

Run the client in another window:

./udp-send

You should immediately see a stream of messages:

Sending packet 0 to 127.0.0.1 port 21234 received message: "ack 0" Sending packet 1 to 127.0.0.1 port 21234 received message: "ack 1" Sending packet 2 to 127.0.0.1 port 21234 received message: "ack 2" Sending packet 3 to 127.0.0.1 port 21234 received message: "ack 3" Sending packet 4 to 127.0.0.1 port 21234 received message: "ack 4"

On the client, you should see messages indicating that packets were received:

waiting on port 21234 received message: "This is packet 0" (16 bytes) sending response "ack 0" waiting on port 21234 received message: "This is packet 1" (16 bytes) sending response "ack 1" waiting on port 21234 received message: "This is packet 2" (16 bytes) sending response "ack 2" waiting on port 21234 received message: "This is packet 3" (16 bytes) sending response "ack 3" waiting on port 21234 received message: "This is packet 4" (16 bytes) sending response "ack 4" waiting on port 21234