Java Reference
In-Depth Information
Socket isusedtocreateasocketontheclientside.Itdeclaresseveralconstructors,
including the following pair:
Socket(InetAddress address, int port) createsastreamsocket
and connects it to the specified port number at the specified IP address .
Thisconstructorthrows java.io.IOException whenanI/Oerroroccurs
while creating the socket, java.lang.IllegalArgumentException
when the argument passed to port is outside the valid range of port values,
which is 0 through 65535, and java.lang.NullPointerException
when address is null .
Socket(String host, int port) createsastreamsocketandconnects
it to the specified port number on the named host . When host is null ,
this constructor is equivalent to invoking Sock-
et(InetAddress.getByName(null), port) . It throws the same
IOException and IllegalArgumentException instances as the pre-
viousconstructor.However,insteadofthrowing NullPointerException ,
it throws UnknownHostException when the host's IP address couldn't be
determined.
When a Socket instance is created via these constructors, it binds to an arbitrary
localhostsocketaddressbeforeconnectingtotheremotehostsocketaddress. Binding
makes a client socket address available to a server socket so that a server process can
communicate with the client process via the server socket.
Socket offers additional constructors to give you flexibility. For example, Sock-
et() and Socket(Proxy proxy) create unbound and unconnected sockets. Be-
fore you can use these sockets, you need to bind them to local socket addresses
by calling void bind(SocketAddress bindpoint) , and then make con-
nections by calling Socket 's connect() methods, such as void
con-
nect(SocketAddress endpoint) .
Note A proxy isacomputerthatsitsbetweenanintranetandtheInternetforsecurity
purposes.Proxysettingsarerepresentedbyinstancesofthe java.net.Proxy class
and help sockets communicate through proxies.
Another constructor is Socket(InetAddress address, int port,
InetAddress localAddr, int localPort) , which lets you specify your
own local host socket address via localAddr and localPort . This constructor
automaticallybindstothelocalsocketaddressandthenattemptsaconnectiontothere-
mote address .
Search WWH ::




Custom Search