public class Socket extends Object implements AutoCloseable
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 and Description |
---|
Socket()
Initializes a new instance of
Socket object without
connecting to a remote host. |
Socket(InetAddress address,
int port)
Initializes a new instance of
Socket and connects to the
address and port number specified as arguments. |
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)
Initializes a new instance of
Socket and connects to the
hostname and port 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. |
Modifier and Type | Method and Description |
---|---|
int |
available()
Returns the number of bytes that the caller can read from this socket
without blocking.
|
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.
|
void |
flush() |
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
|
int |
read()
Reads the next byte of data and returns it as an int.
|
int |
read(byte[] buf,
int offset,
int len)
Reads up to len bytes of data into the caller supplied buffer starting
at offset bytes from the start of the buffer
|
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.
|
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 . |
void |
write(byte[] buf,
int offset,
int len)
Writes len number of bytes from the array buf to the stream starting
at offset bytes into the buffer.
|
void |
write(int b)
Writes a byte (passed in as an int) to the given output stream
|
public Socket()
Socket
object without
connecting to a remote host. This useful for subclasses of socket that
might want this behavior.public Socket(InetAddress address, int port) throws IOException
Socket
and connects to the
address and port number specified as arguments.address
- The address to connect toport
- The port number to connect toIOException
- If an error occurspublic Socket(InetAddress address, int port, InetAddress localAddr, int localPort) throws IOException
Socket
and connects to the
address and port number specified as arguments, plus binds to the
specified local address and port.address
- The remote address to connect toport
- The remote port to connect tolocalAddr
- The local address to connect tolocalPort
- The local port to connect toIOException
- If an error occurspublic Socket(String host, int port) throws UnknownHostException, IOException
Socket
and connects to the
hostname and port specified as arguments.host
- The name of the host to connect toport
- The port number to connect toUnknownHostException
- If the hostname cannot be resolved to a
network address.IOException
- If an error occurspublic Socket(String host, int port, InetAddress localAddr, int localPort) throws IOException
Socket
that connects to the
named host on the specified port and binds to the specified local address
and port.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.IOException
- If a connection error occurs.public int available() throws IOException
IOException
- if an error occurspublic void bind(SocketAddress bindpoint) throws IOException
bindpoint
- The address/port to bind toIOException
- If an error occursIllegalArgumentException
- If the address type is not supportedpublic void close() throws IOException
close
in interface AutoCloseable
IOException
- If an error occurspublic void connect(SocketAddress endpoint) throws IOException
endpoint
- The address to connect toIOException
- If an error occursIllegalArgumentException
- If the addess type is not supportedpublic void connect(SocketAddress endpoint, int timeout) throws IOException
endpoint
- The address to connect totimeout
- The length of the timeout in milliseconds, or
0 to indicate no timeout.IOException
- If an error occursIllegalArgumentException
- If the address type is not supportedSocketTimeoutException
- If the timeout is reachedpublic void flush() throws IOException
IOException
public InetAddress getInetAddress()
null
is returned.public InputStream getInputStream() throws IOException
IOException
- If an error occurs or Socket is not connectedpublic boolean getKeepAlive() throws SocketException
SocketException
- If an error occurs or Socket is not connectedpublic InetAddress getLocalAddress()
is true
, is returned.
public int getLocalPort()
public SocketAddress getLocalSocketAddress()
public boolean getOOBInline() throws SocketException
SocketException
- If an error occurspublic OutputStream getOutputStream() throws IOException
IOException
- If an error occurs or Socket is not connectedpublic int getPort()
public int getReceiveBufferSize() throws SocketException
SocketException
- If an error occurs or Socket is not connectedpublic SocketAddress getRemoteSocketAddress()
public boolean getReuseAddress() throws SocketException
SocketException
- If an error occurspublic int getSendBufferSize() throws SocketException
SocketException
- If an error occurs or socket not connectedpublic int getSoLinger() throws SocketException
SocketException
- If an error occurs or Socket is not connectedpublic int getSoTimeout() throws SocketException
SocketException
- If an error occurs or Socket not connectedpublic boolean getTcpNoDelay() throws SocketException
SocketException
- If an error occurs or Socket not connectedpublic int getTrafficClass() throws SocketException
SocketException
- If an error occurssetTrafficClass(int tc)
public boolean isBound()
public boolean isClosed()
public boolean isConnected()
public boolean isInputShutdown()
public boolean isOutputShutdown()
public int read() throws IOException
IOException
- If an error occurs.public int read(byte[] buf, int offset, int len) throws IOException
buf
- The bufferoffset
- Offset into the buffer to start reading fromlen
- The number of bytes to readIOException
- If an error occurs.public void sendUrgentData(int data) throws IOException
data
- The data to send.
Only the lowest eight bits of data are sentIOException
- If an error occurspublic void setKeepAlive(boolean on) throws SocketException
on
- True if SO_KEEPALIVE should be enabledSocketException
- If an error occurs or Socket is not connectedpublic void setOOBInline(boolean on) throws SocketException
on
- True if SO_OOBLINE should be enabledSocketException
- 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 connectedIllegalArgumentException
- If size is 0 or negativepublic void setReuseAddress(boolean reuseAddress) throws SocketException
reuseAddress
- true if SO_REUSEADDR should be enabled,
false otherwiseSocketException
- If an error occurspublic void setSendBufferSize(int size) throws SocketException
size
- The new send buffer size.SocketException
- If an error occurs or Socket not connectedIllegalArgumentException
- If size is 0 or negativepublic void setSoLinger(boolean on, int linger) throws SocketException
on
- true to enable SO_LINGER, false to disablelinger
- The SO_LINGER timeout in hundreths of a second or -1 if
SO_LINGER not set.SocketException
- If an error occurs or Socket not connectedIllegalArgumentException
- If linger is negativepublic void setSoTimeout(int timeout) throws SocketException
timeout
- The length of the timeout in milliseconds, or
0 to indicate no timeout.SocketException
- If an error occurs or Socket not connectedpublic void setTcpNoDelay(boolean on) throws SocketException
on
- true to enable, false to disableSocketException
- If an error occurs or Socket is not connectedpublic void setTrafficClass(int tc) throws SocketException
tc
- The traffic classSocketException
- If an error occursIllegalArgumentException
- If tc value is illegalgetTrafficClass()
public void shutdownInput() throws IOException
IOException
- If an error occurs.public void shutdownOutput() throws IOException
IOException
- If an error occurs.public String toString()
Socket
to a String
.public void write(byte[] buf, int offset, int len) throws IOException
buf
- The bufferoffset
- Offset into the buffer to start writing fromlen
- The number of bytes to writeIOException
- If an error occurs.public void write(int b) throws IOException
b
- The byte to writeIOException
- If an error occurs