The actual work of the socket is performed by an instance of the SocketImpl
class. An application, by changing the socket factory that creates the socket implementation, can configure itself to create sockets appropriate to the local firewall.
Field Detail |
SocketImpl impl
private static SocketImplFactory factory
Constructor Detail |
protected Socket()
protected Socket(SocketImpl impl)
impl
- an instance of a SocketImpl the subclass wishes to use on the Socket.SocketException
- if there is an error in the underlying protocol, such as a TCP error.public Socket(String host, int port)
If the application has specified a server socket factory, that factory's createSocketImpl
method is called to create the actual socket implementation. Otherwise a "plain" socket is created.
If there is a security manager, its checkConnect
method is called with the host address and port
as its arguments. This could result in a SecurityException.
host
- the host name.port
- the port number.UnknownHostException
- if the IP address of the host could not be determined.IOException
- if an I/O error occurs when creating the socket.SecurityException
- if a security manager exists and its checkConnect
method doesn't allow the operation.public Socket(InetAddress address, int port)
If the application has specified a socket factory, that factory's createSocketImpl
method is called to create the actual socket implementation. Otherwise a "plain" socket is created.
If there is a security manager, its checkConnect
method is called with the host address and port
as its arguments. This could result in a SecurityException.
address
- the IP address.port
- the port number.IOException
- if an I/O error occurs when creating the socket.SecurityException
- if a security manager exists and its checkConnect
method doesn't allow the operation.public Socket(String host, int port, InetAddress localAddr, int localPort)
If there is a security manager, its checkConnect
method is called with the host address and port
as its arguments. This could result in a SecurityException.
host
- the name of the remote hostport
- the remote portlocalAddr
- the local address the socket is bound tolocalPort
- the local port the socket is bound toIOException
- if an I/O error occurs when creating the socket.SecurityException
- if a security manager exists and its checkConnect
method doesn't allow the operation.public Socket(InetAddress address, int port, InetAddress localAddr, int localPort)
If there is a security manager, its checkConnect
method is called with the host address and port
as its arguments. This could result in a SecurityException.
address
- the remote addressport
- the remote portlocalAddr
- the local address the socket is bound tolocalPort
- the local port the socket is bound toIOException
- if an I/O error occurs when creating the socket.SecurityException
- if a security manager exists and its checkConnect
method doesn't allow the operation.public Socket(String host, int port, boolean stream)
If the stream argument is true
, this creates a stream socket. If the stream argument is false
, it creates a datagram socket.
If the application has specified a server socket factory, that factory's createSocketImpl
method is called to create the actual socket implementation. Otherwise a "plain" socket is created.
If there is a security manager, its checkConnect
method is called with the host address and port
as its arguments. This could result in a SecurityException.
host
- the host name.port
- the port number.stream
- a boolean
indicating whether this is a stream socket or a datagram socket.IOException
- if an I/O error occurs when creating the socket.SecurityException
- if a security manager exists and its checkConnect
method doesn't allow the operation.public Socket(InetAddress host, int port, boolean stream)
If the stream argument is true
, this creates a stream socket. If the stream argument is false
, it creates a datagram socket.
If the application has specified a server socket factory, that factory's createSocketImpl
method is called to create the actual socket implementation. Otherwise a "plain" socket is created.
If there is a security manager, its checkConnect
method is called with host.getHostAddress()
and port
as its arguments. This could result in a SecurityException.
host
- the IP address.port
- the port number.stream
- if true
, create a stream socket; otherwise, create a datagram socket.IOException
- if an I/O error occurs when creating the socket.SecurityException
- if a security manager exists and its checkConnect
method doesn't allow the operation.Method Detail |
public InetAddress getInetAddress()
public InetAddress getLocalAddress()
public int getPort()
public int getLocalPort()
public InputStream getInputStream()
IOException
- if an I/O error occurs when creating the input stream.public OutputStream getOutputStream()
IOException
- if an I/O error occurs when creating the output stream.public void setTcpNoDelay(boolean on)
on
- true
to enable TCP_NODELAY, false to disable.
SocketException
- if there is an error in the underlying protocol, such as a TCP error.public boolean getTcpNoDelay()
boolean
indicating whether or not TCP_NODELAY is enabled.SocketException
- if there is an error in the underlying protocol, such as a TCP error.public void setSoLinger(boolean on, int linger)
on
- whether or not to linger on.linger
- how to linger for, if on is true.SocketException
- if there is an error in the underlying protocol, such as a TCP error.IllegalArgumentException
- if the linger value is negative.public int getSoLinger()
SocketException
- if there is an error in the underlying protocol, such as a TCP error.public synchronized void setSoTimeout(int timeout)
timeout
- the specified timeout, in milliseconds.SocketException
- if there is an error in the underlying protocol, such as a TCP error.public synchronized int getSoTimeout()
SocketException
- if there is an error in the underlying protocol, such as a TCP error.public synchronized void setSendBufferSize(int size)
Increasing buffer size can increase the performance of network I/O for high-volume connection, while decreasing it can help reduce the backlog of incoming data. For UDP, this sets the maximum size of a packet that may be sent on this Socket.
Because SO_SNDBUF is a hint, applications that want to verify what size the buffers were set to should call {@link #getSendBufferSize()}.
size
- the size to which to set the send buffer size. This value must be greater than 0.SocketException
- if there is an error in the underlying protocol, such as a TCP error.IllegalArgumentException
- if the value is 0 or is negative.public synchronized int getSendBufferSize()
SocketException
- if there is an error in the underlying protocol, such as a TCP error.public synchronized void setReceiveBufferSize(int size)
Increasing buffer size can increase the performance of network I/O for high-volume connection, while decreasing it can help reduce the backlog of incoming data. For UDP, this sets the maximum size of a packet that may be sent on this Socket.
Because SO_RCVBUF is a hint, applications that want to verify what size the buffers were set to should call {@link #getReceiveBufferSize()}.
size
- the size to which to set the receive buffer size. This value must be greater than 0.IllegalArgumentException
- if the value is 0 or is negative.SocketException
- if there is an error in the underlying protocol, such as a TCP error.public synchronized int getReceiveBufferSize()
SocketException
- if there is an error in the underlying protocol, such as a TCP error.public void setKeepAlive(boolean on)
on
- whether or not to have socket keep alive turned on.SocketException
- if there is an error in the underlying protocol, such as a TCP error.public boolean getKeepAlive()
boolean
indicating whether or not SO_KEEPALIVE is enabled.SocketException
- if there is an error in the underlying protocol, such as a TCP error.public synchronized void close()
IOException
- if an I/O error occurs when closing this socket.public void shutdownInput()
IOException
- if an I/O error occurs when shutting down this socket.public void shutdownOutput()
IOException
- if an I/O error occurs when shutting down this socket.public String toString()
String
.public static synchronized void setSocketImplFactory(SocketImplFactory fac)
When an application creates a new client socket, the socket implementation factory's createSocketImpl
method is called to create the actual socket implementation.
If there is a security manager, this method first calls the security manager's checkSetFactory
method to ensure the operation is allowed. This could result in a SecurityException.
fac
- the desired factory.IOException
- if an I/O error occurs when setting the socket factory.SocketException
- if the factory is already defined.SecurityException
- if a security manager exists and its checkSetFactory
method doesn't allow the operation.