Java Reference
In-Depth Information
SocketAddress proxyAddress = new InetSocketAddress ( "myproxy.example.com" , 1080 );
Proxy proxy = new Proxy ( Proxy . Type . SOCKS , proxyAddress )
Socket s = new Socket ( proxy );
SocketAddress remote = new InetSocketAddress ( "login.ibiblio.org" , 25 );
s . connect ( remote );
SOCKS is the only low-level proxy type Java understands. There's also a high-level
Proxy.Type.HTTP that works in the application layer rather than the transport layer and
a Proxy.Type.DIRECT that represents proxyless connections.
Getting Information About a Socket
Socket objects have several properties that are accessible through getter methods:
• Remote address
• Remote port
• Local address
• Local port
Here are the getter methods for accessing these properties:
public InetAddress getInetAddress ()
public int getPort ()
public InetAddress getLocalAddress ()
public int getLocalPort ()
There are no setter methods. These properties are set as soon as the socket connects,
and are fixed from there on.
The getInetAddress() and getPort() methods tell you the remote host and port the
Socket is connected to; or, if the connection is now closed, which host and port the
Socket was connected to when it was connected. The getLocalAddress() and getLo
calPort() methods tell you the network interface and port the Socket is connected
from.
Unlike the remote port, which (for a client socket) is usually a “well-known port” that
has been preassigned by a standards committee, the local port is usually chosen by the
system at runtime from the available unused ports. This way, many different clients on
a system can access the same service at the same time. The local port is embedded in
outbound IP packets along with the local host's IP address, so the server can send data
back to the right port on the client.
Example 8-6 reads a list of hostnames from the command line, attempts to open a socket
to each one, and then uses these four methods to print the remote host, the remote port,
the local address, and the local port.
Search WWH ::




Custom Search