examples.example2_one_client_OO
Class Server

java.lang.Object
  extended by examples.example2_one_client_OO.Server
All Implemented Interfaces:
Runnable

public class Server
extends Object
implements Runnable

 NetworkingExamples project: examples.example2_one_client_OO.
 
 A simple example of networking (Sockets) in which:
   -- A single Server and Client exchange information, one after the other.
   
 This is the Server code.
 
 This is the same example as examples.example1_one_client
 except structured in a more OO way.  In particular, this structure has 3 classes:
   -- Main: for starting the program.
   -- Server: for initiating and running the Server.
   -- Client: for initiating and running the Client.
 
 See examples.example1_one_client for:
   -- Exactly what information the Server and Client exchange in this demo.
   -- The 7 Key Statements that are all-you-need-to-know to do networking in Java.
 
 See examples.example3_one_client_OO_library for this same example
 but using the simple networking library in package simpleNetworking.
 

Author:
David Mutchler, based on the Java Tutorials on networking. May, 2009.

Constructor Summary
Server(int port)
           Constructs readers and writers for talking to the Client, as follows: 1.
 
Method Summary
(package private)  void close()
          Closes all resources that this Server uses: writer, reader, socket.
 void run()
           A simple example of communication between this Server and the Client.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Server

public Server(int port)
       throws Throwable
 Constructs readers and writers for talking to the Client, as follows:
 
 1. Constructs a ServerSocket.
 
 2. Use the ServerSocket to accept a connection from a Client
    (blocks -- i.e. waits -- until a Client offers to connect)
    (a better program would establish a timeout for connecting).
    
 3. In accepting a connection to the Client, it gets a Socket.
    Use that Socket to:
    a. Get an InputStream.  Then decorate the InputStream
       into a BufferedReader (for more efficient communication).
    b. Get an OutputStream.  Then decorate the OutputStream
       into a PrintWriter (for convenient ways to write messages).
       
 Finally, this constructor:
 4. Starts a Thread in which the Server repeatedly communicates with the
    Client (so that you can see a simple example of network communication).
 

Parameters:
port - Port to use for the ServerSocket that accepts a connection to the Client.
Throws:
Throwable - if any Exception or Error occurs.
Method Detail

run

public void run()
 A simple example of communication between this Server and the Client.
 
 It behaves as follows:
 
 Repeatedly, until the Client sends STOP or the user says to stop:
   -- Get a number from the Client.
   -- Display the number from the Client, and get a number from the user.
   -- Add these numbers and send the total to the Client.
   
 The Client can send whatever number it wishes at each iteration;
 in fact, the particular Client in this program takes the number
 that the Server sends it, chooses a random number between 0 and 10,
 and sends the sum back to this Server.
 
 If the user says to stop, the Server sends a STOP message to the Client
 so that the Client will stop too.
 

Specified by:
run in interface Runnable

close

void close()
Closes all resources that this Server uses: writer, reader, socket.