simpleNetworking
Class Client

java.lang.Object
  extended by simpleNetworking.Client

public class Client
extends Object

 A simple Client that you can use in your programs.
 
 It has methods for:
   -- reading from the Server
   -- writing to the Server
   -- closing its resources
 
 It does a minimal-security validation protocol with the Server
 (who must use the Server's side of that protocol).
 

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

Constructor Summary
Client(String serverHostName)
          Connects to the given Server, getting a Socket, and prepares for reading/writer to the Server based on that Socket.
 
Method Summary
 void close()
          Closes the resources associated with this Client.
(package private)  void finishUpFailedConstruction(Throwable exception, String serverHostName, String errorMessage)
          Something went wrong in constructing this Client.
 String readLine()
          Returns the next line available from the Server, stripping the terminating newline.
protected  boolean validate()
          To validate a proposed Client: -- This Client sends the (announced) password to the Server.
 void writeLine(String stringToWrite)
          Sends the given String, appending a newline to it, to the Server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Client

public Client(String serverHostName)
       throws SocketException
Connects to the given Server, getting a Socket, and prepares for reading/writer to the Server based on that Socket. Also does a minimal-security validation.

Parameters:
serverHostName - Name (e.g. "mutchler-3.rose-hulman.edu") or IP address (e.g. "137.112.250.33") of the Server to which you want to connect.
Throws:
SocketException
Method Detail

readLine

public String readLine()
                throws IOException
Returns the next line available from the Server, stripping the terminating newline. Blocks (waits) if no line is available yet.

Returns:
the next line available from the Server, but with the terminating newline stripped.
Throws:
IOException - if an IO error occurs while reading.

writeLine

public void writeLine(String stringToWrite)
               throws IOException
Sends the given String, appending a newline to it, to the Server.

Parameters:
stringToWrite - String to send (with a newline appended) to the Server.
Throws:
IOException - if an error occurs while writing.

close

public void close()
Closes the resources associated with this Client.


validate

protected boolean validate()
To validate a proposed Client: -- This Client sends the (announced) password to the Server. -- The Server should receive the password and check whether it is OK. -- If it is not OK, the Server should not accept the attempted connection. -- The Server should echo back the sent password. This method is the Client side of the above. Subclasses can override this, simply returning 'true' for no validation or doing their own validation protocol with their Server.

Returns:
true if the validation succeeded

finishUpFailedConstruction

void finishUpFailedConstruction(Throwable exception,
                                String serverHostName,
                                String errorMessage)
                          throws SocketException
Something went wrong in constructing this Client. Close the Client, print an error message, and throw a SocketException.

Parameters:
exception - that was thrown by the failed construction of this Client.
errorMessage - to display in a message dialog.
serverHostName - to which this Client tried to connect.
Throws:
SocketException - so that the caller knows that construction failed.