java.net
Class Socket
java.lang.Object
|
+--java.net.Socket
public class
Socketextends
Object This class models a client site socket. A socket is a TCP/IP endpoint
for network communications conceptually similar to a file handle.
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.
Authors:- Aaron M. Renn (arenn@urbanophile.com)
- Per Bothner (bothner@cygnus.com)
Socket()
|
Socket(java.net.SocketImpl impl)
|
Socket(java.lang.String host, int port)
|
Socket(java.net.InetAddress address, int port)
|
Socket(java.lang.String host, int port, java.net.InetAddress localAddr, int localPort)
|
Socket(java.net.InetAddress address, int port, java.net.InetAddress localAddr, int localPort)
|
Socket(java.lang.String host, int port, boolean stream)
|
Socket(java.net.InetAddress host, int port, boolean stream)
|
Socket
public Socket()
Initializes a new instance of Socket
object without
connecting to a remote host. This useful for subclasses of socket that
might want this behavior.
Since:
Socket
public Socket(java.lang.String host, int port)
Initializes a new instance of Socket
and connects to the
hostname and port specified as arguments.
Parameters:
Throws:
Socket
public Socket(java.lang.String host, int port, boolean stream)
Initializes a new instance of 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:
Throws:
Socket
public Socket(java.lang.String host, int port, java.net.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.
Since:Parameters:
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.
Socket
public Socket(java.net.InetAddress address, int port)
Initializes a new instance of Socket
and connects to the
address and port number specified as arguments.
Parameters:
Throws:
Socket
public Socket(java.net.InetAddress host, int port, boolean stream)
Initializes a new instance of 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:
Throws:
Socket
public Socket(java.net.InetAddress address, int port, java.net.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.
Since:Parameters:
Throws:
Socket
protected Socket(java.net.SocketImpl impl)
Initializes a new instance of
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. This value can be null
, but if it is, all instance
methods in Socket
should be overridden because most of them
rely on this value being populated.
Since:Parameters:
Throws:
bind
public void bind(java.net.SocketAddress bindpoint)
Binds the socket to the givent local address/port
Since:Parameters:
Throws:
close
public synchronized void close()
Closes the socket.
Throws:
connect
public void connect(java.net.SocketAddress endpoint)
Connects the socket with a remote address.
Since:Parameters:
Throws:
connect
public void connect(java.net.SocketAddress endpoint, int timeout)
Connects the socket with a remote address. A timeout of zero is
interpreted as an infinite timeout. The connection will then block
until established or an error occurs.
Since:Parameters:
Throws:
getChannel
public SocketChannel getChannel()
Returns the socket channel associated with this socket.
It returns null if no associated socket exists.
Since:
getInetAddress
public InetAddress getInetAddress()
Returns the address of the remote end of the socket. If this socket
is not connected, then null
is returned.
Returns:
- The remote address this socket is connected to
getInputStream
public InputStream getInputStream()
Returns an InputStream for reading from this socket.
Returns:
Throws:
IOException
- If an error occurs or Socket is not connected
getKeepAlive
public boolean getKeepAlive()
This method returns the value of the socket level socket option
SO_KEEPALIVE.
Since:Returns:
Throws:
getLocalAddress
public InetAddress getLocalAddress()
Returns the local address to which this socket is bound. If this socket
is not connected, then null
is returned.
Since:Returns:
getLocalPort
public int getLocalPort()
Returns the local port number to which this socket is bound. If this
socket is not connected, then -1 is returned.
Returns:
getLocalSocketAddress
public SocketAddress getLocalSocketAddress()
If the socket is already bound this returns the local SocketAddress,
otherwise null
Since:
getOOBInline
public boolean getOOBInline()
Returns the current setting of the SO_OOBINLINE option for this socket
Since:Throws:
getOutputStream
public OutputStream getOutputStream()
Returns an OutputStream for writing to this socket.
Returns:
Throws:
IOException
- If an error occurs or Socket is not connected
getPort
public int getPort()
Returns the port number of the remote end of the socket connection. If
this socket is not connected, then -1 is returned.
Returns:
- The remote port this socket is connected to
getReceiveBufferSize
public 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.
Since:Returns:
Throws:
getRemoteSocketAddress
public SocketAddress getRemoteSocketAddress()
If the socket is already connected this returns the remote SocketAddress,
otherwise null
Since:
getReuseAddress
public boolean getReuseAddress()
Checks if the SO_REUSEADDR option is enabled
Since:Throws:
getSendBufferSize
public 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.
Since:Returns:
Throws:
getSoLinger
public int getSoLinger()
Returns the value of the SO_LINGER option on the socket. If the
SO_LINGER option is set on a socket and there is still data waiting to
be sent when the socket is closed, then the close operation will block
until either that data is delivered or until the timeout period
expires. This method either returns the timeouts (in hundredths of
of a second (platform specific?)) if SO_LINGER is set, or -1 if
SO_LINGER is not set.
Since:Returns:
- The SO_LINGER timeout in hundreths of a second or -1
if SO_LINGER not set
Throws:
getSoTimeout
public synchronized int getSoTimeout()
Returns the value of the SO_TIMEOUT option on the socket. If this value
is set, and an read/write is performed that does not complete within
the timeout period, a short count is returned (or an EWOULDBLOCK signal
would be sent in Unix if no data had been read). A value of 0 for
this option implies that there is no timeout (ie, operations will
block forever). On systems that have separate read and write timeout
values, this method returns the read timeout. This
value is in thousandths of a second (implementation specific?).
Since:Returns:
- The length of the timeout in thousandth's of a second or 0
if not set
Throws:
getTcpNoDelay
public boolean getTcpNoDelay()
Tests whether or not the TCP_NODELAY option is set on the socket.
Returns true if enabled, false if disabled. When on it disables the
Nagle algorithm which means that packets are always send immediatly and
never merged together to reduce network trafic.
Since:Returns:
- Whether or not TCP_NODELAY is set
Throws:
getTrafficClass
public int getTrafficClass()
Returns the current traffic class
Since:Throws:
See Also:
isBound
public boolean isBound()
Checks if the socket is already bound.
Since:
isClosed
public boolean isClosed()
Checks if the socket is closed.
Since:
isConnected
public boolean isConnected()
Checks if the socket is connected
Since:
isInputShutdown
public boolean isInputShutdown()
Checks if the socket's input stream is shutdown
Since:
isOutputShutdown
public boolean isOutputShutdown()
Checks if the socket's output stream is shutdown
Since:
sendUrgentData
public void sendUrgentData(int data)
Sends urgent data through the socket
Since:Parameters:
Throws:
setKeepAlive
public void setKeepAlive(boolean on)
This method sets the value for the socket level socket option
SO_KEEPALIVE.
Since:Parameters:
Throws:
setOOBInline
public void setOOBInline(boolean on)
Enables/disables the SO_OOBINLINE option
Since:Parameters:
Throws:
setReceiveBufferSize
public void setReceiveBufferSize(int size)
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.
Since:Parameters:
Throws:
setReuseAddress
public void setReuseAddress(boolean on)
Enables/Disables the SO_REUSEADDR option
Since:Parameters:
Throws:
setSendBufferSize
public void setSendBufferSize(int size)
This method sets the value for the system level socket option
SO_SNDBUF to the specified value. Note that valid values for this
option are specific to a given operating system.
Since:Parameters:
Throws:
setSoLinger
public void setSoLinger(boolean on, int linger)
Sets the value of the SO_LINGER option on the socket. If the
SO_LINGER option is set on a socket and there is still data waiting to
be sent when the socket is closed, then the close operation will block
until either that data is delivered or until the timeout period
expires. The linger interval is specified in hundreths of a second
(platform specific?)
Since:Parameters:
Throws:
setSoTimeout
public synchronized void setSoTimeout(int timeout)
Sets the value of the SO_TIMEOUT option on the socket. If this value
is set, and an read/write is performed that does not complete within
the timeout period, a short count is returned (or an EWOULDBLOCK signal
would be sent in Unix if no data had been read). A value of 0 for
this option implies that there is no timeout (ie, operations will
block forever). On systems that have separate read and write timeout
values, this method returns the read timeout. This
value is in thousandths of a second.
Since:Parameters:
Throws:
setSocketImplFactory
public static synchronized void setSocketImplFactory(java.net.SocketImplFactory fac)
Sets the 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:
Throws:
setTcpNoDelay
public void setTcpNoDelay(boolean on)
Sets the TCP_NODELAY option on the socket.
Since:Parameters:
Throws:
setTrafficClass
public void setTrafficClass(int tc)
Sets the traffic class value
Since:Parameters:
Throws:
See Also:
shutdownInput
public void shutdownInput()
Closes the input side of the socket stream.
Since:Throws:
shutdownOutput
public void shutdownOutput()
Closes the output side of the socket stream.
Since:Throws:
toString
public String toString()
Converts this Socket
to a String
.
Returns:
- The
String
representation of this Socket
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 theSocketImplFactory interface
. A default factory is provided, however the factory may be set by a call to thesetSocketImplFactory
method. Note that this may only be done once per virtual machine. If a subsequent attempt is made to set the factory, aSocketException
will be thrown.