CSC 325 Operating Systems with an Emphasis on UNIX
Assignment 11

This assignment will continue with socket programming in the Linux environment.

Programming with Sockets

As in the last lab, you will need both the header file, SocketClass.h and the header implementaton SocketClass.cxx as well as the main implementation, socketmain.cpp, .

Here is the sample program we saw before that uses the Socket interface. It is a "Hello World" program using sockets.

#include <iostream>
#include "SocketClass.h"

//These must be set in SocketClass.h
#define LINUX 		//define our OS as linux
#define CONCURRENT	//use the concurrent server model

using namespace std;

int main(int argc, char *argv[])
{
    SocketClass MyServer;	//new instance of socketclass
    int mysocket, theirsocket;  //store the ID of our socket and theirs
    				// (so we know where to write to)

    //We define our server to use the defaults in the class
    //  the socket is bound to all address
    //  and is listening on port DEFAULT_PORT

    mysocket = MyServer.SocketAlloc();				//Allocate the socket
    theirsocket = MyServer.ServerOpenSocket(mysocket);		//Open the socket, does not return until a client connects
    MyServer.setSendBuffer(theirsocket, "Hello World\n");	//Fill the buffer with the data to send
    MyServer.SendData(theirsocket);				//Send the data
    MyServer.RecvData(theirsocket, 10);				//Receive their 10 character response
    cout << MyServer.getRecvBuffer(theirsocket);		//print their response
    MyServer.CloseSocket();					//close our socket

    return 0;       //~SocketClass destructor is called automatically and makes sure all is in order
}

It can also be downloaded from sockethello.cpp.

 

Unique Port

Again, you need to change the port that the program listens on, as you will have LOTS of trouble you try to use the same one as someone else on the same Linux box. Type set to see your user ID, which is listed as UID. It should be something like 5xy.Inside of socketclass.h change the DEFAULT_PORT to 10 times your UID, so it will be 5xy0. This will guarantee that each person is using a unique port for their individual server.Be sure to remember this UIDx10 number because you will need it later...

To terminate the server program. Type "Ctrl-C".

The parameters to main, int argc and char *argv[], contain the command line used to execute the program, including the name of the program (as called) as well as any parameters.

	int argc is the number of parameters
	char *argv[] is an array of C-strings that store these parameters

Your Task:

A popular Linux "toy program" is the fortune program. When run, the fortune program displays a single quote, message, or fortune to the terminal that called it. It is popularly used amongst Linux system administrators to display novel messages when a user logs in.

Your task is to create such a program. Instead of being run interactively, however, it should be utilize sockets so that users can connect to the "Fortune Server" whenever they choose to learn about their fortune. You will be submitting only your C++ code entitled, yourlastnameA11.cpp.

This program should:

You might be interested in checking the status of your server from another instance of putty by using ps or top.

Submit your C++ code, yourlastnameA11.cpp.