Class DatagramTxRx

java.lang.Object
cbsw.DatagramTxRx

public class DatagramTxRx extends Object

A class that encapsulates a datagram (UDP) connection.

  • Field Details

  • Constructor Details

    • DatagramTxRx

      public DatagramTxRx(byte[] ipAddress, int port, int bufferSize)
  • Method Details

    • getRxInputStream

      public ByteArrayInputStream getRxInputStream()

      Gets a ByteArrayInputStream that wraps the receive buffer.

      Returns:
      The ByteArrayInputStream
    • getRxByteBuffer

      public ByteBuffer getRxByteBuffer()

      Gets a ByteBuffer that wraps the receive buffer.

      Returns:
      The ByteBuffer
    • rx

      public void rx(int timeout) throws Exception, NotYetConnectedException, SocketTimeoutException

      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

      public void tx(byte[] message) throws PortUnreachableException

      Writes a message to the tx channel.

      Parameters:
      message - The message byte array
      Throws:
      PortUnreachableException - Port unreachable.
      RuntimeException - Catch-all other exceptions.
    • tx

      public void tx(ByteBuffer message) throws PortUnreachableException

      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

      public static void main(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