Java Reference
In-Depth Information
} catch
catch ( IOException e ) {
System . err . println ( e );
}
}
/** This would do something with one client. */
static
static void
throws IOException {
System . out . println ( "Accept from client " + s . getInetAddress ());
// The conversation would be here.
s . close ();
void process ( Socket s ) throws
}
}
You would normally use the same socket for both reading and writing, as shown in the next
few recipes.
You may want to listen only on a particular network interface. Though we tend to think of
network addresses as computer addresses, the two are not the same. A network address is ac-
tually the address of a particular network card, or network interface connection, on a given
computing device. A desktop computer, laptop, Palm handheld, or cellular phone might have
only a single interface, hence a single network address. But a large server machine might
have two or more interfaces, usually when it is connected to several networks. A network
router is a box, either special purpose (e.g., a Cisco router), or general purpose (e.g., a Unix
host), that has interfaces on multiple networks and has both the capability and the adminis-
trative permission to forward packets from one network to another. A program running on
such a server machine might want to provide services only to its inside network or its outside
network. One way to accomplish this is by specifying the network interface to be listened on.
Suppose you want to provide a different view of web pages for your intranet than you
provide to outside customers. For security reasons, you probably wouldn't run both these ser-
vices on the same machine. But if you wanted to, you could do this by providing the network
interface addresses as arguments to the ServerSocket constructor.
However, to use this form of the constructor, you don't have the option of using a string for
the network address's name, as you did with the client socket; you must convert it to an
InetAddress object. You also have to provide a backlog argument, which is the number of
connections that can queue up to be accepted before clients are told that your server is too
busy. The complete setup is shown in Example 16-2 .
Example 16-2. ListenInside.java
public
public class
ListenInside {
/** The TCP port for the service. */
class ListenInside
Search WWH ::




Custom Search