|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.net.ServerSocket
public class ServerSocket
This class models server side sockets. The basic model is that the
server socket is created and bound to some well known port. It then
listens for and accepts connections. At that point the client and
server sockets are ready to communicate with one another utilizing
whatever application layer protocol they desire.
As with the Socket
class, most instance methods of this class
simply redirect their calls to an implementation class.
Constructor Summary | |
---|---|
ServerSocket()
Constructor that simply sets the implementation. |
|
ServerSocket(int port)
Creates a server socket and binds it to the specified port. |
|
ServerSocket(int port,
int backlog)
Creates a server socket and binds it to the specified port. |
|
ServerSocket(int port,
int backlog,
InetAddress bindAddr)
Creates a server socket and binds it to the specified port. |
Method Summary | |
---|---|
Socket |
accept()
Accepts a new connection and returns a connected Socket
instance representing that connection. |
void |
bind(SocketAddress endpoint)
Binds the server socket to a specified socket address |
void |
bind(SocketAddress endpoint,
int backlog)
Binds the server socket to a specified socket address |
void |
close()
Closes this socket and stops listening for connections |
ServerSocketChannel |
getChannel()
Returns the unique ServerSocketChannel object
associated with this socket, if any. |
InetAddress |
getInetAddress()
This method returns the local address to which this socket is bound |
int |
getLocalPort()
This method returns the local port number to which this socket is bound |
SocketAddress |
getLocalSocketAddress()
Returns the local socket address |
int |
getReceiveBufferSize()
This method returns the value of the system level socket option SO_RCVBUF, which is used by the operating system to tune buffer sizes for data transfers. |
boolean |
getReuseAddress()
Checks if the SO_REUSEADDR option is enabled |
int |
getSoTimeout()
Retrieves the current value of the SO_TIMEOUT setting. |
protected void |
implAccept(Socket socket)
This protected method is used to help subclasses override ServerSocket.accept() . |
boolean |
isBound()
Returns true when the socket is bound, otherwise false |
boolean |
isClosed()
Returns true if the socket is closed, otherwise false |
void |
setReceiveBufferSize(int size)
This method sets the value for the system level socket option SO_RCVBUF to the specified value. |
void |
setReuseAddress(boolean on)
Enables/Disables the SO_REUSEADDR option |
static void |
setSocketFactory(SocketImplFactory fac)
Sets the SocketImplFactory for all
ServerSocket 's. |
void |
setSoTimeout(int timeout)
Sets the value of SO_TIMEOUT. |
String |
toString()
Returns the value of this socket as a String . |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public ServerSocket() throws IOException
IOException
- If an error occurspublic ServerSocket(int port) throws IOException
port
- The port number to bind to
IOException
- If an error occurs
SecurityException
- If a security manager exists and its
checkListen method doesn't allow the operationpublic ServerSocket(int port, int backlog) throws IOException
port
- The port number to bind tobacklog
- The length of the pending connection queue
IOException
- If an error occurs
SecurityException
- If a security manager exists and its
checkListen method doesn't allow the operationpublic ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOException
port
- The port number to bind tobacklog
- The length of the pending connection queuebindAddr
- The address to bind to, or null to bind to all addresses
IOException
- If an error occurs
SecurityException
- If a security manager exists and its
checkListen method doesn't allow the operationMethod Detail |
---|
public void bind(SocketAddress endpoint) throws IOException
endpoint
- The socket address to bind to
IOException
- If an error occurs
IllegalArgumentException
- If address type is not supported
SecurityException
- If a security manager exists and its
checkListen method doesn't allow the operationpublic void bind(SocketAddress endpoint, int backlog) throws IOException
endpoint
- The socket address to bind tobacklog
- The length of the pending connection queue
IOException
- If an error occurs
IllegalArgumentException
- If address type is not supported
SecurityException
- If a security manager exists and its
checkListen method doesn't allow the operationpublic InetAddress getInetAddress()
public int getLocalPort()
public SocketAddress getLocalSocketAddress()
public Socket accept() throws IOException
Socket
instance representing that connection. This method will block until a
connection is available.
IOException
- If an error occurs
SecurityException
- If a security manager exists and its
checkListen method doesn't allow the operation
IllegalBlockingModeException
- If this socket has an associated
channel, and the channel is in non-blocking mode
SocketTimeoutException
- If a timeout was previously set with
setSoTimeout and the timeout has been reachedprotected final void implAccept(Socket socket) throws IOException
ServerSocket.accept()
. The passed in socket will be
connected when this method returns.
socket
- The socket that is used for the accepted connection
IOException
- If an error occurs
IllegalBlockingModeException
- If this socket has an associated
channel, and the channel is in non-blocking modepublic void close() throws IOException
IOException
- If an error occurspublic ServerSocketChannel getChannel()
ServerSocketChannel
object
associated with this socket, if any.
The socket only has a ServerSocketChannel
if its created
by ServerSocketChannel.open()
.
public boolean isBound()
public boolean isClosed()
public void setSoTimeout(int timeout) throws SocketException
timeout
- The new SO_TIMEOUT value
SocketException
- If an error occurspublic int getSoTimeout() throws IOException
IOException
- If an error occurspublic void setReuseAddress(boolean on) throws SocketException
on
- true if SO_REUSEADDR should be enabled, false otherwise
SocketException
- If an error occurspublic boolean getReuseAddress() throws SocketException
SocketException
- If an error occurspublic void setReceiveBufferSize(int size) throws SocketException
size
- The new receive buffer size.
SocketException
- If an error occurs or Socket is not connected
IllegalArgumentException
- If size is 0 or negativepublic int getReceiveBufferSize() throws SocketException
SocketException
- If an error occurs or Socket is not connectedpublic String toString()
String
.
toString
in class Object
String
.Object.getClass()
,
Object.hashCode()
,
Class.getName()
,
Integer.toHexString(int)
public static void setSocketFactory(SocketImplFactory fac) throws IOException
SocketImplFactory
for all
ServerSocket
's. This may only be done
once per virtual machine. Subsequent attempts will generate an
exception. Note that a SecurityManager
check is made prior
to setting the factory. If insufficient privileges exist to set the
factory, an exception will be thrown
fac
- the factory to set
SecurityException
- If this operation is not allowed by the
SecurityManager
.
SocketException
- If the factory object is already defined
IOException
- If any other error occurs
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |