Operating Systems Spring 2005 Sample FIFO and socket code. The programs here illustrate the use of a named pipe (FIFO), tcp and udp sockets. The fifo and TCP programs a very similar (both support stream based communication). Both programs follow a client/server model: server establishes a communication endpoint and waits for a client to connect. client connects to an established communication endpoint and sends some stuff. The actual code that handles the fifo and tcp communication is in the file common.c: the server function reads from a descriptor, each time it reads it sends whatever it got to stdout. The function continues until it detects and EOF condition (indicates that the client has closed it's end of the connection). the client function reads from stdin (using readline), and sends whatever it gets from the user to the server. If the input is the line "quit", the client does not send this string but instead closed it's end of the connection and quits. The idea is to point out that the same code works without knowing whether it is dealing with a fifo or TCP socket. The UDP socket is handled differently. It is possible to use read and write with UDP sockets, although things must be set up a little special (beyond the scope of what we are covering). ========== fifoplay.c ========================================== The file fifoplay.c contains the code that uses a named pipe to support interprocess communication. The program checks it's command line arguments to decide whether it should be a client or a server (there is just one program, but it can act as either a client or a server). If running as a server, fifoplay first attempts to create a named pipe (the pathname is provided on the command line). If this works, the fifo server calls the server() function in common.c, which handles the actual communication. The server will shut down after a single client connection has terminated. If running as a client, fifoplay will first attempt to open a named pipe (the pathname is provided on the command line). If this works, the fifo client calls the client() function in common.c. ========== tcpplay.c ========================================== The file tcpplay.c contains the code that uses a tcp socket to support interprocess communication. The program checks it's command line arguments to decide whether it should be a client or a server (there is just one program, but it can act as either a client or a server). If running as a server, tcpplay first attempts to create a passive mode tcp socket on the port number specified on the command line. If this works, the tcp server waits for a connection, once a connection is established (some client has called connect()) the tcp server calls the server function in common.c, giving it the active tcp socket descriptor. The tcp server will handle multiple clients, each time one client is done the server will look for a new connection. You must use ^C to kill the tcp server. If running as a client, tcpplay will first attempt to create a tcp socket and connect to the server (the server hostname and port must be provided on the command line). If this works, the client tcp client calls the client() function in common.c (and then quits). ========== udpplay.c ========================================== The file udpplay.c contains code that uses udp sockets to support interprocess communication. The program checks it's command line arguments to decide whether it should be a client or server (there is just one program, but it can act as either a client or a server). If running as a server, udpplay first attempts to create a udp socket and bind it to the port number specified on the command line. If this works, the udp server waits for incoming datagrams, for each one received it will send whatever is received to stdout. You must use ^C to kill the udp server. If running as a client, udpplay will first attempt to create a udp socket. Then a sockaddr_in is created with the server's address (the server hostname and port must be provided on the command line). The client will then use readline to read from stdin (one line at a time), and send each line to the server. If the client reads the string "quit" from the user (or stdin is closed), the client program quits. =================================================================== The Makefile included builds all three programs (just type "make" and it will build all three programs). If you want to build these manually you need to include the readline support when compiling on the BSD machines (required by common.c and udpplay.c).