Java Reference
In-Depth Information
public
public static
short PORT = 9999 ;
/** The name of the network interface. */
public
static final
final short
public static
final String INSIDE_HOST = "acmewidgets-inside" ;
/** The number of clients allowed to queue */
public
static final
public static
static final
final int
int BACKLOG = 10 ;
public
public static
static void
void main ( String [] argv ) throws
throws IOException {
ServerSocket sock ;
Socket clientSock ;
try
try {
sock = new
new ServerSocket ( PORT , BACKLOG ,
InetAddress . getByName ( INSIDE_HOST ));
while
while (( clientSock = sock . accept ()) != null
null ) {
// Process it.
process ( clientSock );
}
} catch
catch ( IOException e ) {
System . err . println ( e );
}
}
/** Hold server's conversation with one client. */
static
static void
throws IOException {
System . out . println ( "Connected from " + INSIDE_HOST +
": " + s . getInetAddress ( ));
// The conversation would be here.
s . close ();
void process ( Socket s ) throws
}
}
The InetAddress.getByName() looks up the given hostname in a system-dependent way,
referring to a configuration file in the /etc or \windows directory, or to some kind of resolver
such as the Domain Name System. Consult a good book on networking and system adminis-
tration if you need to modify this data.
Returning a Response (String or Binary)
Problem
You need to write a string or binary data to the client.
Search WWH ::




Custom Search