org.p2psockets
Class P2PSocket

java.lang.Object
  extended byjava.net.Socket
      extended byorg.p2psockets.P2PSocket

public class P2PSocket
extends java.net.Socket

This class implements client sockets (also called just "sockets"). A socket is an endpoint for communication between two machines. Threading: this class is not threadsafe and should only be used from one thread. It is unclear if using the socket's input stream in one thread and the output stream in another thread will be threadsafe; it is also unclear if the original java.net.Sockets supports this.

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

Field Summary
protected  net.jxta.socket.JxtaSocket jxtaSocket
          The JXTA socket that actually does all the hard work.
protected  P2PSocketAddress localSocketAddress
          The local end of the socket connection.
protected  P2PSocketAddress remoteSocketAddress
          The remote end of the socket connection.
protected static int SEND_BUFFER_SIZE
          This is a random number that has no real meaning; we need is for getSoSendBufferSize(), which has no corresponding JXTA value.
protected  java.io.InputStream socketIn
           
protected  java.io.OutputStream socketOut
           
 
Constructor Summary
  P2PSocket()
          Creates an unconnected socket.
  P2PSocket(java.net.InetAddress address, int port)
          Creates a stream socket and connects it to the specified port number at the specified IP address.
  P2PSocket(java.net.InetAddress address, int port, boolean stream)
          Creates a socket and connects it to the specified port number at the specified IP address.
  P2PSocket(java.net.InetAddress address, int port, java.net.InetAddress localAddr, int localPort)
          Creates a socket and connects it to the specified remote address on the specified remote port.
protected P2PSocket(net.jxta.socket.JxtaSocket clientJxtaSocket, java.lang.String remoteHost, int remotePort, java.net.InetAddress localAddress, int localPort)
          Used by P2PServerSocket to create the socket returned by the accept() method.
  P2PSocket(java.lang.String host, int port)
          Creates a stream socket and connects it to the specified port number on the named host.
  P2PSocket(java.lang.String host, int port, boolean stream)
          Creates a stream socket and connects it to the specified port number on the named host.
  P2PSocket(java.lang.String host, int port, java.net.InetAddress localAddr, int localPort)
          Creates a socket and connects it to the specified remote host on the specified remote port.
 
Method Summary
 void bind(java.net.SocketAddress bindpoint)
          Binds the socket to a local address.
 void close()
          Closes this socket.
 void connect(java.net.SocketAddress endpoint)
          Connects this socket to the server.
 void connect(java.net.SocketAddress endpoint, int timeout)
          Connects this socket to the server with a specified timeout value.
 java.net.InetAddress getInetAddress()
          Returns the address to which the socket is connected.
 java.io.InputStream getInputStream()
          Returns an input stream for this socket.
 boolean getKeepAlive()
           
 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()
           
 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()
           
 java.net.SocketAddress getRemoteSocketAddress()
          Returns the address of the endpoint this socket is connected to, or null if it is unconnected.
 boolean getReuseAddress()
           
 int getSendBufferSize()
           
 int getSoLinger()
           
 int getSoTimeout()
           
 boolean getTcpNoDelay()
           
 int getTrafficClass()
           
 boolean isBound()
           
 boolean isClosed()
           
 boolean isConnected()
           
 boolean isInputShutdown()
           
 boolean isOutputShutdown()
           
 void sendUrgentData(int data)
           
 void setKeepAlive(boolean on)
           
 void setOOBInline(boolean on)
           
 void setReceiveBufferSize(int size)
           
 void setReuseAddress(boolean on)
           
 void setSendBufferSize(int size)
           
static void setSocketImplFactory(java.net.SocketImplFactory fac)
           
 void setSoLinger(boolean on, int linger)
           
 void setSoTimeout(int timeout)
           
 void setTcpNoDelay(boolean on)
           
 void setTrafficClass(int tc)
           
 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
getChannel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

remoteSocketAddress

protected P2PSocketAddress remoteSocketAddress
The remote end of the socket connection.


localSocketAddress

protected P2PSocketAddress localSocketAddress
The local end of the socket connection.


jxtaSocket

protected net.jxta.socket.JxtaSocket jxtaSocket
The JXTA socket that actually does all the hard work.


socketIn

protected java.io.InputStream socketIn

socketOut

protected java.io.OutputStream socketOut

SEND_BUFFER_SIZE

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

See Also:
Constant Field Values
Constructor Detail

P2PSocket

public P2PSocket()
Creates an unconnected socket.


P2PSocket

public P2PSocket(java.lang.String host,
                 int port)
          throws java.net.UnknownHostException,
                 java.io.IOException
Creates a stream socket and connects it to the specified port number on the named host.

Parameters:
host - the host name.
port - the port number.
Throws:
java.net.UnknownHostException - if the IP address of the host could not be determined.
java.io.IOException - if an I/O error occurs when creating the socket.

P2PSocket

public P2PSocket(java.net.InetAddress address,
                 int port)
          throws java.io.IOException
Creates a stream socket and connects it to the specified port number at the specified IP address.

Parameters:
address - the IP address.
port - the port number.
Throws:
java.io.IOException - if an I/O error occurs when creating the socket.

P2PSocket

public P2PSocket(java.lang.String host,
                 int port,
                 java.net.InetAddress localAddr,
                 int localPort)
          throws java.io.IOException
Creates a socket and connects it to the specified remote host on the specified remote port. The Socket will also bind() to the local address and port supplied.

Parameters:
host - the name of the remote host
port - the remote port
localAddr - the local address the socket is socketBound to. This value is ignored, since JXTA peers are not multihomed.
localPort - the local port the socket is socketBound to
Throws:
java.io.IOException - if an I/O error occurs when creating the socket.

P2PSocket

public P2PSocket(java.net.InetAddress address,
                 int port,
                 java.net.InetAddress localAddr,
                 int localPort)
          throws java.io.IOException
Creates a socket and connects it to the specified remote address on the specified remote port. The Socket will also bind() to the local address and port supplied.

Parameters:
address - the remote address
port - the remote port
localAddr - the local address the socket is socketBound to. This value is ignored, since JXTA peers are not multihomed.
localPort - the local port the socket is socketBound to
Throws:
java.io.IOException - if an I/O error occurs when creating the socket.

P2PSocket

public P2PSocket(java.lang.String host,
                 int port,
                 boolean stream)
          throws java.io.IOException
Creates a stream socket and connects it to the specified port number on the named host. Non-stream sockets (i.e. UDP sockets) are not currently supported.

Parameters:
host - the host name.
port - the port number.
stream - a boolean indicating whether this is a stream socket or a datagram socket.
Throws:
java.io.IOException - if an I/O error occurs when creating the socket.

P2PSocket

public P2PSocket(java.net.InetAddress address,
                 int port,
                 boolean stream)
          throws java.io.IOException
Creates a socket and connects it to the specified port number at the specified IP address. Non-stream sockets (i.e. UDP sockets) are not currently supported.

Parameters:
port - the port number.
stream - if true, create a stream socket; otherwise, create a datagram socket.
Throws:
java.io.IOException - if an I/O error occurs when creating the socket.

P2PSocket

protected P2PSocket(net.jxta.socket.JxtaSocket clientJxtaSocket,
                    java.lang.String remoteHost,
                    int remotePort,
                    java.net.InetAddress localAddress,
                    int localPort)
             throws java.io.IOException
Used by P2PServerSocket to create the socket returned by the accept() method.

Method Detail

connect

public void connect(java.net.SocketAddress endpoint)
             throws java.io.IOException
Connects this socket to the server.

Parameters:
endpoint - the P2PSocketAddress
Throws:
java.io.IOException - if an error occurs during the connection
java.lang.IllegalArgumentException - if endpoint is null or is a SocketAddress subclass not supported by this socket

connect

public void connect(java.net.SocketAddress endpoint,
                    int timeout)
             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.

Parameters:
endpoint - the SocketAddress
timeout - the timeout value to be used in milliseconds. Timeout values are not currently supported by P2P Sockets.
Throws:
java.io.IOException - if an error occurs during the connection
java.net.SocketTimeoutException - if timeout expires before connecting
java.lang.IllegalArgumentException - if endpoint is null or is a SocketAddress subclass not supported by this socket

bind

public void bind(java.net.SocketAddress bindpoint)
          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.

Parameters:
bindpoint - the SocketAddress to bind to
Throws:
java.io.IOException - if the bind operation fails, or if the socket is already bound.
java.lang.IllegalArgumentException - if bindpoint is a SocketAddress subclass not supported by this socket

getInetAddress

public java.net.InetAddress getInetAddress()
Returns the address to which the socket is connected.

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.

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.

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.

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.

Returns:
a SocketAddress reprensenting the remote endpoint of this socket, or null if it is not connected 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.

getInputStream

public java.io.InputStream getInputStream()
                                   throws java.io.IOException
Returns an input stream for this socket.

Returns:
an input stream 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 socketConnected, 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.

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 on)
                   throws java.net.SocketException
Throws:
java.net.SocketException

getTcpNoDelay

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

setSoLinger

public void setSoLinger(boolean on,
                        int linger)
                 throws java.net.SocketException
Throws:
java.net.SocketException

getSoLinger

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

sendUrgentData

public void sendUrgentData(int data)
                    throws java.io.IOException
Throws:
java.io.IOException

setOOBInline

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

getOOBInline

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

setSoTimeout

public void setSoTimeout(int timeout)
                  throws java.net.SocketException
Throws:
java.net.SocketException

getSoTimeout

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

setSendBufferSize

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

getSendBufferSize

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

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

setKeepAlive

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

getKeepAlive

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

setTrafficClass

public void setTrafficClass(int tc)
                     throws java.net.SocketException
Throws:
java.net.SocketException

getTrafficClass

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

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

close

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

Any thread currently blocked in an I/O operation upon this socket will throw a SocketException.

Once a socket has been closed, it is not available for further networking use (i.e. can't be reconnected or rebound). A new socket needs to be created.

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.

If you read from a socket input stream after invoking shutdownInput() on the socket, the stream will return EOF.

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.

Throws:
java.io.IOException - if an I/O error occurs when shutting down this socket.

isConnected

public boolean isConnected()

isBound

public boolean isBound()

isClosed

public boolean isClosed()

isInputShutdown

public boolean isInputShutdown()

isOutputShutdown

public boolean isOutputShutdown()

toString

public java.lang.String toString()
Converts this socket to a String.

Returns:
a string representation of this socket.

setSocketImplFactory

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