|
NOTE:
These slides have not been updated since 2003. They have been superseded by the book
Anders Møller and Michael Schwartzbach, February 2006 |
|
| INTERACTIVE WEB SERVICES WITH JAVA |
|
Read command-line arguments:
import java.net.*;
import java.io.*;
import java.util.*;
public class MultiClient
{
public static void main(String[] args)
{
// read arguments
if (args.length!=2) {
System.out.println("Usage: java MultiClient <host> <port>");
System.exit(-1);
}
String host = args[0];
int port = Integer.parseInt(args[1]);
System.out.println("MultiClient 1.0");
System.out.println("Enter request followed by one empty line or 'quit' to quit.");
BufferedReader user = new BufferedReader(new InputStreamReader(System.in));
try {
|
Read user's request and send it to the server:
mainloop:
while (true) {
// read user request
StringBuffer req = new StringBuffer();
boolean done = false, first = true;
while (!done) {
// get a line
System.out.print(host + ":" + port + "> ");
String line = user.readLine();
if (line.equals("quit"))
break mainloop;
req.append(line).append("\r\n");
if (line.length()==0 && !first)
done = true; // done when reading blank line
first = false;
}
|
Make connection to server and send the request:
Socket socket = null;
try {
// create socket and connect (don't occupy port too long)
socket = new Socket(host, port);
socket.setSoTimeout(60000); // set timeout to 1 minute
PrintStream out = new PrintStream(socket.getOutputStream());
out.print(req); // send bytes in default encoding
out.flush();
|
Get server reply and print it to standard out:
// show reply
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true) {
String line = in.readLine();
if (line==null)
break;
System.out.println(line);
}
|
Close connection and catch exceptions:
} catch (Exception e) { System.err.println(e); }
if (socket!=null)
socket.close(); // close connection
}
}
catch (Exception e) { System.err.println(e); }
}
}
|
- a good starting point for making a simple Web browser - "just" add an HTML parser and a GUI (as Notscape using javax.swing.text.html)...
|
| COPYRIGHT © 2002-2003 ANDERS MØLLER & MICHAEL I. SCHWARTZBACH |
|