Package cbsw
Class SocketTxRx
- java.lang.Object
-
- cbsw.SocketTxRx
-
public class SocketTxRx extends java.lang.Object
A class that encapsulates a socket connection.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static int
bufferSize
(package private) java.nio.ByteBuffer
rxBuffer
(package private) java.nio.channels.ReadableByteChannel
rxChannel
(package private) java.nio.channels.ServerSocketChannel
serverSocketChannel
(package private) java.nio.channels.SocketChannel
socketChannel
(package private) java.nio.ByteBuffer
txBuffer
(package private) java.nio.channels.WritableByteChannel
txChannel
-
Constructor Summary
Constructors Constructor Description SocketTxRx()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
accept()
Accepts a server socket connection and creates tx/rx channels.static void
main(java.lang.String[] args)
An example program that replies ACK_OK to any message.void
makeServerSocket(java.net.URL url)
Creates a server socket channel.void
makeSocket(java.net.URL url)
Creates a socket channel.java.lang.String
rx(int timeout)
Reads a message from the rx channel.void
rxFlush()
Cleans out any pending messages.void
tx(java.lang.String message)
Writes a message to the tx channel.
-
-
-
Field Detail
-
bufferSize
static final int bufferSize
- See Also:
- Constant Field Values
-
rxBuffer
java.nio.ByteBuffer rxBuffer
-
txBuffer
java.nio.ByteBuffer txBuffer
-
serverSocketChannel
java.nio.channels.ServerSocketChannel serverSocketChannel
-
socketChannel
java.nio.channels.SocketChannel socketChannel
-
rxChannel
java.nio.channels.ReadableByteChannel rxChannel
-
txChannel
java.nio.channels.WritableByteChannel txChannel
-
-
Method Detail
-
rx
public java.lang.String rx(int timeout)
Reads a message from the rx channel.
- Parameters:
timeout
- Socket timeout in milliseconds. 0 for no timeout.- Returns:
- The message string
-
tx
public void tx(java.lang.String message)
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
public void makeServerSocket(java.net.URL url)
Creates a server socket channel.
- Parameters:
url
- A URL to pass the port number
-
makeSocket
public void makeSocket(java.net.URL url)
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
public static void main(java.lang.String[] args)
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
-
-