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.
If there is a security manager, its checkConnect
method
is called with the proxy host address and port number
as its arguments. This could result in a SecurityException.
Examples:
Socket s = new Socket(Proxy.NO_PROXY);
will create
a plain socket ignoring any other proxy configuration.Socket s = new Socket(new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("socks.mydom.com", 1080)));
will create a socket connecting through the specified SOCKS proxy
server.If the specified host is null it is the equivalent of specifying the address as InetAddress.getByName (null). In other words, it is equivalent to specifying an address of the loopback interface.
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.
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.
If the specified host is null it is the equivalent of specifying the address as InetAddress.getByName (null). In other words, it is equivalent to specifying an address of the loopback interface.
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.
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.
If the specified host is null it is the equivalent of specifying the address as InetAddress.getByName (null). In other words, it is equivalent to specifying an address of the loopback interface.
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.
If a UDP socket is used, TCP/IP related socket options will not apply.
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.
If UDP socket is used, TCP/IP related socket options will not apply.
If the address is null
, then the system will pick up
an ephemeral port and a valid local address to bind the socket.
Any thread currently blocked in an I/O operation upon this socket will throw a SocketException .
Once a socket has been closed, it is not available for further networking use (i.e. can't be reconnected or rebound). A new socket needs to be created.
If this socket has an associated channel then the channel is closed as well.
The equals
method implements an equivalence relation
on non-null object references:
x
, x.equals(x)
should return
true
.
x
and y
, x.equals(y)
should return true
if and only if
y.equals(x)
returns true
.
x
, y
, and z
, if
x.equals(y)
returns true
and
y.equals(z)
returns true
, then
x.equals(z)
should return true
.
x
and y
, multiple invocations of
x.equals(y) consistently return true
or consistently return false
, provided no
information used in equals
comparisons on the
objects is modified.
x
,
x.equals(null)
should return false
.
The equals method for class Object
implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x
and
y
, this method returns true
if and only
if x
and y
refer to the same object
(x == y
has the value true
).
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
A socket will have a channel if, and only if, the channel itself was created via the SocketChannel.open or ServerSocketChannel.accept methods.
If this socket has an associated channel then the resulting input stream delegates all of its operations to the channel. If the channel is in non-blocking mode then the input stream's read operations will throw an java.nio.channels.IllegalBlockingModeException .
Under abnormal conditions the underlying connection may be broken by the remote host or the network software (for example a connection reset in the case of TCP connections). When a broken connection is detected by the network software the following applies to the returned input stream :-
The network software may discard bytes that are buffered by the socket. Bytes that aren't discarded by the network software can be read using read .
If there are no bytes buffered on the socket, or all buffered bytes have been consumed by read , then all subsequent calls to read will throw an IOException .
If there are no bytes buffered on the socket, and the
socket has not been closed using close
, then
available
will
return 0
.
null
if it is not bound yet.If this socket has an associated channel then the resulting output stream delegates all of its operations to the channel. If the channel is in non-blocking mode then the output stream's write operations will throw an java.nio.channels.IllegalBlockingModeException .
null
if it is unconnected.java.util.Hashtable
.
The general contract of hashCode
is:
hashCode
method on each of
the two objects must produce the same integer result.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
wait
methods.
The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object. The awakened thread will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened thread enjoys no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:
synchronized
statement
that synchronizes on the object.
Class,
by executing a
synchronized static method of that class.
Only one thread at a time can own an object's monitor.
wait
methods.
The awakened threads will not be able to proceed until the current thread relinquishes the lock on this object. The awakened threads will compete in the usual manner with any other threads that might be actively competing to synchronize on this object; for example, the awakened threads enjoy no reliable privilege or disadvantage in being the next thread to lock this object.
This method should only be called by a thread that is the owner
of this object's monitor. See the notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
Note, only limited support is provided for handling incoming urgent data. In particular, no notification of incoming urgent data is provided and there is no capability to distinguish between normal data and urgent data unless provided by a higher level protocol.
Sockets use the TCP/IP protocol by default. Some implementations may offer alternative protocols which have different performance characteristics than TCP/IP. This method allows the application to express its own preferences as to how these tradeoffs should be made when the implementation chooses from the available protocols.
Performance preferences are described by three integers whose values indicate the relative importance of short connection time, low latency, and high bandwidth. The absolute values of the integers are irrelevant; in order to choose a protocol the values are simply compared, with larger values indicating stronger preferences. Negative values represent a lower priority than positive values. If the application prefers short connection time over both low latency and high bandwidth, for example, then it could invoke this method with the values (1, 0, 0). If the application prefers high bandwidth above low latency, and low latency above short connection time, then it could invoke this method with the values (0, 1, 2).
Invoking this method after this socket has been connected will have no effect.
Increasing the receive 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.
Because SO_RCVBUF is a hint, applications that want to verify what size the buffers were set to should call .
The value of SO_RCVBUF is also used to set the TCP receive window that is advertized to the remote peer. Generally, the window size can be modified at any time when a socket is connected. However, if a receive window larger than 64K is required then this must be requested before the socket is connected to the remote peer. There are two cases to be aware of:
When a TCP connection is closed the connection may remain in a timeout state for a period of time after the connection is closed (typically known as the TIME_WAIT state or 2MSL wait state). For applications using a well known socket address or port it may not be possible to bind a socket to the required SocketAddress if there is a connection in the timeout state involving the socket address or port.
Enabling SO_REUSEADDR prior to binding the socket using allows the socket to be bound even though a previous connection is in a timeout state.
When a Socket is created the initial setting of SO_REUSEADDR is disabled.
The behaviour when SO_REUSEADDR is enabled or disabled after a socket is bound (See ) is not defined.
Because SO_SNDBUF is a hint, applications that want to verify what size the buffers were set to should call .
When an application creates a new client socket, the socket
implementation factory's createSocketImpl
method is
called to create the actual socket implementation.
Passing null
to the method is a no-op unless the factory
was already set.
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.
The tc must be in the range 0 <= tc <=
255
or an IllegalArgumentException will be thrown.
Notes:
for Internet Protocol v4 the value consists of an octet with precedence and TOS fields as detailed in RFC 1349. The TOS field is bitset created by bitwise-or'ing values such the following :-
IPTOS_LOWCOST (0x02)
IPTOS_RELIABILITY (0x04)
IPTOS_THROUGHPUT (0x08)
IPTOS_LOWDELAY (0x10)
Setting bits in the precedence field may result in a SocketException indicating that the operation is not permitted.
for Internet Protocol v6 tc
is the value that
would be placed into the sin6_flowinfo field of the IP header.
If you read from a socket input stream after invoking shutdownInput() on the socket, the stream will return EOF.
String
.
The current thread must own this object's monitor. The thread
releases ownership of this monitor and waits until another thread
notifies threads waiting on this object's monitor to wake up
either through a call to the notify
method or the
notifyAll
method. The thread then waits until it can
re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) { while (<condition does not hold>) obj.wait(); ... // Perform action appropriate to condition }This method should only be called by a thread that is the owner of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.The current thread must own this object's monitor.
This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:
A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:
synchronized (obj) { while (<condition does not hold>) obj.wait(timeout); ... // Perform action appropriate to condition }(For more information on this topic, see Section 3.2.3 in Doug Lea's "Concurrent Programming in Java (Second Edition)" (Addison-Wesley, 2000), or Item 50 in Joshua Bloch's "Effective Java Programming Language Guide" (Addison-Wesley, 2001).
If the current thread is interrupted by another thread while it is waiting, then an InterruptedException is thrown. This exception is not thrown until the lock status of this object has been restored as described above.
Note that the wait method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits.
This method should only be called by a thread that is the owner
of this object's monitor. See the notify
method for a
description of the ways in which a thread can become the owner of
a monitor.
This method is similar to the wait
method of one
argument, but it allows finer control over the amount of time to
wait for a notification before giving up. The amount of real time,
measured in nanoseconds, is given by:
1000000*timeout+nanos
In all other respects, this method does the same thing as the method of one argument. In particular, wait(0, 0) means the same thing as wait(0).
The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until either of the following two conditions has occurred:
notify
method
or the notifyAll
method.
timeout
milliseconds plus nanos
nanoseconds arguments, has
elapsed.
The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:
synchronized (obj) { while (<condition does not hold>) obj.wait(timeout, nanos); ... // Perform action appropriate to condition }This method should only be called by a thread that is the owner of this object's monitor. See the
notify
method for a
description of the ways in which a thread can become the owner of
a monitor.