Programming Lab - Introduction to Sockets
Download and compile individually the following files:
Example:
- Compile with:
gcc tcp_client.c -o tcp_client
- Note the name after
-o
flag
- Note the name after
- Run with:
./tcp_client
We use the following simple client/server applications do demonstrate UDP and TCP socket programming.
- The client reads a line from its standard input and sends it to the server via its socket.
- The server receives the line through its socket and processes it.
- The server converts the line to uppercase.
- The server sends the modified line back to the client through its socket.
- The client receives the modified line via its socket and prints it to the standard output.
To ensure proper execution of each exercise, please remember to stop and recompile both the server and client before running. This helps to clear any previous configurations and ensures a clean environment for accurate results.
Please respond to the following questions:
- Suppose you run
tcp_client
before you runtcp_server
. Enter a value.- What happens?
- Why?
- Suppose you run
udp_client
before you runudp_server
. Enter a value.- What happens?
- Why?
- What occurs if different port numbers are used for the UDP client and UDP server? Please ensure that both programs are running before providing your response.
- What occurs if different port numbers are used for the TCP client and TCP server? Please ensure that both programs are running before providing your response.
- UDP Test Instructions
- Start the
udp_server
. - Launch the
udp_client
and send a message to the server. - While the
udp_server
is still running, rerun theudp_client
again, but this time send a shorter message.- What happens?
- Why?
- Fix it. Make sure to include your code fix as part of your submission.
- Modify the send_msg function in
udp_client
to allocate 5 bytes instead of 50. - Restart both the
udp_server
and the updatedudp_client
. Don’t forget to recompile theudp_client
before restarting! - Use the
udp_client
to send a message that exceeds 5 bytes in length.- What happens?
- Why?
- Revert your changes in
udp_client
and modify the allocation size of recv_msg to 5. After making this adjustment, rerun theudp_client
and send a message that exceeds 5 bytes in length.- What outcomes do you anticipate from this change?
- What was the actual outcome?
- Why?
- Start the
Programming Section:
- Update
tcp_client
,udp_client
,tcp_server
andudp_server
to accept the client and server port numbers as user input. - Modify
tcp_client
andudp_client
to accept the server IP Address as user input. - Implement functionality that allows both clients to continuously send messages until a QUIT command is entered.
Extra Credit:
- Enhance the
tcp_server
to support multiple clients. - Feel free to experiment with the code and surprise me! Don’t hesitate to explore and push boundaries—breaking things can lead to valuable learning experiences. Embrace the process!
References
Testing
- Netcat is a command line tool that connects to an arbitrary TCP or UDP port and allows you to send and receive data.
- To see what TCP ports are in use:
netstat -antop
, for UDP:netstat -anoup
- To kill a process:
kill PID
orpkill PATTERN
Grading
Post your report including source in Marmoset by the scheduled due date in the syllabus.