java.net
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.
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.
|
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
|
void | setSoTimeout(int timeout) - Sets the value of SO_TIMEOUT.
|
static void | setSocketFactory(SocketImplFactory fac) - Sets the
SocketImplFactory for all
ServerSocket 's.
|
String | toString() - Returns the value of this socket as a
String .
|
clone , equals , extends Object> getClass , finalize , hashCode , notify , notifyAll , toString , wait , wait , wait |
ServerSocket
public ServerSocket(int port)
throws IOException
Creates a server socket and binds it to the specified port. If the
port number is 0, a random free port will be chosen. The pending
connection queue on this socket will be set to 50.
port
- The port number to bind to
ServerSocket
public ServerSocket(int port,
int backlog)
throws IOException
Creates a server socket and binds it to the specified port. If the
port number is 0, a random free port will be chosen. The pending
connection queue on this socket will be set to the value passed as
arg2.
port
- The port number to bind tobacklog
- The length of the pending connection queue
ServerSocket
public ServerSocket(int port,
int backlog,
InetAddress bindAddr)
throws IOException
Creates a server socket and binds it to the specified port. If the
port number is 0, a random free port will be chosen. The pending
connection queue on this socket will be set to the value passed as
backlog. The third argument specifies a particular local address to
bind t or null to bind to all local address.
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
accept
public Socket accept()
throws IOException
Accepts a new connection and returns a connected Socket
instance representing that connection. This method will block until a
connection is available.
- socket object for the just accepted connection
bind
public void bind(SocketAddress endpoint)
throws IOException
Binds the server socket to a specified socket address
endpoint
- The socket address to bind to
bind
public void bind(SocketAddress endpoint,
int backlog)
throws IOException
Binds the server socket to a specified socket address
endpoint
- The socket address to bind tobacklog
- The length of the pending connection queue
close
public void close()
throws IOException
Closes this socket and stops listening for connections
getChannel
public ServerSocketChannel getChannel()
Returns the unique
ServerSocketChannel
object
associated with this socket, if any.
The socket only has a
ServerSocketChannel
if its created
by
ServerSocketChannel.open()
.
- the associated socket channel, null if none exists
getInetAddress
public InetAddress getInetAddress()
This method returns the local address to which this socket is bound
- The socket's local address
getLocalPort
public int getLocalPort()
This method returns the local port number to which this socket is bound
getReceiveBufferSize
public int getReceiveBufferSize()
throws SocketException
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.
getReuseAddress
public boolean getReuseAddress()
throws SocketException
Checks if the SO_REUSEADDR option is enabled
- true if SO_REUSEADDR is set, false otherwise
getSoTimeout
public int getSoTimeout()
throws IOException
Retrieves the current value of the SO_TIMEOUT setting. A value of 0
implies that SO_TIMEOUT is disabled (ie, operations never time out).
This is the number of milliseconds a socket operation can block before
an InterruptedIOException is thrown.
implAccept
protected final void implAccept(Socket socket)
throws IOException
This protected method is used to help subclasses override
ServerSocket.accept()
. The passed in socket will be
connected when this method returns.
socket
- The socket that is used for the accepted connection
isBound
public boolean isBound()
Returns true when the socket is bound, otherwise false
- true if socket is bound, false otherwise
isClosed
public boolean isClosed()
Returns true if the socket is closed, otherwise false
- true if socket is closed, false otherwise
setReceiveBufferSize
public void setReceiveBufferSize(int size)
throws SocketException
This method sets the value for the system level socket option
SO_RCVBUF to the specified value. Note that valid values for this
option are specific to a given operating system.
size
- The new receive buffer size.
setReuseAddress
public void setReuseAddress(boolean on)
throws SocketException
Enables/Disables the SO_REUSEADDR option
on
- true if SO_REUSEADDR should be enabled, false otherwise
setSoTimeout
public void setSoTimeout(int timeout)
throws SocketException
Sets the value of SO_TIMEOUT. A value of 0 implies that SO_TIMEOUT is
disabled (ie, operations never time out). This is the number of
milliseconds a socket operation can block before an
InterruptedIOException is thrown.
timeout
- The new SO_TIMEOUT value
setSocketFactory
public static void setSocketFactory(SocketImplFactory fac)
throws IOException
Sets the 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
toString
public String toString()
Returns the value of this socket as a String
.
- toString in interface Object
- This socket represented as a
String
.
ServerSocket.java -- Class for implementing server side sockets
Copyright (C) 1998, 1999, 2000, 2002, 2003, 2004, 2006
Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.