Java Reference
In-Depth Information
Socket clientSocket ;
// Wait here for the next connection.
synchronized
synchronized ( servSock ) {
clientSocket = servSock . accept ();
}
System . out . println ( getName () + " starting, IP=" +
clientSocket . getInetAddress ());
BufferedReader is = new
new BufferedReader (
new InputStreamReader ( clientSocket . getInputStream ()));
PrintStream os = new
new
new PrintStream (
clientSocket . getOutputStream (), true
true );
String line ;
while
while (( line = is . readLine ()) != null
null ) {
os . print ( line + "\r\n" );
os . flush ();
}
System . out . println ( getName () + " ENDED " );
clientSocket . close ();
} catch
catch ( IOException ex ) {
System . out . println ( getName () + ": IO Error on socket " + ex );
return
return ;
}
}
}
}
}
It is quite possible to implement a server of this sort with NIO, the “new” (back in J2SE 1.4)
IO package. However, the code to do so outweighs anything in this chapter, and it is fraught
with “issues.” There are several good tutorials on the Internet for the person who truly needs
the performance gain of using NIO to manage server connections.
Serving the HTTP Protocol
Problem
You want to serve up a protocol such as HTTP.
Search WWH ::




Custom Search