Networking Lab
Goals
Your primary goal for this lab is to write a simple socket-based client to connect to a given server.
Getting started
First, download the networking.tar file to get the code for the lab. Make sure you are on mantis.
wget https://anyaevostinar.github.io/classes/208-f25/networking.tar
Copy it into a folder for today’s lab, and then un-tar the file:
tar xvf networking.tar
You should find the following files in a folder sockets_lab:
client.c: The client code you’ll be modifying.common.h: Some useful#defines used by both the client and the server.server.c: The code for the server you’ll be connecting to.
Part 1: Read through what you’ve got to work with
Take a look through server.c, trying to get a feel for how it sets up its end of the socket(s) to accept connection(s).
Then, look at client.c. There are a bunch of // TODOs for you to fill in.
Think back on what we talked about today – how will the client and server interact? Can multiple users interact with the server at the same time?
Part 2: Implementing a socket-based client
Now you’ll actually implement your client. Again, make sure you’re on mantis, or this will be less exciting (by some definitions of exciting :) ). Try compiling your code after each step below, and make sure to run it once you think it works.
-
Find the comment
// 1) TODOinclient.c. You need to add code to callsocket()to create a UDP socket. Take a look atserver.cfor an example. This page might also be helpful. -
Find the comment
// 2) TODOinclient.c. Now, you need to useconnect()to connect to the server using theservaddrinfo. Don’t forget to check for errors! This page might be helpful.Note: This page might also be really helpful in figuring out how to use
connect(). We aren’t usinggetaddrinfohere, but you can get away with casting astruct sockaddr_in *asstruct sockadd *. You also might find it handy to compare to thebind()call inserver.c. -
Find the comment
// 3) TODOinclient.c. Add a call tosend()the message. This should be analagous to therecvcall inclient.c, but you don’t need the loop. You should store the return value in a variable namedsent_bytes. -
Un-comment the line after
// 4) TODOto print out the number of bytes sent.
Try running it – check the screen up front to see if the server receives your message!
Part 3: Next steps
Once you have all that, play around some more!
-
Find the
// 5) TODOcomment and maybe prompt the user to type a message to send (read it withscanf()orfgets()(but NOTgets), or maybe take the message as a command-line argument. -
Maybe try setting up a loop to continually prompt the user to type new messages, sending each one.