Java Reference
In-Depth Information
// For text output we wrap an OutputStreamWriter around
// the raw output stream and set ASCII character encoding.
OutputStreamWriter osr =
new OutputStreamWriter (out, "8859 - 1");
// Finally, we use a PrintWriter wrapper to obtain its
// higher level output methods. Use autoflush mode.
// (Autoflush occurs only with println().)
PrintWriter pw - client - out = new PrintWriter (osr, true);
...
At this point in the code we have streams for both input and output communica-
tions with the client, but we haven't said anything about just what they commu-
nicate. The client can place arbitrary bytes on the stream and the server will see
them, but unless some agreement is made about what form those bytes should
take and what they mean, the communication that happens is rather meaningless.
In other words, we need to develop some sort of protocol so that the server and
the client can understand each other.
14.3 Hypertext Transfer Protocol (HTTP)
For network communications to work correctly, a common format or protocol
must be established. Many standard protocols have already been defined - for
example, HTTP (Hypertext Transfer Protocol). In an HTTP request, a line such
as the following must be sent from the client to the server:
\ r \ n \ r \ n
GET /index.html HTTP/1.0
Here GET is the request keyword, /index.html is the file requested, and
HTTP/1.0 indicates the protocol and version number of the protocol to be used.
Finally, the characters
\ r \ n \ r \ n indicate the two carriage return/linefeed
pairs that terminate the line.
The next code snippet from run() obtains the request line sent from the
client by invoking the readLine() method of BufferedReader . Then the
request text is broken into tokens with the split() method in the String
class (see Chapter 10). The tokens are checked to determine if the client sent
the GET command and, if so, to obtain the name of the file that the client is
requesting.
... In the run() method in Worker ...
// First read the message line from the client
String client - str = client - in.readLine ();
Search WWH ::




Custom Search