ewe.net
Class ServerSocket

java.lang.Object
  extended byewe.net.ServerSocket

public class ServerSocket
extends Object

A server socket is used to wait for incoming TCP/IP connections.

This operates similar to the java.net.ServerSocket.

If you want to accept connections from a device via the infra-red port, set the host to be "infra-red" and set the port to be 0 when creating the ServerSocket.


Field Summary
static int CHECK_ACCEPT
           
static int CHECK_LISTEN
           
protected  Object localHost
           
protected  int localPortNotUsed
           
 
Constructor Summary
ServerSocket(int port)
          Create a ServerSocket to listen on the local host on a specific port.
ServerSocket(int port, int backlog)
          Create a server socket on the local host at the specified port number.
ServerSocket(int port, int backlog, InetAddress address)
          Creates a server socket at the specified port in blocking mode.
ServerSocket(int port, int backlog, String address, IOHandle handle)
          You should use one of the other ServerSocket constructors which throw an IOException on failure.
ServerSocket(int port, IOHandle handle)
          Creates a server socket at the specified port in non-blocking mode.
ServerSocket(String host, int port)
          Create a ServerSocket to listen on the local host for a specific address, on a specific port.
 
Method Summary
 Socket accept()
          This will accept an incoming TCP/IP connection - blocking version.
 IOHandle accept(IOHandle nullHandle)
          This will accept an incoming TCP/IP connection - non-blocking version.
 void close()
           
 InetAddress getInetAddress()
           
 int getLocalPort()
          This gets the local port the socket is listening on.
 boolean isOpen()
           
protected  int pauseUntilAccepted(int howLong)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Field Detail

localHost

protected Object localHost

localPortNotUsed

protected int localPortNotUsed

CHECK_LISTEN

public static final int CHECK_LISTEN
See Also:
Constant Field Values

CHECK_ACCEPT

public static final int CHECK_ACCEPT
See Also:
Constant Field Values
Constructor Detail

ServerSocket

public ServerSocket(int port,
                    int backlog,
                    InetAddress address)
             throws IOException
Creates a server socket at the specified port in blocking mode. This method blocks the current thread until the ServerSocket is created or an exception is thrown.

If you want to connect to accept connections from a device via the infra-red port, set the host to be "infra-red" and set the port to be 0 when creating the ServerSocket.

Parameters:
address - The address of the local machine to listen to.
port - The port number to listen to. If this is 0, then a new and unused port number will be used. Use getLocalPort() to determine which port was chosen for the ServerSocket.
backlog - The number of backlogged connections to accept.

ServerSocket

public ServerSocket(int port,
                    int backlog)
             throws IOException
Create a server socket on the local host at the specified port number.

This method blocks the current thread until the ServerSocket is created or an exception is thrown.

Parameters:
port - The port to listen for connections on. If the port number is zero a new port number will be assigned. To get the port number actually used, call getLocalPort().
backlog - The number of backlogged connections to accept.
Throws:
IOException - If an error occurs while trying to connect to the specified port.

ServerSocket

public ServerSocket(String host,
                    int port)
             throws IOException
Create a ServerSocket to listen on the local host for a specific address, on a specific port. This method blocks the current thread until the ServerSocket is created or an exception is thrown.

Parameters:
host - The host name to listen to. If this is "infra-red" then the ServerSocket will listen on the infra-red port on the specified port.
port - The port number to listen to. If this is 0, then a new and unused port number will be used (This also works for "infra-red"). Use getLocalPort() to determine which port was chosen for the ServerSocket.
Throws:
IOException - If an error occurs while trying to connect to the specified port.

ServerSocket

public ServerSocket(int port,
                    IOHandle handle)
Creates a server socket at the specified port in non-blocking mode. Provide the constructor with a new IOHandle and then use waitOnFlags() on the Handle to be set to Succeeded or Failed.

Parameters:
port - The port number to listen to. If this is 0, then a new and unused port number will be used. Use getLocalPort() to determine which port was chosen for the ServerSocket.
handle - if this is not null then the method will return immediately and you can use the handle to wait on the handle for success or failure. If handle is null, then the method will not return until the open operation succeeds or fails, blocking all other Threads until it is done.

ServerSocket

public ServerSocket(int port)
             throws IOException
Create a ServerSocket to listen on the local host on a specific port. This method blocks the current thread until the ServerSocket is created or an exception is thrown.

Parameters:
port - The port number to listen to. If this is 0, then a new and unused port number will be used. Use getLocalPort() to determine which port was chosen for the ServerSocket.
Throws:
IOException - If an error occurs while trying to connect to the specified port.

ServerSocket

public ServerSocket(int port,
                    int backlog,
                    String address,
                    IOHandle handle)
You should use one of the other ServerSocket constructors which throw an IOException on failure.

This method starts the process for opening a ServerSocket at the specified IP address for the local host. If the handle parameter is not null, then the method will return immediately and you can use the handle to wait on the handle for success or failure. If handle is null, then the method will not return until the open operation succeeds or fails, blocking all other Threads until it is done. The isOpen() method can then be used to determine if the ServerSocket was created successfully.

The address may be a name for the local host or an IP address for the local host. Since the Local host may have multiple addresses you can use InetAddress.getAllByName(InetAddress.getLocalHostName(),null) to get a list of available InetAddresses for the local host. You can then specify precisely which one you want to use by passing the value returned by InetAddress.getHostAddress() to the constructor for ServerSocket.

Parameters:
port - The port to listen on. If this is zero then any available port is used. You can call getLocalPort() after a successful.
backlog - the backlog of pending connections allowed on the ServerSocket.
address - the local host address to bind to, or null to bind to any on the local host.
handle - if this is not null then the method will return immediately and you can use the handle to wait on the handle for success or failure. If handle is null, then the method will not return until the open operation succeeds or fails, blocking all other Threads until it is done.
Method Detail

close

public void close()
           throws IOException
Throws:
IOException

isOpen

public boolean isOpen()

getLocalPort

public int getLocalPort()
This gets the local port the socket is listening on.


getInetAddress

public InetAddress getInetAddress()

accept

public Socket accept()
              throws IOException
This will accept an incoming TCP/IP connection - blocking version. This will block current Coroutine but allow others to continue. The only way to stop the accept is to close the socket.

Throws:
IOException

accept

public IOHandle accept(IOHandle nullHandle)
This will accept an incoming TCP/IP connection - non-blocking version. When the return ed handle status has the Success flag set, the handle.returnValue member will have a new open Socket to be used for IO. You can abort the accept by calling the stop() method of the returned IOHandle.

Parameters:
nullHandle - should always be null - since a new Handle is always returned.
Returns:
an IOHandle that you can use to monitor or stop the accept.

pauseUntilAccepted

protected int pauseUntilAccepted(int howLong)