Class SocketTxRx
java.lang.Object
cbsw.SocketTxRx
A class that encapsulates a socket connection.
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final int(package private) ByteBuffer(package private) ReadableByteChannel(package private) ServerSocketChannel(package private) SocketChannel(package private) ByteBuffer(package private) WritableByteChannel -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaccept()Accepts a server socket connection and creates tx/rx channels.static voidAn example program that replies ACK_OK to any message.voidmakeServerSocket(URL url) Creates a server socket channel.voidmakeSocket(URL url) Creates a socket channel.rx(int timeout) Reads a message from the rx channel.voidrxFlush()Cleans out any pending messages.voidWrites a message to the tx channel.
-
Field Details
-
bufferSize
static final int bufferSize- See Also:
-
rxBuffer
ByteBuffer rxBuffer -
txBuffer
ByteBuffer txBuffer -
serverSocketChannel
ServerSocketChannel serverSocketChannel -
socketChannel
SocketChannel socketChannel -
rxChannel
ReadableByteChannel rxChannel -
txChannel
WritableByteChannel txChannel
-
-
Constructor Details
-
SocketTxRx
public SocketTxRx()
-
-
Method Details
-
rx
Reads a message from the rx channel.
- Parameters:
timeout- Socket timeout in milliseconds. 0 for no timeout.- Returns:
- The message string
-
tx
Writes a message to the tx channel.
- Parameters:
message- The message string
-
rxFlush
public void rxFlush()Cleans out any pending messages.
Reads socket until it times out indicating no more data.
-
makeServerSocket
Creates a server socket channel.
- Parameters:
url- A URL to pass the port number
-
makeSocket
Creates a socket channel.
- Parameters:
url- A URL to pass the internet address
-
accept
public void accept()Accepts a server socket connection and creates tx/rx channels.
-
main
An example program that replies ACK_OK to any message.
Creates and listens on a server socket. When a connection is made, responds with ACK_OK to all messages.
When the connection is broken, waits for a new connection.
- Parameters:
args- Command-line parameters
-