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:


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:

  1. Run tcp_client before starting tcp_server:
    • What happens?
    • Why?
  2. Run udp_client before starting udp_server:
    • What happens?
    • Why?
  3. UDP port mismatch:
    • Run both programs with different port numbers.
    • What happens?
    • Why?
  4. TCP port mismatch:
    • Run both programs with different port numbers.
    • What happens?
    • Why?
  5. UDP Buffer Test:
    • Start the udp_server.
    • Launch udp_client and send a message.
    • While the server is running, rerun udp_client and send a shorter message.
      • What happens?
      • Why?
      • Fix it. Include your code fix in your submission.
  6. Send buffer limit (udp_client):
    • Modify send_msg in udp_client to allocate 5 bytes instead of 50.
    • Restart both client and server.
    • Send a message longer than 5 bytes.
      • What happens?
      • Why?
  7. Receive buffer limit (udp_client):
    • Revert previous change and instead reduce recv_msg allocation to 5 bytes.
    • Send a message longer than 5 bytes.
      • What do you expect?
      • What actually happened?
      • Why?

Programming Tasks

1. Accept user input for IP and port

Update all programs to prompt the user for:

Example:

Enter server IP: 127.0.0.1
Enter server port: 9000

2. Loop until QUIT

Update tcp_client and udp_client to:

Note: Use standard input methods like fgets() or scanf(). Match QUIT exactly (case-sensitive).


Extra Credit (Optional)


Helpful References


Testing & Debugging Tools


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.