Java Reference
In-Depth Information
... In class ClientApp ...
public void run () {
// Clear the text area
fTextArea.setText ("Downloading...");
try {
// Connect to the server via the given IP address
// and port number
fSocket = new Socket (fIpAddr, fPort);
// Assemble the message line.
String message ="GET /" + fFilename;
// Now get an output stream to the server.
OutputStream server - out = fSocket.getOutputStream ();
// Wrap in writer classes
PrintWriter pw - server - out = new PrintWriter (
new OutputStreamWriter (server - out, " 8859 - 1 " ), true);
// Send the message to the server
pw - server - out.println (message);
// Get the input stream from the server and then
// wrap the stream in two wrappers.
BufferedReader server - reader = new BufferedReader (
new InputStreamReader (socket.getInputStream ()));
fTextArea.setText ("");
String line;
// Add the text one line at a time from the server
// to the text area.
while ((line = server - reader.readLine ())!= null)
fTextArea.append (line + ' \ n');
}
catch (UnknownHostException uhe) {
fTextArea.setText ("Unknown host");
}
catch (IOException ioe) {
fTextArea.setText ( " I/O Exception " );
}
finally {
try {
// End the connection
fSocket.close ();
 
Search WWH ::




Custom Search