UDP/IP Sockets Demo 3

Demo 3: Sending messages from a client to a server

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 and waits for the next message.

To run this program, download the demo file and unzip it to create the demo-udp-03 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:

gcc -o udp-send udp-send.c gcc -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 Sending packet 1 to 127.0.0.1 port 21234 Sending packet 2 to 127.0.0.1 port 21234 Sending packet 3 to 127.0.0.1 port 21234 Sending packet 4 to 127.0.0.1 port 21234

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

waiting on port 21234 received 16 bytes received message: "This is packet 0" waiting on port 21234 received 16 bytes received message: "This is packet 1" waiting on port 21234 received 16 bytes received message: "This is packet 2" waiting on port 21234 received 16 bytes received message: "This is packet 3" waiting on port 21234 received 16 bytes received message: "This is packet 4" waiting on port 21234