|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.net.ServerSocket
org.p2psockets.P2PServerSocket
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.
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 |
public static final java.lang.String ANONYMOUS_PEER_NAME
protected static final int RECEIVE_BUFFER_SIZE
protected static final int defaultSoTimeout
protected net.jxta.socket.JxtaServerSocket jxtaServerSocket
protected P2PSocketAddress socketAddress
Constructor Detail |
public P2PServerSocket() throws java.io.IOException
java.io.IOException
- IO error when opening the socket.public P2PServerSocket(int port) throws java.io.IOException
0
creates a socket on any free port.
port
- the port number, or 0
to use any
free port.
java.io.IOException
- if an I/O error occurs when opening the socket.public P2PServerSocket(int port, int backlog) throws java.io.IOException
0
creates a socket on any
free port.
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.
java.io.IOException
- if an I/O error occurs when opening the socket.public P2PServerSocket(java.lang.String host, int port) throws java.io.IOException
public P2PServerSocket(int port, int backlog, java.net.InetAddress bindAddr) throws java.io.IOException
port
- the local TCP portbacklog
- the listen backlog. This parameter
is not used and is ignored.bindAddr
- the InetAddress the server will bind to.
java.io.IOException
- if an I/O error occurs when opening the socket.Method Detail |
public void bind(java.net.SocketAddress endpoint) throws java.io.IOException
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.
endpoint
- The IP address & port number to bind to.
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 socketpublic void bind(java.net.SocketAddress endpoint, int backlog) throws java.io.IOException
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.
endpoint
- The IP address & port number to bind to.backlog
- The listen backlog length. This parameter
is currently ignored.
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 socketpublic java.net.InetAddress getInetAddress()
null
if the socket is unbound.public int getLocalPort()
public java.net.SocketAddress getLocalSocketAddress()
null
if it is not bound yet.
SocketAddress
representing the local endpoint of this
socket, or null
if it is not bound yet.public java.net.Socket accept() throws java.io.IOException
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.public void close() throws java.io.IOException
java.io.IOException
- if an I/O error occurs when closing the socket.public void setSoTimeout(int timeout) throws java.net.SocketException
timeout
- the specified timeout, in milliseconds
java.net.SocketException
- if there is an error in
the underlying protocol, such as a TCP error.getSoTimeout()
public int getSoTimeout() throws java.io.IOException
java.io.IOException
- if an I/O error occurssetSoTimeout(int)
public void setReuseAddress(boolean on) throws java.net.SocketException
java.net.SocketException
public boolean getReuseAddress() throws java.net.SocketException
java.net.SocketException
public java.lang.String toString()
String
.
public static void setSocketFactory(java.net.SocketImplFactory fac) throws java.io.IOException
java.io.IOException
public void setReceiveBufferSize(int size) throws java.net.SocketException
java.net.SocketException
public int getReceiveBufferSize() throws java.net.SocketException
java.net.SocketException
public boolean isBound()
public boolean isClosed()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |