Package com.metratec.lib.connection
Class TcpConnection
- java.lang.Object
-
- com.metratec.lib.connection.ICommConnection
-
- com.metratec.lib.connection.AbstractTcpConnection
-
- com.metratec.lib.connection.TcpConnection
-
public class TcpConnection extends AbstractTcpConnection
A connection to a tcp port- Author:
- man
-
-
Field Summary
-
Fields inherited from class com.metratec.lib.connection.ICommConnection
CONNECTION_LOST, DEVICE_IN_USE, ETHERNET_TIMEOUT, ETHERNET_UNKNOWN_HOST, NO_DEVICES_FOUND, NO_LIBRARY_FOUND, NOT_AVAILABLE, NOT_INITIALISED, RECV_TIMEOUT, SERIAL_NO_ACCESS, SERIAL_NOT_INITIALISED, SERIAL_PARAMETER_NOT_SET, SERIAL_PORT_NOT_EXIST, SET_CONFIGURATION, UNHANDLED_ERROR, USB_SET_BAUDRATE, USB_SET_DATA_CHARACTERISTICS, USB_SET_FLOWCONTROL, USB_SET_TIMEOUTS, USER_ERRORCODE_01, USER_ERRORCODE_02, USER_ERRORCODE_03, USER_ERRORCODE_04, USER_ERRORCODE_05, WRONG_PARAMETER
-
-
Constructor Summary
Constructors Constructor Description TcpConnection(java.lang.String ip, int port)
Construct a new TCPIPConnection class, with the given parameters.TcpConnection(java.net.Socket socket)
TcpConnection(java.net.Socket socket, boolean isServerConnection)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.StringBuilder
receive(int... terminators)
Receives data from the connected device until one of the terminator signs is found.void
recv(byte[] b, int off, int len)
Receives until buffer is filled or timeout occurrs.void
send(java.lang.String senddata)
Sends data to the connected devicejava.lang.String
toString()
-
Methods inherited from class com.metratec.lib.connection.AbstractTcpConnection
connect, dataAvailable, disconnect, getConnectionTimeout, getInfo, getInputStream, getIPAddress, getOutputStream, getPort, getRecvTimeout, getSocket, isAlive, isAlive, isConnected, isServerConnection, recv, send, setConnectionTimeout, setIPAddress, setPort, setRecvTimeout, setSettings
-
Methods inherited from class com.metratec.lib.connection.ICommConnection
recv, recv
-
-
-
-
Constructor Detail
-
TcpConnection
public TcpConnection(java.lang.String ip, int port)
Construct a new TCPIPConnection class, with the given parameters.- Parameters:
ip
- Device IP addressport
- Device port
-
TcpConnection
public TcpConnection(java.net.Socket socket) throws CommConnectionException
- Parameters:
socket
-Socket
- Throws:
CommConnectionException
- throwed with error codeICommConnection.NOT_INITIALISED
if the given socket is not connected
-
TcpConnection
public TcpConnection(java.net.Socket socket, boolean isServerConnection) throws CommConnectionException
- Parameters:
socket
- the new socketisServerConnection
- set to true if the connection is created by accepting a client - so no reconnect is available- Throws:
CommConnectionException
- throwed with error codeICommConnection.NOT_INITIALISED
if the given socket is not connected
-
-
Method Detail
-
recv
public void recv(byte[] b, int off, int len) throws CommConnectionException
Description copied from class:ICommConnection
Receives until buffer is filled or timeout occurrs.- Overrides:
recv
in classICommConnection
- Parameters:
b
- Byte array to filloff
- Offset into array (it is filled beginning with the offset)len
- Number of bytes to read.- Throws:
CommConnectionException
- possible Errorcodes:- for TCP Connection:
- CONNECTION_LOST
- RECV_TIMEOUT
- UNHANDLED_ERROR
- NOT_INITIALISE
- for USB Connection:
- CONNECTION_LOST
- RECV_TIMEOUT
- UNHANDLED_ERROR
- NOT_INITIALISE
- for RS232 Connection:
- CONNECTION_LOST
- RECV_TIMEOUT
- UNHANDLED_ERROR
- NOT_INITIALISE
- for TCP Connection:
-
send
public void send(java.lang.String senddata) throws CommConnectionException
Description copied from class:ICommConnection
Sends data to the connected device- Overrides:
send
in classICommConnection
- Parameters:
senddata
- data/command send to the the connected device- Throws:
CommConnectionException
- possible Errorcodes:- for TCP Connection:
- WRONG_PARAMETER
- UNHANDLED_ERROR
- NOT_INITIALISE
- for USB Connection:
- WRONG_PARAMETER
- UNHANDLED_ERROR
- NOT_INITIALISE
- for RS232 Connection:
- WRONG_PARAMETER
- CONNECTION_LOST
- UNHANDLED_ERROR
- NOT_INITIALISE
- for TCP Connection:
-
receive
public java.lang.StringBuilder receive(int... terminators) throws CommConnectionException
Description copied from class:ICommConnection
Receives data from the connected device until one of the terminator signs is found. The result is returned as a StringBuilder for performance reasons.- Overrides:
receive
in classICommConnection
- Parameters:
terminators
- A list of terminator signs. Note that this may be a list of parameters or an array.- Returns:
- a StringBuilder object, including the termination sign
- Throws:
CommConnectionException
- possible Errorcodes:- for TCP Connection:
- CONNECTION_LOST
- RECV_TIMEOUT
- UNHANDLED_ERROR
- NOT_INITIALISE
- for USB Connection:
- CONNECTION_LOST
- RECV_TIMEOUT
- UNHANDLED_ERROR
- NOT_INITIALISE
- for RS232 Connection:
- CONNECTION_LOST
- RECV_TIMEOUT
- UNHANDLED_ERROR
- NOT_INITIALISE
- for TCP Connection:
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-