|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object![]()
![]()
![]()
java.net.Socket
public class Socket

This class implements client sockets (also called just "sockets"). A socket is an endpoint for communication between two machines.
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.
setSocketImplFactory(java.net.SocketImplFactory)
,
SocketImpl
,
SocketChannel

| Constructor Summary | |
|---|---|
|
Socket
Creates an unconnected socket, with the system-default type of SocketImpl. |
|
Socket
Creates a stream socket and connects it to the specified port number at the specified IP address. |
|
Socket
Deprecated. Use DatagramSocket instead for UDP transport. |
|
Socket
Creates a socket and connects it to the specified remote address on the specified remote port. |
|
Socket
Creates an unconnected socket, specifying the type of proxy, if any, that should be used regardless of any other settings. |
protected |
Socket
Creates an unconnected Socket with a user-specified SocketImpl. |
|
Socket
Creates a stream socket and connects it to the specified port number on the named host. |
|
Socket
Deprecated. Use DatagramSocket instead for UDP transport. |
|
Socket
Creates a socket and connects it to the specified remote host on the specified remote port. |
| Method Summary | |
|---|---|
void |
bind
Binds the socket to a local address. |
void |
close
Closes this socket. |
void |
connect
Connects this socket to the server. |
void |
connect
Connects this socket to the server with a specified timeout value. |
SocketChannel |
getChannel
Returns the unique SocketChannel
object associated with this socket, if any. |
InetAddress |
getInetAddress
Returns the address to which the socket is connected. |
InputStream |
getInputStream
Returns an input stream for this socket. |
boolean |
getKeepAlive
Tests if SO_KEEPALIVE is enabled. |
InetAddress |
getLocalAddress
Gets the local address to which the socket is bound. |
int |
getLocalPort
Returns the local port to which this socket is bound. |
SocketAddress |
getLocalSocketAddress
Returns the address of the endpoint this socket is bound to, or null if it is not bound yet. |
boolean |
getOOBInline
Tests if OOBINLINE is enabled. |
OutputStream |
getOutputStream
Returns an output stream for this socket. |
int |
getPort
Returns the remote port to which this socket is connected. |
int |
getReceiveBufferSize
Gets the value of the SO_RCVBUF option for this Socket, that is the buffer size used by the platform for input on this Socket. |
SocketAddress |
getRemoteSocketAddress
Returns the address of the endpoint this socket is connected to, or null if it is unconnected. |
boolean |
getReuseAddress
Tests if SO_REUSEADDR is enabled. |
int |
getSendBufferSize
Get value of the SO_SNDBUF option for this Socket, that is the buffer size used by the platform for output on this Socket. |
int |
getSoLinger
Returns setting for SO_LINGER. |
int |
getSoTimeout
Returns setting for SO_TIMEOUT. |
boolean |
getTcpNoDelay
Tests if TCP_NODELAY is enabled. |
int |
getTrafficClass
Gets traffic class or type-of-service in the IP header for packets sent from this Socket |
boolean |
isBound
Returns the binding state of the socket. |
boolean |
isClosed
Returns the closed state of the socket. |
boolean |
isConnected
Returns the connection state of the socket. |
boolean |
isInputShutdown
Returns whether the read-half of the socket connection is closed. |
boolean |
isOutputShutdown
Returns whether the write-half of the socket connection is closed. |
void |
sendUrgentData
Send one byte of urgent data on the socket. |
void |
setKeepAlive
Enable/disable SO_KEEPALIVE. |
void |
setOOBInline
Enable/disable OOBINLINE (receipt of TCP urgent data) By default, this option is disabled and TCP urgent data received on a socket is silently discarded. |
void |
setPerformancePreferences
Sets performance preferences for this socket. |
void |
setReceiveBufferSize
Sets the SO_RCVBUF option to the specified value for this Socket. |
void |
setReuseAddress
Enable/disable the SO_REUSEADDR socket option. |
void |
setSendBufferSize
Sets the SO_SNDBUF option to the specified value for this Socket. |
static void |
setSocketImplFactory
Sets the client socket implementation factory for the application. |
void |
setSoLinger
Enable/disable SO_LINGER with the specified linger time in seconds. |
void |
setSoTimeout
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. |
void |
setTcpNoDelay
Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm). |
void |
setTrafficClass
Sets traffic class or type-of-service octet in the IP header for packets sent from this Socket. |
void |
shutdownInput
Places the input stream for this socket at "end of stream". |
void |
shutdownOutput
Disables the output stream for this socket. |
String |
toString
Converts this socket to a String. |
Methods inherited from class java.lang.Object ![]() |
|---|
clone |
| Constructor Detail |
|---|

public Socket()

public Socket(Proxy![]()
![]()
proxy)
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.
proxy - a Proxy
object specifying what kind
of proxying should be used.
IllegalArgumentException

- if the proxy is of an invalid type
or null.
SecurityException

- if a security manager is present and
permission to connect to the proxy is
denied.ProxySelector
,
Proxy


protected Socket(SocketImpl![]()
![]()
impl) throws SocketException
![]()
![]()
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) throws UnknownHostException
![]()
![]()
, IOException
![]()
![]()
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.
host - the host name, or null for the loopback address.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.setSocketImplFactory(java.net.SocketImplFactory)
,
SocketImpl
,
SocketImplFactory.createSocketImpl()
,
SecurityManager.checkConnect(java.lang.String, int)


public Socket(InetAddress![]()
![]()
address, int port) throws IOException
![]()
![]()
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.setSocketImplFactory(java.net.SocketImplFactory)
,
SocketImpl
,
SocketImplFactory.createSocketImpl()
,
SecurityManager.checkConnect(java.lang.String, int)


public Socket(String![]()
![]()
host, int port, InetAddress
![]()
![]()
localAddr, int localPort) throws IOException
![]()
![]()
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.
host - the name of the remote host, or null for the loopback address.port - the remote portlocalAddr - the local address the socket is bound tolocalPort - the local port the socket is bound to
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.SecurityManager.checkConnect(java.lang.String, int)


public Socket(InetAddress![]()
![]()
address, int port, InetAddress
![]()
![]()
localAddr, int localPort) throws IOException
![]()
![]()
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 to
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.SecurityManager.checkConnect(java.lang.String, int)


@Deprecated public Socket(String![]()
![]()
host, int port, boolean stream) throws IOException
![]()
![]()
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.
host - the host name, or null for the loopback address.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.setSocketImplFactory(java.net.SocketImplFactory)
,
SocketImpl
,
SocketImplFactory.createSocketImpl()
,
SecurityManager.checkConnect(java.lang.String, int)


@Deprecated public Socket(InetAddress![]()
![]()
host, int port, boolean stream) throws IOException
![]()
![]()
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.
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.setSocketImplFactory(java.net.SocketImplFactory)
,
SocketImpl
,
SocketImplFactory.createSocketImpl()
,
SecurityManager.checkConnect(java.lang.String, int)
