public class TCPSocketClient extends Object implements Runnable
TCPSocketServer
.DEFAULT_port
44444
.java ca.bc.webarts.tools.sockets.TCPSocketClient [tcpSocketServer] - portNumber is optionalIt reads messages directly from the System.in/commandline.
try { TCPSocketClient instance = new TCPSocketClient( "someHostName", 44844); String req = "ogg123 -z /mnt/snd/someTunesDir"; while ( !req.trim().equalsIgnoreCase("Bye") && !req.trim().equalsIgnoreCase("ttfn") && !req.trim().equalsIgnoreCase("Quit") && !req.trim().equalsIgnoreCase("Exit")) { try { if( //instance.isListening() && !req.trim().equals("")) { //req = inputLine.readLine().trim(); if (instance.debug_>1) System.out.println("Request: "+req); instance.initSocket(); if (instance.isInit()) { instance.sendAndWait(req,2000); instance.closeSocket(); } else System.err.println("Socket NOT init"); } } catch (IOException e) { System.err.println("IOException: " + e); e.printStackTrace(); } //Go Back for another message req = getAnotherMessage().trim(); } // loop back and send another msg } catch (UnknownHostException e) { System.err.println("Don't know about host " + instance.host_); } catch (IOException ioEx) { ioEx.printStackTrace(); System.err.println("Couldn't get I/O for the connection to the host " + instance.host_); }
Modifier and Type | Field and Description |
---|---|
protected Socket |
clientSocket_
The client socket.
|
protected int |
debug_
Simple multi-level debug flag (0-2).The higher the number, the higher the verbosity.
|
protected static String |
DEFAULT_SERVER_HOST |
protected boolean |
errorResponse_ |
protected String |
host_ |
protected DataInputStream |
is_
The input stream.
|
protected Thread |
listener_ |
protected boolean |
listening_ |
protected PrintStream |
os_
The output stream.
|
protected int |
port_
The port to send messages.
|
(package private) Vector<String> |
responseList_ |
protected boolean |
waitingForResponse_ |
Constructor and Description |
---|
TCPSocketClient()
Default constructor that uses default server and port.
|
TCPSocketClient(String hostName,
int portNum)
Constructor that uses sets a specified server and port.
|
Modifier and Type | Method and Description |
---|---|
void |
closeSocket()
Clean shutdown.
|
Vector<String> |
getResponseList()
Get Method for class field 'responseList_'.
|
Socket |
initSocket()
if NOT already initialized, it will instantiate a new socket, and associated in and out streams; Else it will return the existing.
|
boolean |
isInit() |
private boolean |
isListening()
Confirms if a listener Thread has started and is running/listening for a server response.
|
protected boolean |
isWaitngForResponse() |
boolean |
listenToSocket()
Starts the listener Thread, AFTER checking that everything is initialized and ready to go (initsocket).
|
static void |
main(String[] args) |
protected void |
printResponse(InputStream i)
Chunks the output from the passed inputStream into individual lines/Strings.
|
protected void |
printResponse(InputStream i,
OutputStream o) |
protected Vector<String> |
readResponse(InputStream i)
Chunks the output from the passed inputStream into individual lines/Strings.
|
void |
run() |
void |
send(String message)
Send the message and return as soon as it is sent (without any waiting for a response.
|
void |
sendAndWait(String message,
int maxBlockTime_ms)
Send the message to the TCPSocketServer AND wait for a response upto a maximum blocking time.
|
private boolean |
setListening() |
private boolean |
setListening(boolean l)
Only a new thread should call this.
|
void |
setResponseList(Vector<String> responseList)
Set Method for class field 'responseList_'.
|
protected boolean |
setWaitngForResponse() |
private boolean |
setWaitngForResponse(boolean l) |
void |
signalClose()
sends an interrupt message to the listener thread.
|
protected static String DEFAULT_SERVER_HOST
protected int debug_
protected Socket clientSocket_
protected PrintStream os_
protected DataInputStream is_
protected int port_
TCPSocketServer.DEFAULT_PORT
.protected boolean listening_
protected boolean waitingForResponse_
protected boolean errorResponse_
Vector<String> responseList_
public TCPSocketClient()
public TCPSocketClient(String hostName, int portNum)
public Socket initSocket() throws UnknownHostException, IOException
UnknownHostException
IOException
public boolean listenToSocket() throws IOException
IOException
public void signalClose()
public void closeSocket() throws IOException
initSocket
.IOException
private boolean setListening(boolean l)
private boolean setListening()
private boolean isListening()
private boolean setWaitngForResponse(boolean l)
protected boolean setWaitngForResponse()
protected boolean isWaitngForResponse()
public void setResponseList(Vector<String> responseList)
responseList
- is the value to set this class field to.public Vector<String> getResponseList()
public boolean isInit()
public void send(String message) throws IOException
IOException
public void sendAndWait(String message, int maxBlockTime_ms) throws IOException
IOException
protected void printResponse(InputStream i)
protected void printResponse(InputStream i, OutputStream o)
protected Vector<String> readResponse(InputStream i)
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses