java.net.Socket
This class represents sockets to be used in connection-oriented (streaming)
protocols.
Known Direct Subclasses
Summary
Public Constructors
|
|
|
|
|
|
Socket() |
|
|
|
|
|
|
Socket(Proxy proxy) |
|
|
|
|
|
|
Socket(String dstName, int dstPort) |
|
|
|
|
|
|
Socket(String dstName, int dstPort, InetAddress localAddress, int localPort) |
|
|
|
|
|
|
Socket(String hostName, int port, boolean streaming) |
|
|
|
|
|
|
Socket(InetAddress dstAddress, int dstPort) |
|
|
|
|
|
|
Socket(InetAddress dstAddress, int dstPort, InetAddress localAddress, int localPort) |
|
|
|
|
|
|
Socket(InetAddress addr, int port, boolean streaming) |
Protected Constructors
Public Methods
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait
Details
Public Constructors
public
Socket()
Construct a connection-oriented Socket. The Socket is created in the
factory
if declared, or otherwise of the default type.
public
Socket(Proxy proxy)
Constructs a connection-oriented Socket with specified
proxy
.
Method
checkConnect
is called if a security manager
exists, and the proxy host address and port number are passed as
parameters.
Parameters
proxy
| the specified proxy for this Socket. |
public
Socket(String dstName, int dstPort)
Construct a stream socket connected to the nominated destination
host/port. By default, the socket binds it to any available port on the
default localhost.
Parameters
dstName
| the destination host to connect to |
dstPort
| the port on the destination host to connect to |
public
Socket(String dstName, int dstPort, InetAddress localAddress, int localPort)
Construct a stream socket connected to the nominated destination
host/port. The socket is bound it to the nominated localAddress/port.
Parameters
dstName
| the destination host to connect to |
dstPort
| the port on the destination host to connect to |
localAddress
| the local host address to bind to |
localPort
| the local port to bind to |
public
Socket(String hostName, int port, boolean streaming)
This constructor is deprecated.
As of JDK 1.1, replaced by Socket
Answer a new socket. This constructor is deprecated.
Parameters
hostName
| the host name |
port
| the port on the host |
streaming
| if true, answer a stream socket, else answer a a datagram
socket. |
public
Socket(InetAddress dstAddress, int dstPort)
Construct a stream socket connected to the nominated destination host
address/port. By default, the socket binds it to any available port on
the default localhost.
Parameters
dstAddress
| the destination host address to connect to |
dstPort
| the port on the destination host to connect to |
Throws
IOException
| if an error occurs while instantiating the socket
|
public
Socket(InetAddress dstAddress, int dstPort, InetAddress localAddress, int localPort)
Construct a stream socket connected to the nominated destination host
address/port. The socket is bound it to the nominated localAddress/port.
Parameters
dstAddress
| the destination host address to connect to |
dstPort
| the port on the destination host to connect to |
localAddress
| the local host address to bind to |
localPort
| the local port to bind to |
Throws
IOException
| if an error occurs while instantiating the socket
|
public
Socket(InetAddress addr, int port, boolean streaming)
This constructor is deprecated.
As of JDK 1.1, replaced by Socket
Answer a new socket. This constructor is deprecated.
Parameters
addr
| the internet address |
port
| the port on the host |
streaming
| if true, answer a stream socket, else answer a datagram
socket. |
Protected Constructors
protected
Socket(SocketImpl anImpl)
Creates an unconnected socket, wrapping the
socketImpl
argument.
Parameters
anImpl
| the socket to wrap |
Public Methods
public
void
bind(SocketAddress localAddr)
Bind the Socket to the nominated local host/port.
Parameters
localAddr
| the local machine address and port to bind on |
public
synchronized
void
close()
Close the socket. It is not valid to use the socket thereafter.
public
void
connect(SocketAddress remoteAddr, int timeout)
Connect the Socket to the host/port specified by the SocketAddress with a
specified timeout.
Parameters
remoteAddr
| the remote machine address and port to connect to |
timeout
| the millisecond timeout value, the connect will block
indefinitely for a zero value. |
Throws
IllegalArgumentException
| if the timeout is negative, or the SocketAddress is not
supported |
IOException
| if the socket is already connected, or a problem occurs
during the connect
|
public
void
connect(SocketAddress remoteAddr)
Connect the Socket to the host/port specified by the SocketAddress.
Parameters
remoteAddr
| the remote machine address and port to connect to |
if Socket is created by a SocketChannel, returns the related
SocketChannel
Returns
- the related SocketChannel
public
InetAddress
getInetAddress()
Returns an
InetAddress instance representing the address this
socket has connected to.
Returns
- if this socket is connected, the address it is connected to. A
null
return signifies no connection has been made.
public
InputStream
getInputStream()
Answer the socket input stream, to read byte data off the socket. Note,
multiple input streams may be created on a single socket.
Returns
- a byte oriented read stream for this socket
public
boolean
getKeepAlive()
Answer the SO_KEEPALIVE option for this socket.
Returns
- the socket SO_KEEPALIVE option setting
public
InetAddress
getLocalAddress()
Returns an
InetAddress instance representing the
local
address this socket is bound to.
Returns
- the local address that this socket has bound to
public
int
getLocalPort()
Answer the local port to which the socket is bound.
Returns
- the local port to which the socket is bound
public
SocketAddress
getLocalSocketAddress()
Answer the local SocketAddress for this socket, or null if the socket is
not bound.
This is useful on multihomed hosts.
public
boolean
getOOBInline()
Get the state of the SO_OOBINLINE socket option.
Returns
true
if the SO_OOBINLINE is enabled,
false
otherwise.
public
OutputStream
getOutputStream()
Answer the socket output stream, for writing byte data on the socket.
Note, multiplie output streams may be created on a single socket.
Returns
- OutputStream a byte oriented write stream for this socket
public
int
getPort()
Returns the number of the remote port this socket is connected to.
Returns
- int the remote port number that this socket has connected to. A
return of
0
(zero) indicates that there is no
connection in place.
public
synchronized
int
getReceiveBufferSize()
Answer the socket receive buffer size (SO_RCVBUF).
Returns
- socket receive buffer size
public
SocketAddress
getRemoteSocketAddress()
Answer the remote SocketAddress for this socket, or null if the socket is
not connected.
Returns
- the remote socket address
public
boolean
getReuseAddress()
Get the state of the SO_REUSEADDR socket option.
Returns
true
if the SO_REUSEADDR is enabled,
false
otherwise.
public
synchronized
int
getSendBufferSize()
Answer the socket send buffer size (SO_SNDBUF).
public
int
getSoLinger()
Answer the linger-on-close timeout for this socket (the SO_LINGER value).
Returns
- this socket's SO_LINGER value. A value of
-1
will
be returned if the option is not enabled.
public
synchronized
int
getSoTimeout()
Answer the socket read timeout. The SO_TIMEOUT option, a value of 0
indicates it is disabled and a read operation will block indefinitely
waiting for data.
public
boolean
getTcpNoDelay()
Answer true if the socket is using Nagle's algorithm. The TCP_NODELAY
option setting.
Returns
- the socket TCP_NODELAY option setting
public
int
getTrafficClass()
Get the IP_TOS socket option.
Returns
- the IP_TOS socket option value
public
boolean
isBound()
Return if the socket is bound to a local address and port.
Returns
true
if the socket is bound to a local address,
false
otherwise.
public
boolean
isClosed()
Return if the socket is closed.
Returns
true
if the socket is closed, false
otherwise.
public
boolean
isConnected()
Return if the socket is connected.
Returns
true
if the socket is connected,
false
otherwise.
public
boolean
isInputShutdown()
Return if
shutdownInput() has been called.
Returns
true
if shutdownInput
has been
called, false
otherwise.
public
boolean
isOutputShutdown()
Return if
shutdownOutput() has been called.
Returns
true
if shutdownOutput
has been
called, false
otherwise.
public
void
sendUrgentData(int value)
Send the single byte of urgent data on the socket.
Parameters
value
| the byte of urgent data |
public
void
setKeepAlive(boolean value)
Set the SO_KEEPALIVE option for this socket.
Parameters
value
| the socket SO_KEEPALIVE option setting |
public
void
setOOBInline(boolean oobinline)
Set the SO_OOBINLINE socket option. When this option is enabled, out of
band data is recieved in the normal data stream.
Parameters
oobinline
| the socket SO_OOBINLINE option setting |
public
void
setPerformancePreferences(int connectionTime, int latency, int bandwidth)
sets performance preference for connectionTime,latency and bandwidth
Parameters
connectionTime
| the importance of connect time |
latency
| the importance of latency |
bandwidth
| the importance of bandwidth
|
public
synchronized
void
setReceiveBufferSize(int size)
Set the socket receive buffer size.
Parameters
size
| the buffer size, in bytes |
Throws
SocketException
| tf an error occurs while setting the size or the size is
invalid.
|
public
void
setReuseAddress(boolean reuse)
Set the SO_REUSEADDR socket option.
Parameters
reuse
| the socket SO_REUSEADDR option setting |
public
synchronized
void
setSendBufferSize(int size)
Set the socket send buffer size.
Parameters
size
| the buffer size, in bytes |
Throws
SocketException
| if an error occurs while setting the size or the size is
invalid.
|
public
void
setSoLinger(boolean on, int timeout)
Set the SO_LINGER option, with the specified time, in seconds. The
SO_LINGER option is silently limited to 65535 seconds.
Parameters
on
| if linger is enabled |
timeout
| the linger timeout value, in seconds |
public
synchronized
void
setSoTimeout(int timeout)
Set the read timeout on this socket. The SO_TIMEOUT option, is specified
in milliseconds. The read operation will block indefinitely for a zero
value.
Parameters
timeout
| the read timeout value |
public
static
synchronized
void
setSocketImplFactory(SocketImplFactory fac)
Specifies the application's socket implementation factory. This may only
be executed the once over the lifetime of the application.
Parameters
fac
| the socket factory to set |
public
void
setTcpNoDelay(boolean on)
Set whether the socket is to use Nagle's algorithm. The TCP_NODELAY
option setting.
Parameters
on
| the socket TCP_NODELAY option setting |
public
void
setTrafficClass(int value)
Set the IP_TOS socket option.
Parameters
value
| the socket IP_TOS setting |
public
void
shutdownInput()
Shutdown the input portion of the socket.
public
void
shutdownOutput()
Shutdown the output portion of the socket.
public
String
toString()
Returns a string containing a concise, human-readable description of the
socket.