java.net
public class Socket extends Object
This class does not actually do any work. Instead, it redirects all of
its calls to a socket implementation object which implements the
SocketImpl
interface. The implementation class is
instantiated by factory class that implements the
SocketImplFactory interface
. A default
factory is provided, however the factory may be set by a call to
the setSocketImplFactory
method. Note that this may only be
done once per virtual machine. If a subsequent attempt is made to set the
factory, a SocketException
will be thrown.
Constructor Summary | |
---|---|
Socket()
Initializes a new instance of Socket object without
connecting to a remote host. | |
protected | Socket(SocketImpl impl)
Initializes a new instance of Socket object without
connecting to a remote host. |
Socket(String host, int port)
Initializes a new instance of Socket and connects to the
hostname and port specified as arguments.
| |
Socket(InetAddress address, int port)
Initializes a new instance of Socket and connects to the
address and port number specified as arguments.
| |
Socket(String host, int port, InetAddress localAddr, int localPort)
Initializes a new instance of Socket that connects to the
named host on the specified port and binds to the specified local address
and port.
| |
Socket(InetAddress address, int port, InetAddress localAddr, int localPort)
Initializes a new instance of Socket and connects to the
address and port number specified as arguments, plus binds to the
specified local address and port.
| |
Socket(String host, int port, boolean stream)
Initializes a new instance of Socket and connects to the
hostname and port specified as arguments. | |
Socket(InetAddress host, int port, boolean stream)
Initializes a new instance of Socket and connects to the
address and port number specified as arguments. |
Method Summary | |
---|---|
void | bind(SocketAddress bindpoint)
Binds the socket to the given local address/port
|
void | close()
Closes the socket.
|
void | connect(SocketAddress endpoint)
Connects the socket with a remote address.
|
void | connect(SocketAddress endpoint, int timeout)
Connects the socket with a remote address. |
SocketChannel | getChannel()
Returns the socket channel associated with this socket.
|
InetAddress | getInetAddress()
Returns the address of the remote end of the socket. |
InputStream | getInputStream()
Returns an InputStream for reading from this socket.
|
boolean | getKeepAlive()
This method returns the value of the socket level socket option
SO_KEEPALIVE.
|
InetAddress | getLocalAddress()
Returns the local address to which this socket is bound. |
int | getLocalPort()
Returns the local port number to which this socket is bound. |
SocketAddress | getLocalSocketAddress()
Returns local socket address.
|
boolean | getOOBInline()
Returns the current setting of the SO_OOBINLINE option for this socket
|
OutputStream | getOutputStream()
Returns an OutputStream for writing to this socket.
|
int | getPort()
Returns the port number of the remote end of the socket connection. |
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.
|
SocketAddress | getRemoteSocketAddress()
Returns the remote socket address.
|
boolean | getReuseAddress()
Checks if the SO_REUSEADDR option is enabled
|
int | getSendBufferSize()
This method returns the value of the system level socket option
SO_SNDBUF, which is used by the operating system to tune buffer
sizes for data transfers.
|
int | getSoLinger()
Returns the value of the SO_LINGER option on the socket. |
int | getSoTimeout()
Returns the value of the SO_TIMEOUT option on the socket. |
boolean | getTcpNoDelay()
Tests whether or not the TCP_NODELAY option is set on the socket.
|
int | getTrafficClass()
Returns the current traffic class
|
boolean | isBound()
Checks if the socket is already bound.
|
boolean | isClosed()
Checks if the socket is closed.
|
boolean | isConnected()
Checks if the socket is connected
|
boolean | isInputShutdown()
Checks if the socket's input stream is shutdown
|
boolean | isOutputShutdown()
Checks if the socket's output stream is shutdown
|
void | sendUrgentData(int data)
Sends urgent data through the socket
|
void | setKeepAlive(boolean on)
This method sets the value for the socket level socket option
SO_KEEPALIVE.
|
void | setOOBInline(boolean on)
Enables/disables the SO_OOBINLINE option
|
void | setReceiveBufferSize(int size)
This method sets the value for the system level socket option
SO_RCVBUF to the specified value. |
void | setReuseAddress(boolean reuseAddress)
Enables/Disables the SO_REUSEADDR option
|
void | setSendBufferSize(int size)
This method sets the value for the system level socket option
SO_SNDBUF to the specified value. |
static void | setSocketImplFactory(SocketImplFactory fac)
Sets the SocketImplFactory . |
void | setSoLinger(boolean on, int linger)
Sets the value of the SO_LINGER option on the socket. |
void | setSoTimeout(int timeout)
Sets the value of the SO_TIMEOUT option on the socket. |
void | setTcpNoDelay(boolean on)
Sets the TCP_NODELAY option on the socket.
|
void | setTrafficClass(int tc)
Sets the traffic class value
|
void | shutdownInput()
Closes the input side of the socket stream.
|
void | shutdownOutput()
Closes the output side of the socket stream.
|
String | toString()
Converts this Socket to a String .
|
Socket
object without
connecting to a remote host. This useful for subclasses of socket that
might want this behavior.
Since: 1.1
UNKNOWN: This constructor is public since JDK 1.4
Socket
object without
connecting to a remote host. This is useful for subclasses of socket
that might want this behavior.
Additionally, this socket will be created using the supplied
implementation class instead the default class or one returned by a
factory. If this value is null
, the default Socket
implementation is used.
Parameters: impl The SocketImpl
to use for this
Socket
Throws: SocketException If an error occurs
Since: 1.1
Socket
and connects to the
hostname and port specified as arguments.
Parameters: host The name of the host to connect to port The port number to connect to
Throws: UnknownHostException If the hostname cannot be resolved to a network address. IOException If an error occurs SecurityException If a security manager exists and its checkConnect method doesn't allow the operation
Socket
and connects to the
address and port number specified as arguments.
Parameters: address The address to connect to port The port number to connect to
Throws: IOException If an error occurs SecurityException If a security manager exists and its checkConnect method doesn't allow the operation
Socket
that connects to the
named host on the specified port and binds to the specified local address
and port.
Parameters: host The name of the remote host to connect to. port The remote port to connect to. localAddr The local address to bind to. localPort The local port to bind to.
Throws: SecurityException If the SecurityManager
exists and does not allow a connection to the specified host/port or
binding to the specified local host/port. IOException If a connection error occurs.
Since: 1.1
Socket
and connects to the
address and port number specified as arguments, plus binds to the
specified local address and port.
Parameters: address The remote address to connect to port The remote port to connect to localAddr The local address to connect to localPort The local port to connect to
Throws: IOException If an error occurs SecurityException If a security manager exists and its checkConnect method doesn't allow the operation
Since: 1.1
Deprecated: Use the DatagramSocket
class to create
datagram oriented sockets.
Socket
and connects to the
hostname and port specified as arguments. If the stream argument is set
to true
, then a stream socket is created. If it is
false
, a datagram socket is created.
Parameters: host The name of the host to connect to port The port to connect to stream true
for a stream socket, false
for a datagram socket
Throws: IOException If an error occurs SecurityException If a security manager exists and its checkConnect method doesn't allow the operation
Deprecated: Use the DatagramSocket
class to create
datagram oriented sockets.
Socket
and connects to the
address and port number specified as arguments. If the stream param is
true
, a stream socket will be created, otherwise a datagram
socket is created.
Parameters: host The address to connect to port The port number to connect to stream true
to create a stream socket,
false
to create a datagram socket.
Throws: IOException If an error occurs SecurityException If a security manager exists and its checkConnect method doesn't allow the operation
Parameters: bindpoint The address/port to bind to
Throws: IOException If an error occurs SecurityException If a security manager exists and its checkConnect method doesn't allow the operation IllegalArgumentException If the address type is not supported
Since: 1.4
Throws: IOException If an error occurs
Parameters: endpoint The address to connect to
Throws: IOException If an error occurs IllegalArgumentException If the addess type is not supported IllegalBlockingModeException If this socket has an associated channel, and the channel is in non-blocking mode
Since: 1.4
Parameters: endpoint The address to connect to timeout The length of the timeout in milliseconds, or 0 to indicate no timeout.
Throws: IOException If an error occurs IllegalArgumentException If the address type is not supported IllegalBlockingModeException If this socket has an associated channel, and the channel is in non-blocking mode SocketTimeoutException If the timeout is reached
Since: 1.4
Returns: the associated socket channel, null if no associated channel exists
Since: 1.4
null
is returned.
Returns: The remote address this socket is connected to
Returns: The InputStream object
Throws: IOException If an error occurs or Socket is not connected
Returns: The setting
Throws: SocketException If an error occurs or Socket is not connected
Since: 1.3
Returns: The local address
Since: 1.1
See Also: is
true
, is returned.
Returns: The local port
Returns: the local socket address, null if not bound
Since: 1.4
Returns: True if SO_OOBINLINE is set, false otherwise.
Throws: SocketException If an error occurs
Since: 1.4
Returns: The OutputStream object
Throws: IOException If an error occurs or Socket is not connected
Returns: The remote port this socket is connected to
Returns: The receive buffer size.
Throws: SocketException If an error occurs or Socket is not connected
Since: 1.2
Returns: the remote socket address, null of not connected
Since: 1.4
Returns: True if SO_REUSEADDR is set, false otherwise.
Throws: SocketException If an error occurs
Since: 1.4
Returns: The send buffer size.
Throws: SocketException If an error occurs or socket not connected
Since: 1.2
Returns: The SO_LINGER timeout in hundreths of a second or -1 if SO_LINGER not set
Throws: SocketException If an error occurs or Socket is not connected
Since: 1.1
Returns: The length of the timeout in thousandth's of a second or 0 if not set
Throws: SocketException If an error occurs or Socket not connected
Since: 1.1
Returns: Whether or not TCP_NODELAY is set
Throws: SocketException If an error occurs or Socket not connected
Since: 1.1
Returns: The current traffic class.
Throws: SocketException If an error occurs
Since: 1.4
Returns: True if socket is bound, false otherwise.
Since: 1.4
Returns: True if socket is closed, false otherwise.
Since: 1.4
Returns: True if socket is connected, false otherwise.
Since: 1.4
Returns: True if input is shut down.
Since: 1.4
Returns: True if output is shut down.
Since: 1.4
Parameters: data The data to send. Only the lowest eight bits of data are sent
Throws: IOException If an error occurs
Since: 1.4
Parameters: on True if SO_KEEPALIVE should be enabled
Throws: SocketException If an error occurs or Socket is not connected
Since: 1.3
Parameters: on True if SO_OOBLINE should be enabled
Throws: SocketException If an error occurs
Since: 1.4
Parameters: size The new receive buffer size.
Throws: SocketException If an error occurs or Socket is not connected IllegalArgumentException If size is 0 or negative
Since: 1.2
Parameters: reuseAddress true if SO_REUSEADDR should be enabled, false otherwise
Throws: SocketException If an error occurs
Since: 1.4
Parameters: size The new send buffer size.
Throws: SocketException If an error occurs or Socket not connected IllegalArgumentException If size is 0 or negative
Since: 1.2
SocketImplFactory
. This may be done only once per
virtual machine. Subsequent attempts will generate a
SocketException
. Note that a SecurityManager
check is made prior to setting the factory. If
insufficient privileges exist to set the factory, then an
IOException
will be thrown.
Parameters: fac the factory to set
Throws: SecurityException If the SecurityManager
does
not allow this operation. SocketException If the SocketImplFactory is already defined IOException If any other error occurs
Parameters: on true to enable SO_LINGER, false to disable linger The SO_LINGER timeout in hundreths of a second or -1 if SO_LINGER not set.
Throws: SocketException If an error occurs or Socket not connected IllegalArgumentException If linger is negative
Since: 1.1
Parameters: timeout The length of the timeout in milliseconds, or 0 to indicate no timeout.
Throws: SocketException If an error occurs or Socket not connected
Since: 1.1
Parameters: on true to enable, false to disable
Throws: SocketException If an error occurs or Socket is not connected
Since: 1.1
Parameters: tc The traffic class
Throws: SocketException If an error occurs IllegalArgumentException If tc value is illegal
Since: 1.4
Throws: IOException If an error occurs.
Since: 1.3
Throws: IOException If an error occurs.
Since: 1.3
Socket
to a String
.
Returns: The String
representation of this Socket