Constructors Summary |
---|
public Socket()Creates a new unconnected socket. When a SocketImplFactory is defined it
creates the internal socket implementation, otherwise the default socket
implementation will be used for this socket.
Platform.getNetworkSystem().oneTimeInitialization(true);
impl = factory != null ? factory.createSocketImpl()
: SocketImplProvider.getSocketImpl();
|
public Socket(Proxy proxy)Creates a new unconnected socket using the given proxy type. When a
{@code SocketImplFactory} is defined it creates the internal socket
implementation, otherwise the default socket implementation will be used
for this socket.
Example that will create a socket connection through a {@code SOCKS}
proxy server:
{@code Socket sock = new Socket(new Proxy(Proxy.Type.SOCKS, new
InetSocketAddress("test.domain.org", 2130)));}
if (null == proxy || Proxy.Type.HTTP == proxy.type()) {
// KA023=Proxy is null or invalid type
throw new IllegalArgumentException(Msg.getString("KA023")); //$NON-NLS-1$
}
InetSocketAddress address = (InetSocketAddress) proxy.address();
if (null != address) {
InetAddress addr = address.getAddress();
String host;
if (null != addr) {
host = addr.getHostAddress();
} else {
host = address.getHostName();
}
int port = address.getPort();
checkConnectPermission(host, port);
}
impl = factory != null ? factory.createSocketImpl()
: SocketImplProvider.getSocketImpl(proxy);
this.proxy = proxy;
|
public Socket(String dstName, int dstPort)Creates a new streaming socket connected to the target host specified by
the parameters {@code dstName} and {@code dstPort}. The socket is bound
to any available port on the local host.
this();
InetAddress dstAddress = InetAddress.getByName(dstName);
checkDestination(dstAddress, dstPort);
startupSocket(dstAddress, dstPort, null, 0, true);
|
public Socket(String dstName, int dstPort, InetAddress localAddress, int localPort)Creates a new streaming socket connected to the target host specified by
the parameters {@code dstName} and {@code dstPort}. On the local endpoint
the socket is bound to the given address {@code localAddress} on port
{@code localPort}.
If {@code host} is {@code null} a loopback address is used to connect to.
this();
InetAddress dstAddress = InetAddress.getByName(dstName);
checkDestination(dstAddress, dstPort);
startupSocket(dstAddress, dstPort, localAddress, localPort, true);
|
public Socket(String hostName, int port, boolean streaming)Creates a new streaming or datagram socket connected to the target host
specified by the parameters {@code hostName} and {@code port}. The socket
is bound to any available port on the local host.
this();
InetAddress host = InetAddress.getByName(hostName);
checkDestination(host, port);
startupSocket(host, port, null, 0, streaming);
|
public Socket(InetAddress dstAddress, int dstPort)Creates a new streaming socket connected to the target host specified by
the parameters {@code dstAddress} and {@code dstPort}. The socket is
bound to any available port on the local host.
this();
checkDestination(dstAddress, dstPort);
startupSocket(dstAddress, dstPort, null, 0, true);
|
public Socket(InetAddress dstAddress, int dstPort, InetAddress localAddress, int localPort)Creates a new streaming socket connected to the target host specified by
the parameters {@code dstAddress} and {@code dstPort}. On the local
endpoint the socket is bound to the given address {@code localAddress} on
port {@code localPort}.
this();
checkDestination(dstAddress, dstPort);
startupSocket(dstAddress, dstPort, localAddress, localPort, true);
|
public Socket(InetAddress addr, int port, boolean streaming)Creates a new streaming or datagram socket connected to the target host
specified by the parameters {@code addr} and {@code port}. The socket is
bound to any available port on the local host.
this();
checkDestination(addr, port);
startupSocket(addr, port, null, 0, streaming);
|
protected Socket(SocketImpl anImpl)Creates an unconnected socket with the given socket implementation.
impl = anImpl;
|
Methods Summary |
---|
void | accepted()Set the appropriate flags for a socket created by {@code
ServerSocket.accept()}.
isCreated = isBound = isConnected = true;
|
public void | bind(java.net.SocketAddress localAddr)Binds this socket to the given local host address and port specified by
the SocketAddress {@code localAddr}. If {@code localAddr} is set to
{@code null}, this socket will be bound to an available local address on
any free port.
checkClosedAndCreate(true);
if (isBound()) {
throw new BindException(Msg.getString("K0315")); //$NON-NLS-1$
}
int port = 0;
InetAddress addr = InetAddress.ANY;
if (localAddr != null) {
if (!(localAddr instanceof InetSocketAddress)) {
throw new IllegalArgumentException(Msg.getString(
"K0316", localAddr.getClass())); //$NON-NLS-1$
}
InetSocketAddress inetAddr = (InetSocketAddress) localAddr;
if ((addr = inetAddr.getAddress()) == null) {
throw new SocketException(Msg.getString(
"K0317", inetAddr.getHostName())); //$NON-NLS-1$
}
port = inetAddr.getPort();
}
synchronized (this) {
try {
impl.bind(addr, port);
isBound = true;
} catch (IOException e) {
impl.close();
throw e;
}
}
|
private void | checkClosedAndCreate(boolean create)Checks whether the socket is closed, and throws an exception. Otherwise
creates the underlying SocketImpl.
if (isClosed()) {
throw new SocketException(Msg.getString("K003d")); //$NON-NLS-1$
}
if (!create) {
if (!isConnected()) {
throw new SocketException(Msg.getString("K0320")); //$NON-NLS-1$
// a connected socket must be created
}
/*
* return directly to fix a possible bug, if !create, should return
* here
*/
return;
}
if (isCreated) {
return;
}
synchronized (this) {
if (isCreated) {
return;
}
try {
impl.create(true);
} catch (SocketException e) {
throw e;
} catch (IOException e) {
throw new SocketException(e.toString());
}
isCreated = true;
}
|
private void | checkConnectPermission(java.lang.String hostname, int dstPort)Checks whether the connection destination satisfies the security policy.
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkConnect(hostname, dstPort);
}
|
void | checkDestination(java.net.InetAddress destAddr, int dstPort)Checks whether the connection destination satisfies the security policy
and the validity of the port range.
if (dstPort < 0 || dstPort > 65535) {
throw new IllegalArgumentException(Msg.getString("K0032")); //$NON-NLS-1$
}
// BEGIN android-changed
checkConnectPermission(destAddr.getHostAddress(), dstPort);
// END android-changed
|
public synchronized void | close()Closes the socket. It is not possible to reconnect or rebind to this
socket thereafter which means a new socket instance has to be created.
isClosed = true;
impl.close();
|
public void | connect(java.net.SocketAddress remoteAddr)Connects this socket to the given remote host address and port specified
by the SocketAddress {@code remoteAddr}.
connect(remoteAddr, 0);
|
public void | connect(java.net.SocketAddress remoteAddr, int timeout)Connects this socket to the given remote host address and port specified
by the SocketAddress {@code remoteAddr} with the specified timeout. The
connecting method will block until the connection is established or an
error occurred.
checkClosedAndCreate(true);
if (timeout < 0) {
throw new IllegalArgumentException(Msg.getString("K0036")); //$NON-NLS-1$
}
if (isConnected()) {
throw new SocketException(Msg.getString("K0079")); //$NON-NLS-1$
}
if (remoteAddr == null) {
throw new IllegalArgumentException(Msg.getString("K0318")); //$NON-NLS-1$
}
if (!(remoteAddr instanceof InetSocketAddress)) {
throw new IllegalArgumentException(Msg.getString(
"K0316", remoteAddr.getClass())); //$NON-NLS-1$
}
InetSocketAddress inetAddr = (InetSocketAddress) remoteAddr;
InetAddress addr;
if ((addr = inetAddr.getAddress()) == null) {
throw new UnknownHostException(Msg.getString("K0317", remoteAddr));//$NON-NLS-1$
}
int port = inetAddr.getPort();
checkDestination(addr, port);
synchronized (connectLock) {
try {
if (!isBound()) {
// socket allready created at this point by earlier call or
// checkClosedAndCreate this caused us to lose socket
// options on create
// impl.create(true);
if (!NetUtil.usingSocks(proxy)) {
impl.bind(InetAddress.ANY, 0);
}
isBound = true;
}
impl.connect(remoteAddr, timeout);
isConnected = true;
} catch (IOException e) {
impl.close();
throw e;
}
}
|
public java.nio.channels.SocketChannel | getChannel()Gets the SocketChannel of this socket, if one is available. The current
implementation of this method returns always {@code null}.
return null;
|
public java.net.InetAddress | getInetAddress()Gets the IP address of the target host this socket is connected to.
if (!isConnected()) {
return null;
}
return impl.getInetAddress();
|
public java.io.InputStream | getInputStream()Gets an input stream to read data from this socket.
checkClosedAndCreate(false);
if (isInputShutdown()) {
throw new SocketException(Msg.getString("K0321")); //$NON-NLS-1$
}
return impl.getInputStream();
|
public boolean | getKeepAlive()Gets the setting of the socket option {@code SocketOptions.SO_KEEPALIVE}.
checkClosedAndCreate(true);
return ((Boolean) impl.getOption(SocketOptions.SO_KEEPALIVE))
.booleanValue();
|
public java.net.InetAddress | getLocalAddress()Gets the local IP address this socket is bound to.
if (!isBound()) {
return InetAddress.ANY;
}
return Platform.getNetworkSystem().getSocketLocalAddress(impl.fd,
InetAddress.preferIPv6Addresses());
|
public int | getLocalPort()Gets the local port this socket is bound to.
if (!isBound()) {
return -1;
}
return impl.getLocalPort();
|
public java.net.SocketAddress | getLocalSocketAddress()Gets the local address and port of this socket as a SocketAddress or
{@code null} if the socket is unbound. This is useful on multihomed
hosts.
if (!isBound()) {
return null;
}
return new InetSocketAddress(getLocalAddress(), getLocalPort());
|
public boolean | getOOBInline()Gets the setting of the socket option {@code SocketOptions.SO_OOBINLINE}.
checkClosedAndCreate(true);
return ((Boolean) impl.getOption(SocketOptions.SO_OOBINLINE))
.booleanValue();
|
public java.io.OutputStream | getOutputStream()Gets an output stream to write data into this socket.
checkClosedAndCreate(false);
if (isOutputShutdown()) {
throw new SocketException(Msg.getString("KA00f")); //$NON-NLS-1$
}
return impl.getOutputStream();
|
public int | getPort()Gets the port number of the target host this socket is connected to.
if (!isConnected()) {
return 0;
}
return impl.getPort();
|
public synchronized int | getReceiveBufferSize()Gets the receive buffer size of this socket.
checkClosedAndCreate(true);
return ((Integer) impl.getOption(SocketOptions.SO_RCVBUF)).intValue();
|
public java.net.SocketAddress | getRemoteSocketAddress()Gets the remote address and port of this socket as a {@code
SocketAddress} or {@code null} if the socket is not connected.
if (!isConnected()) {
return null;
}
return new InetSocketAddress(getInetAddress(), getPort());
|
public boolean | getReuseAddress()Gets the setting of the socket option {@code SocketOptions.SO_REUSEADDR}.
checkClosedAndCreate(true);
return ((Boolean) impl.getOption(SocketOptions.SO_REUSEADDR))
.booleanValue();
|
public synchronized int | getSendBufferSize()Gets the send buffer size of this socket.
checkClosedAndCreate(true);
return ((Integer) impl.getOption(SocketOptions.SO_SNDBUF)).intValue();
|
public int | getSoLinger()Gets the value of the socket option {@code SocketOptions.SO_LINGER}.
checkClosedAndCreate(true);
return ((Integer) impl.getOption(SocketOptions.SO_LINGER)).intValue();
|
public synchronized int | getSoTimeout()Gets the timeout for this socket during which a reading operation shall
block while waiting for data.
checkClosedAndCreate(true);
return ((Integer) impl.getOption(SocketOptions.SO_TIMEOUT)).intValue();
|
public boolean | getTcpNoDelay()Gets the setting of the socket option {@code SocketOptions.TCP_NODELAY}.
checkClosedAndCreate(true);
return ((Boolean) impl.getOption(SocketOptions.TCP_NODELAY))
.booleanValue();
|
public int | getTrafficClass()Gets the value of the socket option {@code SocketOptions.IP_TOS}.
checkClosedAndCreate(true);
return ((Number) impl.getOption(SocketOptions.IP_TOS)).intValue();
|
public boolean | isBound()Returns whether this socket is bound to a local address and port.
return isBound;
|
public boolean | isClosed()Returns whether this socket is closed.
return isClosed;
|
public boolean | isConnected()Returns whether this socket is connected to a remote host.
return isConnected;
|
public boolean | isInputShutdown()Returns whether the incoming channel of the socket has already been
closed.
return isInputShutdown;
|
public boolean | isOutputShutdown()Returns whether the outgoing channel of the socket has already been
closed.
return isOutputShutdown;
|
static boolean | preferIPv4Stack()
String result = AccessController.doPrivileged(new PriviAction<String>(
"java.net.preferIPv4Stack")); //$NON-NLS-1$
return "true".equals(result); //$NON-NLS-1$
|
public void | sendUrgentData(int value)Sends the given single byte data which is represented by the lowest octet
of {@code value} as "TCP urgent data".
if (!impl.supportsUrgentData()) {
throw new SocketException(Msg.getString("K0333")); //$NON-NLS-1$
}
impl.sendUrgentData(value);
|
public void | setKeepAlive(boolean value)Sets the state of the {@code SocketOptions.SO_KEEPALIVE} for this socket.
if (impl != null) {
checkClosedAndCreate(true);
impl.setOption(SocketOptions.SO_KEEPALIVE, value ? Boolean.TRUE
: Boolean.FALSE);
}
|
public void | setOOBInline(boolean oobinline)Sets the state of the {@code SocketOptions.SO_OOBINLINE} for this socket.
When this option is enabled urgent data can be received in-line with
normal data.
checkClosedAndCreate(true);
impl.setOption(SocketOptions.SO_OOBINLINE, oobinline ? Boolean.TRUE
: Boolean.FALSE);
|
public void | setPerformancePreferences(int connectionTime, int latency, int bandwidth)Sets performance preferences for connectionTime, latency and bandwidth.
This method does currently nothing.
// Our socket implementation only provide one protocol: TCP/IP, so
// we do nothing for this method
|
public synchronized void | setReceiveBufferSize(int size)Sets the receive buffer size of this socket.
checkClosedAndCreate(true);
if (size < 1) {
throw new IllegalArgumentException(Msg.getString("K0035")); //$NON-NLS-1$
}
impl.setOption(SocketOptions.SO_RCVBUF, Integer.valueOf(size));
|
public void | setReuseAddress(boolean reuse)Sets the state of the {@code SocketOptions.SO_REUSEADDR} for this socket.
checkClosedAndCreate(true);
impl.setOption(SocketOptions.SO_REUSEADDR, reuse ? Boolean.TRUE
: Boolean.FALSE);
|
public synchronized void | setSendBufferSize(int size)Sets the send buffer size of this socket.
checkClosedAndCreate(true);
if (size < 1) {
throw new IllegalArgumentException(Msg.getString("K0035")); //$NON-NLS-1$
}
impl.setOption(SocketOptions.SO_SNDBUF, Integer.valueOf(size));
|
public void | setSoLinger(boolean on, int timeout)Sets the state of the {@code SocketOptions.SO_LINGER} with the given
timeout in seconds. The timeout value for this option is silently limited
to the maximum of {@code 65535}.
checkClosedAndCreate(true);
if (on && timeout < 0) {
throw new IllegalArgumentException(Msg.getString("K0045")); //$NON-NLS-1$
}
// BEGIN android-changed
/*
* The spec indicates that the right way to turn off an option
* is to pass Boolean.FALSE, so that's what we do here.
*/
if (on) {
if (timeout > 65535) {
timeout = 65535;
}
impl.setOption(SocketOptions.SO_LINGER, Integer.valueOf(timeout));
} else {
impl.setOption(SocketOptions.SO_LINGER, Boolean.FALSE);
}
// END android-changed
|
public synchronized void | setSoTimeout(int timeout)Sets the reading timeout in milliseconds for this socket. The read
operation will block indefinitely if this option value is set to {@code
0}. The timeout must be set before calling the read operation. A
{@code SocketTimeoutException} is thrown when this timeout expires.
checkClosedAndCreate(true);
if (timeout < 0) {
throw new IllegalArgumentException(Msg.getString("K0036")); //$NON-NLS-1$
}
impl.setOption(SocketOptions.SO_TIMEOUT, Integer.valueOf(timeout));
|
public static synchronized void | setSocketImplFactory(java.net.SocketImplFactory fac)Sets the internal factory for creating socket implementations. This may
only be executed once during the lifetime of the application.
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkSetFactory();
}
if (factory != null) {
throw new SocketException(Msg.getString("K0044")); //$NON-NLS-1$
}
factory = fac;
|
public void | setTcpNoDelay(boolean on)Sets the state of the {@code SocketOptions.TCP_NODELAY} for this socket.
checkClosedAndCreate(true);
impl.setOption(SocketOptions.TCP_NODELAY, Boolean.valueOf(on));
|
public void | setTrafficClass(int value)Sets the value of the {@code SocketOptions.IP_TOS} for this socket. See
the specification RFC 1349 for more information about the type of service
field.
checkClosedAndCreate(true);
if (value < 0 || value > 255) {
throw new IllegalArgumentException();
}
impl.setOption(SocketOptions.IP_TOS, Integer.valueOf(value));
|
public void | shutdownInput()Closes the input stream of this socket. Any further data sent to this
socket will be discarded. Reading from this socket after this method has
been called will return the value {@code EOF}.
if (isInputShutdown()) {
throw new SocketException(Msg.getString("K0321")); //$NON-NLS-1$
}
checkClosedAndCreate(false);
impl.shutdownInput();
isInputShutdown = true;
|
public void | shutdownOutput()Closes the output stream of this socket. All buffered data will be sent
followed by the termination sequence. Writing to the closed output stream
will cause an {@code IOException}.
if (isOutputShutdown()) {
throw new SocketException(Msg.getString("KA00f")); //$NON-NLS-1$
}
checkClosedAndCreate(false);
impl.shutdownOutput();
isOutputShutdown = true;
|
void | startupSocket(java.net.InetAddress dstAddress, int dstPort, java.net.InetAddress localAddress, int localPort, boolean streaming)Creates a stream socket, binds it to the nominated local address/port,
then connects it to the nominated destination address/port.
if (localPort < 0 || localPort > 65535) {
throw new IllegalArgumentException(Msg.getString("K0046")); //$NON-NLS-1$
}
InetAddress addr = localAddress == null ? InetAddress.ANY
: localAddress;
synchronized (this) {
impl.create(streaming);
isCreated = true;
try {
if (!streaming || !NetUtil.usingSocks(proxy)) {
impl.bind(addr, localPort);
}
isBound = true;
impl.connect(dstAddress, dstPort);
isConnected = true;
} catch (IOException e) {
impl.close();
throw e;
}
}
|
public java.lang.String | toString()Returns a {@code String} containing a concise, human-readable description of the
socket.
if (!isConnected()) {
return "Socket[unconnected]"; //$NON-NLS-1$
}
return impl.toString();
|