Class UnreliableSocket


  • public class UnreliableSocket
    extends java.lang.Object
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int MAX_PACKET_SIZE  
    • Constructor Summary

      Constructors 
      Constructor Description
      UnreliableSocket​(java.net.DatagramSocket socket)
      Creates a new UnreliableSocket with the given socket as the underlying socket.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes the underlying socket.
      void connect​(java.net.InetAddress address, int port)
      "Connects" (Remember, UDP is a connection-less protocol) the socket to another socket.
      java.io.InputStream getInputStream()  
      java.io.OutputStream getOutputStream()  
      java.net.InetAddress getRemoteAddress()  
      int getRemotePort()  
      java.net.DatagramSocket getSocket()  
      void setRemoteAddress​(java.net.InetAddress remoteAddress)  
      void setRemotePort​(int remotePort)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • UnreliableSocket

        public UnreliableSocket​(java.net.DatagramSocket socket)
                         throws java.net.SocketException
        Creates a new UnreliableSocket with the given socket as the underlying socket. The socket can be in any state (connected, data flow already started...) as long as it isn't closed.
        After calling this constructor, you should call the connect() method if the underlying socket isn't already connected
        If necessary, the receiver buffer size of the underlying socket is changed to receive large packets. A SocketException is thrown if this fails.
        Parameters:
        socket - The underlying socket where data is send and received.
        Throws:
        java.net.SocketException - If buffer resizing fails
    • Method Detail

      • connect

        public void connect​(java.net.InetAddress address,
                            int port)
        "Connects" (Remember, UDP is a connection-less protocol) the socket to another socket. This method doesn't send any data to the given socket, it just remembers the given values.
        You HAVE TO call this method if you want to send data (over the OutputStream provided by this socket).
        Once connected, the InputStream will only accept data received from this remote address.
        This method doesn't call the connect() methods of the underlying socket. If you call the connect() methods of the underlying socket manually, don't forget to call the connect() method of this class!
        It's possible to call the connect() method multiple times, sender and receiver will immediatly update
        Parameters:
        address - Remote address to remember
        port - Remote port to remember
      • getSocket

        public java.net.DatagramSocket getSocket()
        Returns:
        The underlying socket where data is actually send and received
      • close

        public void close()
                   throws java.io.IOException
        Closes the underlying socket.
        Throws:
        java.io.IOException - When closing of a stream fails. Should never happen.
      • getRemoteAddress

        public java.net.InetAddress getRemoteAddress()
        Returns:
        The remote address to which this socket is connected (Set by the connect() method).
      • setRemoteAddress

        public void setRemoteAddress​(java.net.InetAddress remoteAddress)
        See Also:
        connect(InetAddress, int)
      • getRemotePort

        public int getRemotePort()
        Returns:
        The remote port to which this socket is connected (Set by the connect() method).
      • getOutputStream

        public java.io.OutputStream getOutputStream()
        Returns:
        An OutputStream provided by this instance. The OutputStream writes all data into UDP packets and sends them over the underlying socket. As usual in UDP, packets can be lost or received out of order.
        Note: Call the connect() method first before using this OutputStream. Always call the flush() method, otherwise data is only buffered!
      • getInputStream

        public java.io.InputStream getInputStream()
        Returns:
        An InputStream provided by this instance. The InputStream reads data from UDP packets which are received from the underlying socket. As usual in UDP, packets can be lost or received out of order.
        Note: If connected, the InputStream will only accept data send from the connected remote address.