com.marringtons.net
Class ClientSocket

java.lang.Object
  extended bycom.marringtons.net.ClientSocket

public class ClientSocket
extends Object

A client TCP/IP Socket class for simplified basic network I/O.

 ClientSocket client = new ClientSocket( port);
 client.writeLine( "Command");
 String response = client.readLine();
 client.close();
 

Author:
Paul Marrington

Constructor Summary
ClientSocket(InetAddress inetAddress, int port)
          Open a client socket on the local server ready for reading and writing.
ClientSocket(int port)
          Open a client socket on the local server ready for reading and writing.
ClientSocket(String hostName, int port)
          Open a client socket on the local server ready for reading and writing.
 
Method Summary
 void close()
          Close a socket and release all resources.
 void flush()
          The data from a write or writeLine will not be sent unless flush() is called.
 BufferedReader getReader()
          Return the reader so that programs that want to get data directly can do so (i.e.
 boolean isAlive()
          See if the connection is still alive.
 String read(int characters)
          Synchronous read of an expected number of characters from the socket server.
 String readLine()
          Read a text line from the client socket.
 void write(String string)
          Send a string to the socket server
 void writeLine(String line)
          Send a line (including training linefeed) to the socket server.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClientSocket

public ClientSocket(int port)
             throws IOException
Open a client socket on the local server ready for reading and writing.

Parameters:
port - to connect to.
Throws:
IOException

ClientSocket

public ClientSocket(String hostName,
                    int port)
             throws IOException,
                    UnknownHostException
Open a client socket on the local server ready for reading and writing.

Parameters:
hostName - name or IP as a string
port - to connect to.
Throws:
IOException
UnknownHostException

ClientSocket

public ClientSocket(InetAddress inetAddress,
                    int port)
             throws IOException
Open a client socket on the local server ready for reading and writing.

Parameters:
inetAddress -
port - to connect to.
Throws:
IOException
Method Detail

close

public void close()
Close a socket and release all resources.


isAlive

public boolean isAlive()
See if the connection is still alive.

Returns:
boolean - true if socket is OK and streams are still open

read

public String read(int characters)
            throws IOException
Synchronous read of an expected number of characters from the socket server. If the header includes a "Content-length" field it will provide the size of the body.

Parameters:
characters - to read
Returns:
String
Throws:
IOException

readLine

public String readLine()
                throws IOException
Read a text line from the client socket.

Returns:
a string containing the line or null for end of stream
Throws:
IOException

write

public void write(String string)
           throws IOException
Send a string to the socket server

Parameters:
string - to write to the port.
Throws:
IOException

flush

public void flush()
The data from a write or writeLine will not be sent unless flush() is called. That way you can accumulate information through multiple calls.


writeLine

public void writeLine(String line)
               throws IOException
Send a line (including training linefeed) to the socket server.

Parameters:
line - to write to the port.
Throws:
IOException

getReader

public BufferedReader getReader()
Return the reader so that programs that want to get data directly can do so (i.e. Header).

Returns:
reader in use.


Copyright © 2005 Paul Marrington http://library.marringtons.com