We going to write a simple fortune cookie server. That is, you need to write two pieces of software: a client that sends requests for a fortune cookie to display, and a server to provide the fortunes upon request. Note that you should pick a partner and work in pairs on this project.
There are a few critical requirements for the communication between the client and server:
The server and client should be invoked, respectively, like so:
remus:~> java FortuneServer 2468
romulus:~> java FortuneClient remus 2468
That is, the server should be provided a port number on which to listen. Similarly, the client should be provided both the server's hostname and the port number. Your group should randomly select a single port number between 10,000 and 65,636 and use it consistently. Be aware of the small possibility that two groups will pick the same value, and thus interfere with one another.
Your client and server interaction should follow a specific sequence of steps:
Communication via sockets requires use of some special Java classes.
InetAddress: This class handles Internet addressing. The method InetAddress.getByName() is likely to be of interest.
ServerSocket: Bind a socket to a port and listen for a connection to that port. Upon accepting that connection, get a Socket object with which to do the real communications.
Socket: Like a File object, an object through which network communications actually passes. From a socket object, the Socket.getInputStream() and Socket.getOutputStream() methods allow you to construct reader and writer objects (e.g. Scanner or BufferedReader for reading, and PrintWriter writing) to perform input and output through the socket as you would with a file.
To find out more about these classes, see the Java 6 API documentation. This page will allow you to browse these (and all of the other standard Java) classes, seeing all of their methods and descriptions of those methods.
Submit your FortuneClient.java, FortuneServer.java, and any other supporting source code like so:
cs29-submit project-2 *.java