org.p2psockets
Class P2PServerSocket

java.lang.Object
  extended byjava.net.ServerSocket
      extended byorg.p2psockets.P2PServerSocket

public class P2PServerSocket
extends java.net.ServerSocket

This class implements server sockets. A server socket waits for requests to come in over the network. It performs some operation based on that request, and then possibly returns a result to the requester. Instead of being attached to a particular peer group ServerSockets can also be attached to a single specific peer. The name's of these peer ServerSockets are generated from the peer name, such as "BradGNUberg", and always have the ending ".peer", resulting in "BradGNUberg.peer". To "start" a web-site named "www.nike.laborpolicy" on "P2P port" 80, you would use the following: ServerSocket myServer = new P2PServerSocket("www.nike.laborpolicy", 80); Socket client = myServer.accept(); InputStream in = client.getInputStream(); OutputStream out = client.getOutputStream(); // do something client.close(); Note that you must call P2PNetwork.signin(username, password) before using the Sockets and ServerSockets. You must also call P2PNetwork.setApplicationName() to the peer name you want all domain requests to be scoped to. Threading: This class should only be used within one thread. However, the Socket that is returned from the accept() method can safely be passed to another thread if it is only used within that thread.

Version:
0.5
Author:
Brad GNUberg, bkn3@columbia.edu

Field Summary
static java.lang.String ANONYMOUS_PEER_NAME
          This is the default host name we give to a foreign peer that is connecting to us but which we don't know his peer name.
protected static int defaultSoTimeout
          The default timeout for how long to wait for clients.
protected  net.jxta.socket.JxtaServerSocket jxtaServerSocket
          The JxtaServerSocket that we wrap the does all the hard work.
protected static int RECEIVE_BUFFER_SIZE
          This is a random number that has no real meaning; we need is for getSoReceiveBufferSize(), which has no corresponding Jxta value.
protected  P2PSocketAddress socketAddress
           
 
Constructor Summary
P2PServerSocket()
          Creates an unbound server socket.
P2PServerSocket(int port)
          Creates a server socket, bound to the specified port.
P2PServerSocket(int port, int backlog)
          Creates a server socket and binds it to the specified local port number, with the specified backlog.
P2PServerSocket(int port, int backlog, java.net.InetAddress bindAddr)
          Create a server with the specified port, listen backlog, and local IP address to bind to.
P2PServerSocket(java.lang.String host, int port)
          Creates a server socket and binds it to the specified port number and host name
 
Method Summary
 java.net.Socket accept()
          Listens for a connection to be made to this socket and accepts it.
 void bind(java.net.SocketAddress endpoint)
          Binds the ServerSocket to a specific address (IP address and port number).
 void bind(java.net.SocketAddress endpoint, int backlog)
          Binds the ServerSocket to a specific address (IP address and port number).
 void close()
          Closes this socket.
 java.net.InetAddress getInetAddress()
          Returns the local address of this server socket.
 int getLocalPort()
          Returns the port on which this socket is listening.
 java.net.SocketAddress getLocalSocketAddress()
          Returns the address of the endpoint this socket is bound to, or null if it is not bound yet.
 int getReceiveBufferSize()
           
 boolean getReuseAddress()
           
 int getSoTimeout()
          Retrive setting for SO_TIMEOUT.
 boolean isBound()
           
 boolean isClosed()
           
 void setReceiveBufferSize(int size)
           
 void setReuseAddress(boolean on)
           
static void setSocketFactory(java.net.SocketImplFactory fac)
           
 void setSoTimeout(int timeout)
          Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds.
 java.lang.String toString()
          Returns the implementation address and implementation port of this socket as a String.
 
Methods inherited from class java.net.ServerSocket
getChannel, implAccept
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ANONYMOUS_PEER_NAME

public static final java.lang.String ANONYMOUS_PEER_NAME
This is the default host name we give to a foreign peer that is connecting to us but which we don't know his peer name. This is used when we construct a P2PSocket after a JxtaSocket is returned from calling JxtaSocket.accept(). At this point we don't know the foreign peers name but we can't set his host name to null or this will resolve to the loopback address, which would be incorrect.

See Also:
Constant Field Values

RECEIVE_BUFFER_SIZE

protected static final int RECEIVE_BUFFER_SIZE
This is a random number that has no real meaning; we need is for getSoReceiveBufferSize(), which has no corresponding Jxta value.

See Also:
Constant Field Values

defaultSoTimeout

protected static final int defaultSoTimeout
The default timeout for how long to wait for clients. We wait for infinity (i.e. 0).

See Also:
Constant Field Values

jxtaServerSocket

protected net.jxta.socket.JxtaServerSocket jxtaServerSocket
The JxtaServerSocket that we wrap the does all the hard work.


socketAddress

protected P2PSocketAddress socketAddress
Constructor Detail

P2PServerSocket

public P2PServerSocket()
                throws java.io.IOException
Creates an unbound server socket.

Throws:
java.io.IOException - IO error when opening the socket.

P2PServerSocket

public P2PServerSocket(int port)
                throws java.io.IOException
Creates a server socket, bound to the specified port. A port of 0 creates a socket on any free port.

Parameters:
port - the port number, or 0 to use any free port.
Throws:
java.io.IOException - if an I/O error occurs when opening the socket.

P2PServerSocket

public P2PServerSocket(int port,
                       int backlog)
                throws java.io.IOException
Creates a server socket and binds it to the specified local port number, with the specified backlog. A port number of 0 creates a socket on any free port.

Parameters:
port - the specified port, or 0 to use any free port.
backlog - the maximum length of the queue. This parameter is not used and is ignored.
Throws:
java.io.IOException - if an I/O error occurs when opening the socket.

P2PServerSocket

public P2PServerSocket(java.lang.String host,
                       int port)
                throws java.io.IOException
Creates a server socket and binds it to the specified port number and host name


P2PServerSocket

public P2PServerSocket(int port,
                       int backlog,
                       java.net.InetAddress bindAddr)
                throws java.io.IOException
Create a server with the specified port, listen backlog, and local IP address to bind to. The port must be between 0 and 65535, inclusive.

Parameters:
port - the local TCP port
backlog - the listen backlog. This parameter is not used and is ignored.
bindAddr - the InetAddress the server will bind to.
Throws:
java.io.IOException - if an I/O error occurs when opening the socket.
Method Detail

bind

public void bind(java.net.SocketAddress endpoint)
          throws java.io.IOException
Binds the ServerSocket to a specific address (IP address and port number).

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

Parameters:
endpoint - The IP address & port number to bind to.
Throws:
java.io.IOException - if the bind operation fails, or if the socket is already bound.
java.lang.IllegalArgumentException - if endpoint is a SocketAddress subclass not supported by this socket

bind

public void bind(java.net.SocketAddress endpoint,
                 int backlog)
          throws java.io.IOException
Binds the ServerSocket to a specific address (IP address and port number).

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

Parameters:
endpoint - The IP address & port number to bind to.
backlog - The listen backlog length. This parameter is currently ignored.
Throws:
java.io.IOException - if the bind operation fails, or if the socket is already bound.
java.lang.IllegalArgumentException - if endpoint is a SocketAddress subclass not supported by this socket

getInetAddress

public java.net.InetAddress getInetAddress()
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 port on which this socket is listening.

Returns:
the port number to which this socket is listening or -1 if the socket is not bound yet.

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.

Returns:
a SocketAddress representing the local endpoint of this socket, or null if it is not bound yet.

accept

public java.net.Socket accept()
                       throws java.io.IOException
Listens for a connection to be made to this socket and accepts it. The method blocks until a connection is made.

Returns:
the new Socket
Throws:
java.io.IOException - if an I/O error occurs when waiting for a connection.
java.net.SocketTimeoutException - if a timeout was previously set with setSoTimeout and the timeout has been reached.

close

public void close()
           throws java.io.IOException
Closes this socket.

Throws:
java.io.IOException - if an I/O error occurs when closing the socket.

setSoTimeout

public void setSoTimeout(int timeout)
                  throws java.net.SocketException
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a call to accept() for this ServerSocket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the ServerSocket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

Parameters:
timeout - the specified timeout, in milliseconds
Throws:
java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
Since:
JDK1.1
See Also:
getSoTimeout()

getSoTimeout

public int getSoTimeout()
                 throws java.io.IOException
Retrive setting for SO_TIMEOUT. 0 returns implies that the option is disabled (i.e., timeout of infinity).

Returns:
the SO_TIMEOUT value
Throws:
java.io.IOException - if an I/O error occurs
Since:
JDK1.1
See Also:
setSoTimeout(int)

setReuseAddress

public void setReuseAddress(boolean on)
                     throws java.net.SocketException
Throws:
java.net.SocketException

getReuseAddress

public boolean getReuseAddress()
                        throws java.net.SocketException
Throws:
java.net.SocketException

toString

public java.lang.String toString()
Returns the implementation address and implementation port of this socket as a String.

Returns:
a string representation of this socket.

setSocketFactory

public static void setSocketFactory(java.net.SocketImplFactory fac)
                             throws java.io.IOException
Throws:
java.io.IOException

setReceiveBufferSize

public void setReceiveBufferSize(int size)
                          throws java.net.SocketException
Throws:
java.net.SocketException

getReceiveBufferSize

public int getReceiveBufferSize()
                         throws java.net.SocketException
Throws:
java.net.SocketException

isBound

public boolean isBound()

isClosed

public boolean isClosed()