Package cbsw
Class DatagramTxRx
- java.lang.Object
-
- cbsw.DatagramTxRx
-
public class DatagramTxRx extends java.lang.ObjectA class that encapsulates a datagram (UDP) connection.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) java.net.DatagramSocketdatagramSocket(package private) java.net.InetAddressinetAddress(package private) intport(package private) java.nio.ByteBufferrxBB(package private) byte[]rxBuffer(package private) java.io.ByteArrayInputStreamrxStream(package private) java.nio.ByteBuffertxBuffer
-
Constructor Summary
Constructors Constructor Description DatagramTxRx(byte[] ipAddress, int port, int bufferSize)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the UDP socket.java.nio.ByteBuffergetRxByteBuffer()Gets a ByteBuffer that wraps the receive buffer.java.io.ByteArrayInputStreamgetRxInputStream()Gets a ByteArrayInputStream that wraps the receive buffer.static voidmain(java.lang.String[] args)An example program that replies ACK_OK to any message.voidmakeServerSocket()Creates a server UDP datagram socket.voidmakeSocket()Creates 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(java.nio.ByteBuffer message)Writes a message to the tx channel.
-
-
-
Method Detail
-
getRxInputStream
public java.io.ByteArrayInputStream getRxInputStream()
Gets a ByteArrayInputStream that wraps the receive buffer.
- Returns:
- The ByteArrayInputStream
-
getRxByteBuffer
public java.nio.ByteBuffer getRxByteBuffer()
Gets a ByteBuffer that wraps the receive buffer.
- Returns:
- The ByteBuffer
-
rx
public void rx(int timeout) throws java.lang.Exception, java.nio.channels.NotYetConnectedException, java.net.SocketTimeoutExceptionReads a message from the rx channel.
- Parameters:
timeout- Socket timeout in milliseconds. 0 for no timeout.- Throws:
java.lang.Exception- Catch-all other exceptions.java.net.SocketTimeoutException- Socket timeout.java.nio.channels.NotYetConnectedException- Socket not yet connected.
-
tx
public void tx(byte[] message) throws java.net.PortUnreachableExceptionWrites a message to the tx channel.
- Parameters:
message- The message byte array- Throws:
java.net.PortUnreachableException- Port unreachable.java.lang.RuntimeException- Catch-all other exceptions.
-
tx
public void tx(java.nio.ByteBuffer message) throws java.net.PortUnreachableExceptionWrites a message to the tx channel.
- Parameters:
message- A ByteBuffer containing the message- Throws:
java.net.PortUnreachableException- Port unreachable.
-
makeServerSocket
public void makeServerSocket()
Creates a server UDP datagram socket.
- Throws:
java.lang.RuntimeException- Exception during open or bind.
-
makeSocket
public void makeSocket()
Creates a UDP datagram socket.
-
close
public void close()
Closes the UDP socket.
-
main
public static void main(java.lang.String[] args)
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
-
-