Class SocketWrapper

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class SocketWrapper
    extends java.net.Socket
    A wrapper class of Socket class for use by the RMI. Socket class implements client sockets. A socket is an endpoint for communication between two machines.
    • Constructor Summary

      Constructors 
      Constructor Description
      SocketWrapper​(java.net.Socket aSocket)
      Creates SocketWrapper by given Socket.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void bind​(java.net.SocketAddress aBindpoint)
      Binds the socket to a local address.
      void close()
      Closes this socket.
      void connect​(java.net.SocketAddress aEndpoint)
      Connects this socket to the server.
      void connect​(java.net.SocketAddress aEndpoint, int aTimeout)
      Connects this socket to the server with a specified timeout value.
      java.nio.channels.SocketChannel getChannel()
      Returns the unique SocketChannel object associated with this socket, if any.
      java.net.InetAddress getInetAddress()
      Returns the address to which the socket is connected.
      java.io.InputStream getInputStream()
      Returns an InputStreamWrapper for this socket.
      boolean getKeepAlive()
      Tests if SO_KEEPALIVE is enabled.
      java.net.InetAddress getLocalAddress()
      Gets the local address to which the socket is bound.
      int getLocalPort()
      Returns the local port to which this socket is bound.
      java.net.SocketAddress getLocalSocketAddress()
      Returns the address of the endpoint this socket is bound to, or null if it is not bound yet
      boolean getOOBInline()
      Tests if OOBINLINE is enabled.
      java.io.OutputStream getOutputStream()
      Returns an output stream for this socket.
      int getPort()
      Returns the remote port to which this socket is connected.
      int getReceiveBufferSize()
      Gets the value of the SO_RCVBUF option for this Socket, that is the buffer size used by the platform for input on this Socket.
      java.net.SocketAddress getRemoteSocketAddress()
      Returns the address of the endpoint this socket is connected to, or null if it is unconnected.
      boolean getReuseAddress()
      Tests if SO_REUSEADDR is enabled.
      int getSendBufferSize()
      Get value of the SO_SNDBUF option for this Socket, that is the buffer size used by the platform for output on this Socket.
      int getSoLinger()
      Returns setting for SO_LINGER.
      int getSoTimeout()
      Returns setting for SO_TIMEOUT.
      boolean getTcpNoDelay()
      Tests if TCP_NODELAY is enabled.
      int getTrafficClass()
      Gets traffic class or type-of-service in the IP header for packets sent from this Socket
      boolean isBound()
      Returns the binding state of the socket.
      boolean isClosed()
      Returns the closed state of the socket.
      boolean isConnected()
      Returns the connection state of the socket.
      boolean isInputShutdown()
      Returns whether the read-half of the socket connection is closed.
      boolean isOutputShutdown()
      Returns whether the write-half of the socket connection is closed.
      void sendUrgentData​(int aData)
      Send one byte of urgent data on the socket.
      void setKeepAlive​(boolean aOn)
      Enable/disable SO_KEEPALIVE.
      void setOOBInline​(boolean aOn)
      Enable/disable OOBINLINE (receipt of TCP urgent data) By default, this option is disabled and TCP urgent data received on a socket is silently discarded.
      void setReceiveBufferSize​(int aSize)
      Sets the SO_RCVBUF option to the specified value for this Socket.
      void setReuseAddress​(boolean aOn)
      Enable/disable the SO_REUSEADDR socket option.
      void setSendBufferSize​(int aSize)
      Sets the SO_SNDBUF option to the specified value for this Socket.
      void setSoLinger​(boolean aOn, int aLinger)
      Enable/disable SO_LINGER with the specified linger time in seconds.
      void setSoTimeout​(int aTimeout)
      Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
      void setTcpNoDelay​(boolean aOn)
      Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
      void setTrafficClass​(int aTc)
      Sets traffic class or type-of-service octet in the IP header for packets sent from this Socket.
      void shutdownInput()
      Places the input stream for this socket at "end of stream".
      void shutdownOutput()
      Disables the output stream for this socket.
      java.lang.String toString()
      Converts this socket to a String.
      • Methods inherited from class java.net.Socket

        getOption, setOption, setPerformancePreferences, setSocketImplFactory, supportedOptions
      • Methods inherited from class java.lang.Object

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

      • SocketWrapper

        public SocketWrapper​(java.net.Socket aSocket)
        Creates SocketWrapper by given Socket.
        Parameters:
        aSocket - Socket object
    • Method Detail

      • connect

        public void connect​(java.net.SocketAddress aEndpoint)
                     throws java.io.IOException
        Connects this socket to the server.
        Overrides:
        connect in class java.net.Socket
        Parameters:
        aEndpoint - the SocketAddress
        Throws:
        java.io.IOException - if an error occurs during the connection
      • connect

        public void connect​(java.net.SocketAddress aEndpoint,
                            int aTimeout)
                     throws java.io.IOException
        Connects this socket to the server with a specified timeout value. A timeout of zero is interpreted as an infinite timeout. The connection will then block until established or an error occurs.
        Overrides:
        connect in class java.net.Socket
        Parameters:
        aEndpoint - the SocketAddress
        aTimeout - the timeout value to be used in milliseconds.
        Throws:
        java.io.IOException - if an error occurs during the connection
      • bind

        public void bind​(java.net.SocketAddress aBindpoint)
                  throws java.io.IOException
        Binds the socket to a local address.

        If the address is null, then the system will pick up an ephemeral port and a valid local address to bind the socket.

        Overrides:
        bind in class java.net.Socket
        Parameters:
        aBindpoint - the SocketAddress to bind to
        Throws:
        java.io.IOException
      • getInetAddress

        public java.net.InetAddress getInetAddress()
        Returns the address to which the socket is connected.
        Overrides:
        getInetAddress in class java.net.Socket
        Returns:
        the remote IP address to which this socket is connected, or null if the socket is not connected.
      • getLocalAddress

        public java.net.InetAddress getLocalAddress()
        Gets the local address to which the socket is bound.
        Overrides:
        getLocalAddress in class java.net.Socket
        Returns:
        the local address to which the socket is bound or InetAddress.anyLocalAddress() if the socket is not bound yet
      • getPort

        public int getPort()
        Returns the remote port to which this socket is connected.
        Overrides:
        getPort in class java.net.Socket
        Returns:
        the remote port number to which this socket is connected, or 0 if the socket is not connected yet
      • getLocalPort

        public int getLocalPort()
        Returns the local port to which this socket is bound.
        Overrides:
        getLocalPort in class java.net.Socket
        Returns:
        the local port number to which this socket is bound or -1 if the socket is not bound yet
      • getRemoteSocketAddress

        public java.net.SocketAddress getRemoteSocketAddress()
        Returns the address of the endpoint this socket is connected to, or null if it is unconnected.
        Overrides:
        getRemoteSocketAddress in class java.net.Socket
        Returns:
        a SocketAddress reprensenting the remote endpoint of this socket, or null if it is not connected yet
        See Also:
        getInetAddress(), connect(SocketAddress)
      • getLocalSocketAddress

        public java.net.SocketAddress getLocalSocketAddress()
        Returns the address of the endpoint this socket is bound to, or null if it is not bound yet
        Overrides:
        getLocalSocketAddress in class java.net.Socket
        Returns:
        a SocketAddress representing the local endpoint of this socket, or null if it is not bound yet
        See Also:
        getLocalAddress(), bind(SocketAddress)
      • getChannel

        public java.nio.channels.SocketChannel getChannel()
        Returns the unique SocketChannel object associated with this socket, if any.
        Overrides:
        getChannel in class java.net.Socket
        Returns:
        the socket channel associated with this socket, or null if this socket was not created for a channel
      • getInputStream

        public java.io.InputStream getInputStream()
                                           throws java.io.IOException
        Returns an InputStreamWrapper for this socket.
        Overrides:
        getInputStream in class java.net.Socket
        Returns:
        an InputStreamWrapper for reading bytes from this socket
        Throws:
        java.io.IOException - if an I/O error occurs when creating the input stream, the socket is closed, the socket is not connected, or the socket input has been shutdown using shutdownInput()
      • getOutputStream

        public java.io.OutputStream getOutputStream()
                                             throws java.io.IOException
        Returns an output stream for this socket.
        Overrides:
        getOutputStream in class java.net.Socket
        Returns:
        an output stream for writing bytes to this socket
        Throws:
        java.io.IOException - if an I/O error occurs when creating the output stream or if the socket is not connected
      • setTcpNoDelay

        public void setTcpNoDelay​(boolean aOn)
                           throws java.net.SocketException
        Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
        Overrides:
        setTcpNoDelay in class java.net.Socket
        Parameters:
        aOn - true to enable TCP_NODELAY, false to disable.
        Throws:
        java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
      • getTcpNoDelay

        public boolean getTcpNoDelay()
                              throws java.net.SocketException
        Tests if TCP_NODELAY is enabled.
        Overrides:
        getTcpNoDelay in class java.net.Socket
        Returns:
        a boolean indicating whether or not TCP_NODELAY is enabled.
        Throws:
        java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
      • setSoLinger

        public void setSoLinger​(boolean aOn,
                                int aLinger)
                         throws java.net.SocketException
        Enable/disable SO_LINGER with the specified linger time in seconds. The maximum timeout value is platform specific. The setting only affects socket close.
        Overrides:
        setSoLinger in class java.net.Socket
        Parameters:
        aOn - whether or not to linger on
        aLinger - how long to linger for, if on is true
        Throws:
        java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error
      • getSoLinger

        public int getSoLinger()
                        throws java.net.SocketException
        Returns setting for SO_LINGER. -1 returns implies that the option is disabled. The setting only affects socket close.
        Overrides:
        getSoLinger in class java.net.Socket
        Returns:
        the setting for SO_LINGER.
        Throws:
        java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error
      • sendUrgentData

        public void sendUrgentData​(int aData)
                            throws java.io.IOException
        Send one byte of urgent data on the socket. The byte to be sent is the lowest eight bits of the data parameter. The urgent byte is sent after any preceding writes to the socket OutputStream and before any future writes to the OutputStream.
        Overrides:
        sendUrgentData in class java.net.Socket
        Parameters:
        aData - The byte of data to send
        Throws:
        java.io.IOException - if there is an error sending the data.
      • setOOBInline

        public void setOOBInline​(boolean aOn)
                          throws java.net.SocketException
        Enable/disable OOBINLINE (receipt of TCP urgent data) By default, this option is disabled and TCP urgent data received on a socket is silently discarded.
        Overrides:
        setOOBInline in class java.net.Socket
        Parameters:
        aOn - true to enable OOBINLINE, false to disable.
        Throws:
        java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
      • getOOBInline

        public boolean getOOBInline()
                             throws java.net.SocketException
        Tests if OOBINLINE is enabled.
        Overrides:
        getOOBInline in class java.net.Socket
        Returns:
        a boolean indicating whether or not OOBINLINE is enabled.
        Throws:
        java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
      • setSoTimeout

        public void setSoTimeout​(int aTimeout)
                          throws java.net.SocketException
        Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
        Overrides:
        setSoTimeout in class java.net.Socket
        Parameters:
        aTimeout - the specified timeout, in milliseconds.
        Throws:
        java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
        See Also:
        getSoTimeout()
      • getSoTimeout

        public int getSoTimeout()
                         throws java.net.SocketException
        Returns setting for SO_TIMEOUT. 0 returns implies that the option is disabled (i.e., timeout of infinity).
        Overrides:
        getSoTimeout in class java.net.Socket
        Returns:
        the setting for SO_TIMEOUT
        Throws:
        java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
        See Also:
        setSoTimeout(int)
      • setSendBufferSize

        public void setSendBufferSize​(int aSize)
                               throws java.net.SocketException
        Sets the SO_SNDBUF option to the specified value for this Socket.
        Overrides:
        setSendBufferSize in class java.net.Socket
        Parameters:
        aSize - the size to which to set the send buffer size. This value must be greater than 0.
        Throws:
        java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
        See Also:
        getSendBufferSize()
      • getSendBufferSize

        public int getSendBufferSize()
                              throws java.net.SocketException
        Get value of the SO_SNDBUF option for this Socket, that is the buffer size used by the platform for output on this Socket.
        Overrides:
        getSendBufferSize in class java.net.Socket
        Returns:
        the value of the SO_SNDBUF option for this Socket.
        Throws:
        java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
        See Also:
        setSendBufferSize(int)
      • setReceiveBufferSize

        public void setReceiveBufferSize​(int aSize)
                                  throws java.net.SocketException
        Sets the SO_RCVBUF option to the specified value for this Socket.
        Overrides:
        setReceiveBufferSize in class java.net.Socket
        Parameters:
        aSize - the size to which to set the receive buffer size. This value must be greater than 0.
        Throws:
        java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
        See Also:
        getReceiveBufferSize()
      • getReceiveBufferSize

        public int getReceiveBufferSize()
                                 throws java.net.SocketException
        Gets the value of the SO_RCVBUF option for this Socket, that is the buffer size used by the platform for input on this Socket.
        Overrides:
        getReceiveBufferSize in class java.net.Socket
        Returns:
        the value of the SO_RCVBUF option for this Socket.
        Throws:
        java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
        See Also:
        setReceiveBufferSize(int)
      • setKeepAlive

        public void setKeepAlive​(boolean aOn)
                          throws java.net.SocketException
        Enable/disable SO_KEEPALIVE.
        Overrides:
        setKeepAlive in class java.net.Socket
        Parameters:
        aOn - whether or not to have socket keep alive turned on.
        Throws:
        java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
        See Also:
        getKeepAlive()
      • getKeepAlive

        public boolean getKeepAlive()
                             throws java.net.SocketException
        Tests if SO_KEEPALIVE is enabled.
        Overrides:
        getKeepAlive in class java.net.Socket
        Returns:
        a boolean indicating whether or not SO_KEEPALIVE is enabled.
        Throws:
        java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
        See Also:
        setKeepAlive(boolean)
      • setTrafficClass

        public void setTrafficClass​(int aTc)
                             throws java.net.SocketException
        Sets traffic class or type-of-service octet in the IP header for packets sent from this Socket.
        Overrides:
        setTrafficClass in class java.net.Socket
        Parameters:
        aTc - an int value for the bitset.
        Throws:
        java.net.SocketException - if there is an error setting the traffic class or type-of-service
        See Also:
        getTrafficClass()
      • getTrafficClass

        public int getTrafficClass()
                            throws java.net.SocketException
        Gets traffic class or type-of-service in the IP header for packets sent from this Socket
        Overrides:
        getTrafficClass in class java.net.Socket
        Returns:
        the traffic class or type-of-service already set
        Throws:
        java.net.SocketException - if there is an error obtaining the traffic class or type-of-service value.
        See Also:
        setTrafficClass(int)
      • setReuseAddress

        public void setReuseAddress​(boolean aOn)
                             throws java.net.SocketException
        Enable/disable the SO_REUSEADDR socket option.
        Overrides:
        setReuseAddress in class java.net.Socket
        Parameters:
        aOn - whether to enable or disable the socket option
        Throws:
        java.net.SocketException - if an error occurs enabling or disabling the SO_RESUEADDR socket option, or the socket is closed.
        See Also:
        getReuseAddress()
      • getReuseAddress

        public boolean getReuseAddress()
                                throws java.net.SocketException
        Tests if SO_REUSEADDR is enabled.
        Overrides:
        getReuseAddress in class java.net.Socket
        Returns:
        a boolean indicating whether or not SO_REUSEADDR is enabled.
        Throws:
        java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
        See Also:
        setReuseAddress(boolean)
      • close

        public void close()
                   throws java.io.IOException
        Closes this socket.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.net.Socket
        Throws:
        java.io.IOException - if an I/O error occurs when closing this socket.
      • shutdownInput

        public void shutdownInput()
                           throws java.io.IOException
        Places the input stream for this socket at "end of stream". Any data sent to the input stream side of the socket is acknowledged and then silently discarded.
        Overrides:
        shutdownInput in class java.net.Socket
        Throws:
        java.io.IOException - if an I/O error occurs when shutting down this socket.
      • shutdownOutput

        public void shutdownOutput()
                            throws java.io.IOException
        Disables the output stream for this socket. For a TCP socket, any previously written data will be sent followed by TCP's normal connection termination sequence. If you write to a socket output stream after invoking shutdownOutput() on the socket, the stream will throw an IOException.
        Overrides:
        shutdownOutput in class java.net.Socket
        Throws:
        java.io.IOException - if an I/O error occurs when shutting down this socket.
      • toString

        public java.lang.String toString()
        Converts this socket to a String.
        Overrides:
        toString in class java.net.Socket
        Returns:
        string representation of this socket.
      • isConnected

        public boolean isConnected()
        Returns the connection state of the socket.
        Overrides:
        isConnected in class java.net.Socket
        Returns:
        true if the socket successfuly connected to a server
      • isBound

        public boolean isBound()
        Returns the binding state of the socket.
        Overrides:
        isBound in class java.net.Socket
        Returns:
        true if the socket successfuly bound to an address
      • isClosed

        public boolean isClosed()
        Returns the closed state of the socket.
        Overrides:
        isClosed in class java.net.Socket
        Returns:
        true if the socket has been closed
        See Also:
        close()
      • isInputShutdown

        public boolean isInputShutdown()
        Returns whether the read-half of the socket connection is closed.
        Overrides:
        isInputShutdown in class java.net.Socket
        Returns:
        true if the input of the socket has been shutdown
        See Also:
        shutdownInput()
      • isOutputShutdown

        public boolean isOutputShutdown()
        Returns whether the write-half of the socket connection is closed.
        Overrides:
        isOutputShutdown in class java.net.Socket
        Returns:
        true if the output of the socket has been shutdown