Class ServerSocket


  • public class ServerSocket
    extends java.lang.Object
    ServerSocket is a TCP/IP network server socket.

    Under Java and Windows CE, if no network is present, the socket constructor may hang for an extended period of time due to the implementation of sockets in the underlying OS. This is a known problem.

    Here is an example showing the server accepting a client connection and writing/reading some data to/from it:

     ServerSocket server = new ServerSocket(1024);
     Socket client = server.accept();
     byte[] bytes = "HELLO WORLD".getBytes();
     client.writeBytes(bytes, 0, bytes.length);
     byte[] buf = new byte[10];
     int count = client.readBytes(buf, 0, buf.length);
     if (count == buf.length)
        ...
     client.close();
     server.close();
     
    Important: you cannot open a server socket before the main event loop. In other words, you cannot open a socket in the app's constructor, but CAN in the initUI method.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_BACKLOG
      The default backlog value
      static int DEFAULT_SOTIMEOUT
      The default value for the accept operation timeout.
      static int WAIT_FOREVER
      Passing a value of 0 to the constructor causes any accept operation to wait indefinitely.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected ServerSocket()
      For internal use only
        ServerSocket​(int port)
      Opens a server socket.
        ServerSocket​(int port, int timeout)
      Opens a server socket.
        ServerSocket​(int port, int timeout, int backlog, java.lang.String addr)
      Opens a server socket.
        ServerSocket​(int port, int timeout, java.lang.String addr)
      Opens a server socket.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Socket accept()
      Listens for a connection to be made to this socket and accepts it, returning a Socket representing the connection established with the client.
      void close()
      Closes the server socket.
      protected void finalize()  
      java.lang.String getHost()
      Returns the local address of this server socket.
      int getLocalPort()
      Returns the local TCP port on which this socket is listening, passed in the constructor.
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_SOTIMEOUT

        public static final int DEFAULT_SOTIMEOUT
        The default value for the accept operation timeout.
        See Also:
        Constant Field Values
      • DEFAULT_BACKLOG

        public static final int DEFAULT_BACKLOG
        The default backlog value
        See Also:
        Constant Field Values
      • WAIT_FOREVER

        public static final int WAIT_FOREVER
        Passing a value of 0 to the constructor causes any accept operation to wait indefinitely.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ServerSocket

        protected ServerSocket()
        For internal use only
      • ServerSocket

        public ServerSocket​(int port)
                     throws IOException
        Opens a server socket. This method establishes a server socket connection at the specified port, with the default timeout for accept operations.
        Parameters:
        port - the local TCP port to listen for incoming connections
        Throws:
        IOException
      • ServerSocket

        public ServerSocket​(int port,
                            int timeout)
                     throws IOException
        Opens a server socket. This method establishes a server socket connection at the specified port, with the specified timeout for accept operations.
        Parameters:
        port - the local TCP port to listen for incoming connections
        timeout - the accept operation timeout
        Throws:
        IOException
      • ServerSocket

        public ServerSocket​(int port,
                            int timeout,
                            java.lang.String addr)
                     throws IOException
        Opens a server socket. This method establishes a server socket connection at the specified port, with the specified timeout for accept operations. The addr argument can be used on a multi-homed host for a ServerSocket that will only accept connect requests to one of its addresses.
        Parameters:
        port - the local TCP port to listen for incoming connections
        timeout - the accept operation timeout
        addr - the local address this server will bind to
        Throws:
        IOException
      • ServerSocket

        public ServerSocket​(int port,
                            int timeout,
                            int backlog,
                            java.lang.String addr)
                     throws IOException
        Opens a server socket. This method establishes a server socket connection at the specified port, with the specified timeout for accept operations. The addr argument can be used on a multi-homed host for a ServerSocket that will only accept connect requests to one of its addresses.
        Parameters:
        port - the local TCP port to listen for incoming connections
        timeout - the accept operation timeout
        backlog - the limit of concurrent connections that are accepted.
        addr - the local address this server will bind to
        Throws:
        IOException
    • Method Detail

      • getHost

        public java.lang.String getHost()
        Returns the local address of this server socket.
        Returns:
        the address to which this socket is bound, or null if the socket is unbound.
      • getLocalPort

        public int getLocalPort()
        Returns the local TCP port on which this socket is listening, passed in the constructor.
        Returns:
        the port number to which this socket is listening.
      • accept

        public Socket accept()
                      throws IOException
        Listens for a connection to be made to this socket and accepts it, returning a Socket representing the connection established with the client. This method blocks until a connection is made or the operation times out.
        Returns:
        the client Socket
        Throws:
        IOException
      • finalize

        protected void finalize()
        Overrides:
        finalize in class java.lang.Object