Class DatagramTxRx
java.lang.Object
cbsw.DatagramTxRx
A class that encapsulates a datagram (UDP) connection.
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) DatagramSocket(package private) InetAddress(package private) int(package private) ByteBuffer(package private) byte[](package private) ByteArrayInputStream(package private) ByteBuffer -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the UDP socket.Gets a ByteBuffer that wraps the receive buffer.Gets a ByteArrayInputStream that wraps the receive buffer.static voidAn example program that replies ACK_OK to any message.voidCreates a server UDP datagram socket.voidCreates a UDP datagram socket.voidrx(int timeout) Reads a message from the rx channel.voidtx(byte[] message) Writes a message to the tx channel.voidtx(ByteBuffer message) Writes a message to the tx channel.
-
Field Details
-
rxBuffer
byte[] rxBuffer -
rxBB
ByteBuffer rxBB -
rxStream
ByteArrayInputStream rxStream -
txBuffer
ByteBuffer txBuffer -
inetAddress
InetAddress inetAddress -
port
int port -
datagramSocket
DatagramSocket datagramSocket
-
-
Constructor Details
-
DatagramTxRx
public DatagramTxRx(byte[] ipAddress, int port, int bufferSize)
-
-
Method Details
-
getRxInputStream
Gets a ByteArrayInputStream that wraps the receive buffer.
- Returns:
- The ByteArrayInputStream
-
getRxByteBuffer
Gets a ByteBuffer that wraps the receive buffer.
- Returns:
- The ByteBuffer
-
rx
Reads a message from the rx channel.
- Parameters:
timeout- Socket timeout in milliseconds. 0 for no timeout.- Throws:
Exception- Catch-all other exceptions.SocketTimeoutException- Socket timeout.NotYetConnectedException- Socket not yet connected.
-
tx
Writes a message to the tx channel.
- Parameters:
message- The message byte array- Throws:
PortUnreachableException- Port unreachable.RuntimeException- Catch-all other exceptions.
-
tx
Writes a message to the tx channel.
- Parameters:
message- A ByteBuffer containing the message- Throws:
PortUnreachableException- Port unreachable.
-
makeServerSocket
public void makeServerSocket()Creates a server UDP datagram socket.
- Throws:
RuntimeException- Exception during open or bind.
-
makeSocket
public void makeSocket()Creates a UDP datagram socket.
-
close
public void close()Closes the UDP socket.
-
main
An example program that replies ACK_OK to any message.
Creates and reads a server socket. Responds with ACK_OK to all messages.
- Parameters:
args- Command-line parameters
-