| Constructor: |
public Socket() {
impl = factory != null ? factory.createSocketImpl()
: new PlainSocketImpl();
}
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. |
public Socket(Proxy proxy) {
if (null == proxy || Proxy.Type.HTTP == proxy.type()) {
// luni.73=Proxy is null or invalid type
throw new IllegalArgumentException(Messages.getString("luni.73")); //$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()
: new PlainSocketImpl(proxy);
this.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)));} Parameters:
proxy -
the specified proxy for this socket.
Throws:
IllegalArgumentException -
if the argument {@code proxy} is {@code null} or of an
invalid type.
SecurityException -
if a security manager exists and it denies the permission to
connect to the given proxy.
Also see:
- SocketImplFactory
- SocketImpl
|
protected Socket(SocketImpl anImpl) throws SocketException {
impl = anImpl;
}
Creates an unconnected socket with the given socket implementation. Parameters:
anImpl -
the socket implementation to be used.
Throws:
SocketException -
if an error occurs while creating the socket.
|
public Socket(String dstName,
int dstPort) throws UnknownHostException, IOException {
this();
InetAddress dstAddress = InetAddress.getByName(dstName);
checkDestination(dstAddress, dstPort);
startupSocket(dstAddress, dstPort, null, 0, true);
}
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. Parameters:
dstName -
the target host name or IP address to connect to.
dstPort -
the port on the target host to connect to.
Throws:
UnknownHostException -
if the host name could not be resolved into an IP address.
IOException -
if an error occurs while creating the socket.
SecurityException -
if a security manager exists and it denies the permission to
connect to the given address and port.
|
public Socket(InetAddress dstAddress,
int dstPort) throws IOException {
this();
checkDestination(dstAddress, dstPort);
startupSocket(dstAddress, dstPort, null, 0, true);
}
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. Parameters:
dstAddress -
the target host address to connect to.
dstPort -
the port on the target host to connect to.
Throws:
IOException -
if an error occurs while creating the socket.
SecurityException -
if a security manager exists and it denies the permission to
connect to the given address and port.
|
public Socket(String hostName,
int port,
boolean streaming) throws IOException {
this();
InetAddress host = InetAddress.getByName(hostName);
checkDestination(host, port);
startupSocket(host, port, null, 0, 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. Parameters:
hostName -
the target host name or IP address to connect to.
port -
the port on the target host to connect to.
streaming -
if {@code true} a streaming socket is returned, a datagram
socket otherwise.
Throws:
UnknownHostException -
if the host name could not be resolved into an IP address.
IOException -
if an error occurs while creating the socket.
SecurityException -
if a security manager exists and it denies the permission to
connect to the given address and port.
|
public Socket(InetAddress addr,
int port,
boolean streaming) throws IOException {
this();
checkDestination(addr, port);
startupSocket(addr, port, null, 0, 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. Parameters:
addr -
the Internet address to connect to.
port -
the port on the target host to connect to.
streaming -
if {@code true} a streaming socket is returned, a datagram
socket otherwise.
Throws:
IOException -
if an error occurs while creating the socket.
SecurityException -
if a security manager exists and it denies the permission to
connect to the given address and port.
|
public Socket(String dstName,
int dstPort,
InetAddress localAddress,
int localPort) throws IOException {
this();
InetAddress dstAddress = InetAddress.getByName(dstName);
checkDestination(dstAddress, dstPort);
startupSocket(dstAddress, dstPort, localAddress, localPort, true);
}
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. Parameters:
dstName -
the target host name or IP address to connect to.
dstPort -
the port on the target host to connect to.
localAddress -
the address on the local host to bind to.
localPort -
the port on the local host to bind to.
Throws:
UnknownHostException -
if the host name could not be resolved into an IP address.
IOException -
if an error occurs while creating the socket.
SecurityException -
if a security manager exists and it denies the permission to
connect to the given address and port.
|
public Socket(InetAddress dstAddress,
int dstPort,
InetAddress localAddress,
int localPort) throws IOException {
this();
checkDestination(dstAddress, dstPort);
startupSocket(dstAddress, dstPort, localAddress, localPort, true);
}
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}. Parameters:
dstAddress -
the target host address to connect to.
dstPort -
the port on the target host to connect to.
localAddress -
the address on the local host to bind to.
localPort -
the port on the local host to bind to.
Throws:
IOException -
if an error occurs while creating the socket.
SecurityException -
if a security manager exists and it denies the permission to
connect to the given address and port.
|
| Method from java.net.Socket Detail: |
void accepted() {
isCreated = isBound = isConnected = true;
}
Set the appropriate flags for a socket created by {@code
ServerSocket.accept()}. |
public void bind(SocketAddress localAddr) throws IOException {
checkClosedAndCreate(true);
if (isBound()) {
throw new BindException(Messages.getString("luni.71")); //$NON-NLS-1$
}
int port = 0;
InetAddress addr = InetAddress.ANY;
if (localAddr != null) {
if (!(localAddr instanceof InetSocketAddress)) {
throw new IllegalArgumentException(Messages.getString(
"luni.49", localAddr.getClass())); //$NON-NLS-1$
}
InetSocketAddress inetAddr = (InetSocketAddress) localAddr;
if ((addr = inetAddr.getAddress()) == null) {
throw new SocketException(Messages.getString(
"luni.1A", inetAddr.getHostName())); //$NON-NLS-1$
}
port = inetAddr.getPort();
}
synchronized (this) {
try {
impl.bind(addr, port);
isBound = true;
} catch (IOException e) {
impl.close();
throw e;
}
}
}
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. |
void checkDestination(InetAddress destAddr,
int dstPort) {
if (dstPort < 0 || dstPort > 65535) {
throw new IllegalArgumentException(Messages.getString("luni.38")); //$NON-NLS-1$
}
checkConnectPermission(destAddr.getHostName(), dstPort);
}
Checks whether the connection destination satisfies the security policy
and the validity of the port range. |
public synchronized void close() throws IOException {
isClosed = true;
impl.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. |
public void connect(SocketAddress remoteAddr) throws IOException {
connect(remoteAddr, 0);
}
Connects this socket to the given remote host address and port specified
by the SocketAddress {@code remoteAddr}. |
public void connect(SocketAddress remoteAddr,
int timeout) throws IOException {
checkClosedAndCreate(true);
if (timeout < 0) {
throw new IllegalArgumentException(Messages.getString("luni.5B")); //$NON-NLS-1$
}
if (isConnected()) {
throw new SocketException(Messages.getString("luni.5F")); //$NON-NLS-1$
}
if (remoteAddr == null) {
throw new IllegalArgumentException(Messages.getString("luni.5D")); //$NON-NLS-1$
}
if (!(remoteAddr instanceof InetSocketAddress)) {
throw new IllegalArgumentException(Messages.getString(
"luni.49", remoteAddr.getClass())); //$NON-NLS-1$
}
InetSocketAddress inetAddr = (InetSocketAddress) remoteAddr;
InetAddress addr;
if ((addr = inetAddr.getAddress()) == null) {
throw new UnknownHostException(Messages.getString("luni.1A", 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;
}
}
}
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. |
public SocketChannel getChannel() {
return null;
}
Gets the SocketChannel of this socket, if one is available. The current
implementation of this method returns always {@code null}. |
public InetAddress getInetAddress() {
if (!isConnected()) {
return null;
}
return impl.getInetAddress();
}
Gets the IP address of the target host this socket is connected to. |
public InputStream getInputStream() throws IOException {
checkClosedAndCreate(false);
if (isInputShutdown()) {
throw new SocketException(Messages.getString("luni.74")); //$NON-NLS-1$
}
return impl.getInputStream();
}
Gets an input stream to read data from this socket. |
public boolean getKeepAlive() throws SocketException {
checkClosedAndCreate(true);
return ((Boolean) impl.getOption(SocketOptions.SO_KEEPALIVE))
.booleanValue();
}
Gets the setting of the socket option {@code SocketOptions.SO_KEEPALIVE}. |
public InetAddress getLocalAddress() {
if (!isBound()) {
return InetAddress.ANY;
}
return Platform.getNetworkSystem().getSocketLocalAddress(impl.fd,
InetAddress.preferIPv6Addresses());
}
Gets the local IP address this socket is bound to. |
public int getLocalPort() {
if (!isBound()) {
return -1;
}
return impl.getLocalPort();
}
Gets the local port this socket is bound to. |
public SocketAddress getLocalSocketAddress() {
if (!isBound()) {
return null;
}
return new InetSocketAddress(getLocalAddress(), getLocalPort());
}
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. |
public boolean getOOBInline() throws SocketException {
checkClosedAndCreate(true);
return ((Boolean) impl.getOption(SocketOptions.SO_OOBINLINE))
.booleanValue();
}
Gets the setting of the socket option {@code SocketOptions.SO_OOBINLINE}. |
public OutputStream getOutputStream() throws IOException {
checkClosedAndCreate(false);
if (isOutputShutdown()) {
throw new SocketException(Messages.getString("luni.75")); //$NON-NLS-1$
}
return impl.getOutputStream();
}
Gets an output stream to write data into this socket. |
public int getPort() {
if (!isConnected()) {
return 0;
}
return impl.getPort();
}
Gets the port number of the target host this socket is connected to. |
public synchronized int getReceiveBufferSize() throws SocketException {
checkClosedAndCreate(true);
return ((Integer) impl.getOption(SocketOptions.SO_RCVBUF)).intValue();
}
Gets the receive buffer size of this socket. |
public SocketAddress getRemoteSocketAddress() {
if (!isConnected()) {
return null;
}
return new InetSocketAddress(getInetAddress(), getPort());
}
Gets the remote address and port of this socket as a {@code
SocketAddress} or {@code null} if the socket is not connected. |
public boolean getReuseAddress() throws SocketException {
checkClosedAndCreate(true);
return ((Boolean) impl.getOption(SocketOptions.SO_REUSEADDR))
.booleanValue();
}
Gets the setting of the socket option {@code SocketOptions.SO_REUSEADDR}. |
public synchronized int getSendBufferSize() throws SocketException {
checkClosedAndCreate(true);
return ((Integer) impl.getOption(SocketOptions.SO_SNDBUF)).intValue();
}
Gets the send buffer size of this socket. |
public int getSoLinger() throws SocketException {
checkClosedAndCreate(true);
return ((Integer) impl.getOption(SocketOptions.SO_LINGER)).intValue();
}
Gets the value of the socket option {@code SocketOptions.SO_LINGER}. |
public synchronized int getSoTimeout() throws SocketException {
checkClosedAndCreate(true);
return ((Integer) impl.getOption(SocketOptions.SO_TIMEOUT)).intValue();
}
Gets the timeout for this socket during which a reading operation shall
block while waiting for data. |
public boolean getTcpNoDelay() throws SocketException {
checkClosedAndCreate(true);
return ((Boolean) impl.getOption(SocketOptions.TCP_NODELAY))
.booleanValue();
}
Gets the setting of the socket option {@code SocketOptions.TCP_NODELAY}. |
public int getTrafficClass() throws SocketException {
checkClosedAndCreate(true);
return ((Number) impl.getOption(SocketOptions.IP_TOS)).intValue();
}
Gets the value of the socket option {@code SocketOptions.IP_TOS}. |
public boolean isBound() {
return isBound;
}
Returns whether this socket is bound to a local address and port. |
public boolean isClosed() {
return isClosed;
}
Returns whether this socket is closed. |
public boolean isConnected() {
return isConnected;
}
Returns whether this socket is connected to a remote host. |
public boolean isInputShutdown() {
return isInputShutdown;
}
Returns whether the incoming channel of the socket has already been
closed. |
public boolean isOutputShutdown() {
return isOutputShutdown;
}
Returns whether the outgoing channel of the socket has already been
closed. |
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) throws IOException {
if (!impl.supportsUrgentData()) {
throw new SocketException(Messages.getString("luni.79")); //$NON-NLS-1$
}
impl.sendUrgentData(value);
}
Sends the given single byte data which is represented by the lowest octet
of {@code value} as "TCP urgent data". |
public void setKeepAlive(boolean value) throws SocketException {
if (impl != null) {
checkClosedAndCreate(true);
impl.setOption(SocketOptions.SO_KEEPALIVE, value ? Boolean.TRUE
: Boolean.FALSE);
}
}
Sets the state of the {@code SocketOptions.SO_KEEPALIVE} for this socket. |
public void setOOBInline(boolean oobinline) throws SocketException {
checkClosedAndCreate(true);
impl.setOption(SocketOptions.SO_OOBINLINE, oobinline ? Boolean.TRUE
: Boolean.FALSE);
}
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. |
public void setPerformancePreferences(int connectionTime,
int latency,
int bandwidth) {
// Our socket implementation only provide one protocol: TCP/IP, so
// we do nothing for this method
}
|
public synchronized void setReceiveBufferSize(int size) throws SocketException {
checkClosedAndCreate(true);
if (size < 1) {
throw new IllegalArgumentException(Messages.getString("luni.5A")); //$NON-NLS-1$
}
impl.setOption(SocketOptions.SO_RCVBUF, Integer.valueOf(size));
}
Sets the receive buffer size of this socket. |
public void setReuseAddress(boolean reuse) throws SocketException {
checkClosedAndCreate(true);
impl.setOption(SocketOptions.SO_REUSEADDR, reuse ? Boolean.TRUE
: Boolean.FALSE);
}
Sets the state of the {@code SocketOptions.SO_REUSEADDR} for this socket. |
public synchronized void setSendBufferSize(int size) throws SocketException {
checkClosedAndCreate(true);
if (size < 1) {
throw new IllegalArgumentException(Messages.getString("luni.5A")); //$NON-NLS-1$
}
impl.setOption(SocketOptions.SO_SNDBUF, Integer.valueOf(size));
}
Sets the send buffer size of this socket. |
public void setSoLinger(boolean on,
int timeout) throws SocketException {
checkClosedAndCreate(true);
if (on && timeout < 0) {
throw new IllegalArgumentException(Messages.getString("luni.76")); //$NON-NLS-1$
}
int val = on ? (65535 < timeout ? 65535 : timeout) : -1;
impl.setOption(SocketOptions.SO_LINGER, Integer.valueOf(val));
}
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}. |
public synchronized void setSoTimeout(int timeout) throws SocketException {
checkClosedAndCreate(true);
if (timeout < 0) {
throw new IllegalArgumentException(Messages.getString("luni.5B")); //$NON-NLS-1$
}
impl.setOption(SocketOptions.SO_TIMEOUT, Integer.valueOf(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. |
public static synchronized void setSocketImplFactory(SocketImplFactory fac) throws IOException {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkSetFactory();
}
if (factory != null) {
throw new SocketException(Messages.getString("luni.5C")); //$NON-NLS-1$
}
factory = fac;
}
Sets the internal factory for creating socket implementations. This may
only be executed once during the lifetime of the application. |
public void setTcpNoDelay(boolean on) throws SocketException {
checkClosedAndCreate(true);
impl.setOption(SocketOptions.TCP_NODELAY, Boolean.valueOf(on));
}
Sets the state of the {@code SocketOptions.TCP_NODELAY} for this socket. |
public void setTrafficClass(int value) throws SocketException {
checkClosedAndCreate(true);
if (value < 0 || value > 255) {
throw new IllegalArgumentException();
}
impl.setOption(SocketOptions.IP_TOS, Integer.valueOf(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. |
public void shutdownInput() throws IOException {
if (isInputShutdown()) {
throw new SocketException(Messages.getString("luni.74")); //$NON-NLS-1$
}
checkClosedAndCreate(false);
impl.shutdownInput();
isInputShutdown = true;
}
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}. |
public void shutdownOutput() throws IOException {
if (isOutputShutdown()) {
throw new SocketException(Messages.getString("luni.75")); //$NON-NLS-1$
}
checkClosedAndCreate(false);
impl.shutdownOutput();
isOutputShutdown = true;
}
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}. |
void startupSocket(InetAddress dstAddress,
int dstPort,
InetAddress localAddress,
int localPort,
boolean streaming) throws IOException {
if (localPort < 0 || localPort > 65535) {
throw new IllegalArgumentException(Messages.getString("luni.77")); //$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;
}
}
}
Creates a stream socket, binds it to the nominated local address/port,
then connects it to the nominated destination address/port. |
public String toString() {
if (!isConnected()) {
return "Socket[unconnected]"; //$NON-NLS-1$
}
return impl.toString();
}
Returns a {@code String} containing a concise, human-readable description of the
socket. |