Java Reference
In-Depth Information
find rogue servers: for example, LowPortScanner might tell you that there's a server on
port 800, which, on further investigation, turns out to be an HTTP server somebody is
running to serve MP3 files, and which is saturating your T1.
Three constructors create unconnected sockets. These provide more control over ex‐
actly how the underlying socket behaves, for instance by choosing a different proxy
server or an encryption scheme:
public Socket ()
public Socket ( Proxy proxy )
protected Socket ( SocketImpl impl )
Picking a Local Interface to Connect From
Two constructors specify both the host and port to connect to and the interface and
port to connect from :
public Socket ( String host , int port , InetAddress interface , int localPort )
throws IOException , UnknownHostException
public Socket ( InetAddress host , int port , InetAddress interface , int localPort )
throws IOException
This socket connects to the host and port specified in the first two arguments. It connects
from the local network interface and port specified by the last two arguments. The
network interface may be either physical (e.g., an Ethernet card) or virtual (a multi‐
homed host with more than one IP address). If 0 is passed for the localPort argument,
Java chooses a random available port between 1024 and 65535.
Selecting a particular network interface from which to send data is uncommon, but a
need does come up occasionally. One situation where you might want to explicitly
choose the local address would be on a router/firewall that uses dual Ethernet ports.
Incoming connections would be accepted on one interface, processed, and forwarded
to the local network from the other interface. Suppose you were writing a program to
periodically dump error logs to a printer or send them over an internal mail server.
You'd want to make sure you used the inward-facing network interface instead of the
outward-facing network interface. For example:
try {
InetAddress inward = InetAddress . getByName ( "router" );
Socket socket = new Socket ( "mail" , 25 , inward , 0 );
// work with the sockets...
} catch ( IOException ex ) {
System . err . println ( ex );
}
By passing 0 for the local port number, I say that I don't care which port is used but I
do want to use the network interface bound to the local hostname router .
Search WWH ::




Custom Search