Programming Lab - Introduction to Sockets
Download and Compile Individually
Download and compile the following files:
Example:
Compile:
gcc tcp_client.c -o tcp_client
Run with:
./tcp_client
Be sure to stop any running servers or clients and recompile after making changes.
Lab Overview
We will use simple client/server applications to explore TCP and UDP socket programming:
- The client reads a line from standard input.
- It sends the line to the server over a socket.
- The server converts the line to uppercase.
- The server sends the modified line back to the client.
- The client prints the result to standard output.
TCP vs UDP Quick Reference
| Protocol | Connection | Reliability | Ordering | Use Case |
|---|---|---|---|---|
| TCP | Yes | Yes | Yes | Web, FTP, Email |
| UDP | No | No | No | DNS, VoIP, TFTP |
Questions
Answer the following in your lab report:
- Run
tcp_clientbefore startingtcp_server:- What happens?
- Why?
- Run
udp_clientbefore startingudp_server:- What happens?
- Why?
- UDP port mismatch:
- Run both programs with different port numbers.
- What happens?
- Why?
- TCP port mismatch:
- Run both programs with different port numbers.
- What happens?
- Why?
- UDP Buffer Test:
- Start the
udp_server. - Launch
udp_clientand send a message. - While the server is running, rerun
udp_clientand send a shorter message.- What happens?
- Why?
- Fix it. Include your code fix in your submission.
- Start the
- Send buffer limit (udp_client):
- Modify
send_msginudp_clientto allocate 5 bytes instead of 50. - Restart both client and server.
- Send a message longer than 5 bytes.
- What happens?
- Why?
- Modify
- Receive buffer limit (udp_client):
- Revert previous change and instead reduce
recv_msgallocation to 5 bytes. - Send a message longer than 5 bytes.
- What do you expect?
- What actually happened?
- Why?
- Revert previous change and instead reduce
Programming Tasks
1. Accept user input for IP and port
Update all programs to prompt the user for:
- Server IP address (in client programs)
- Server and client port numbers
Example:
Enter server IP: 127.0.0.1
Enter server port: 9000
2. Loop until QUIT
Update tcp_client and udp_client to:
- Continuously read input from the user
- Send the message to the server
- Exit the loop when the user types
QUIT
Note: Use standard input methods like
fgets()orscanf(). MatchQUITexactly (case-sensitive).
Extra Credit (Optional)
- Add multi-client support in
tcp_server. - Add logging features (client IP, timestamps, etc.)
- Add error recovery or retry logic in UDP.
- Feel free to experiment and go beyond the lab instructions! Surprise me!
Helpful References
Testing & Debugging Tools
- Netcat – tool for sending and receiving data
- Check open ports:
- TCP:
netstat -antop - UDP:
netstat -anoup
- TCP:
- Kill processes:
- By PID:
kill PID - By name:
pkill PATTERN
- By PID:
Grading
Submit your lab report, including answers to all questions, modified source code files and screenshots, via Marmoset by the scheduled due date listed in the syllabus.
