
CS350 Computer System Concepts Lab 5
Sockets
For interprocess communication between unrelated processes, and processes on different machines
of a network, sockets should be used. Unfortunately, sockets are complicated. This lab is intended
to be more informative than requiring you write programs. Rather than duplicate socket tutorial
information, a tutorial obtained from the internet is provided:
http://www.silicontao.com/ProgrammingGuide/other/beejnet.pdf
See also: http://shoe.bocks.com/net/
http://http://www.ecst.csuchico.edu/~chafey/prog/sockets/sinfo1.html
The style is quite informal, but most of what you need to know about sockets is in there. Read the
tutorial and compile and run the sample programs, noting the comments below.
1. The getip program must be compiled with the nsl library
(e.g. gcc -o getip getip.c -lnsl). Unfortunately, Solaris doesn’t have the herror function,
so replace herror with printf. Try it out with getip whitehouse.gov. I get a different IP
address (63.161.169.137) than the tutorial.
The getip program is a very much stripped down version of the nslookup program.
2. The client and server programs must be compiled with the socket and nsl libraries (e.g.
gcc -o client client.c -lnsl).
•Change the MYPORT macro to a different port. If everyone tries to use the same port on
the same machine chaos will result. Don’t use any port specified in the file /etc/services
though.
•The server and client can be run on different machines.
•Create a new client and server (based on the old ones) where the client is the same as the
old one, except that it takes an extra command line argument, and sends that string to the
server. The new server receives the string and converts it to upper case (use toupper on
each char), and sends it back to the client. The client then prints out the string it received.
•Web browsers (like Netscape) are just fancy clients to web servers (like Apache). The WWW
is a TCP/IP (or AF_INET,SOCK_STREAM from the socket’s point of view) based protocol like
the client and server example here. You can use the telnet program (another TCP/IP
based program) to do web browsing. For example, type in telnet shell 80 and press
return. Once connected, type GET / (G - E - T - space - slash). The telnet program
sends this to port 80 on shell. The web server on shell should respond with the CSEE
home page1in raw HTML.
•You can use the client and server programs as a starting point for your assignment.
3. The talker and listener programs must also be compiled with the nsl library.
4. The select program needs no additional libraries. In fact, the program doesn’t use sockets at
all. The select call works for most types of file descriptors, including sockets. Make sure you
get the correct version of select by typing ./select in the shell, otherwise you may get the
shell select command.
Why does the shell get the input that is typed in while the select program is running?
1You might not see all of it if the connection was closed early. To prevent this, redirect the output of the telnet
command to a file. (e.g. telnet shell 80 > afile)
1