隨著 Java 語言普及及其與 Internet Web 的緊密結合,其跨平台、面向物件、分散式、多執行緒與完善安全機制等特性,使其成為現代資訊系統建設的優質開發平台與執行環境。
完整 Java 應用環境本質上是瀏覽器/伺服器模型(B/S),區別於傳統 C/S 兩層結構。Web 模型將伺服器端分解為 應用伺服器 (Web Server) 與 資料庫伺服器,形成三層架構。
Java 網路類庫為分散式計算提供強力支援:
Java 網路類庫支援多種 Internet 協定,核心子類庫包括:
| 子類庫 | 功能 |
|---|---|
| java.net | 基礎網路功能(含遠程登入 Telnet) |
| java.net.ftp | FTP 協定處理 |
| java.net.www.content | WWW 頁面內容處理 |
| java.net.www.html | HTML 語言支援 |
| java.net.www.http | HTTP 協定支援 |
基於連接的網路通訊模式:定義通訊協定 → 建立 Socket 可靠連線 → 客戶端發請求 → 伺服器監聽響應 → 可靠傳輸資料。典型「請求-應答」流程:
// Server.java
import java.io.*; import java.net.*;
public class Server extends Thread {
public final static int Default_Port = 6543;
protected int port;
protected ServerSocket listen_socket;
public static void fail(Exception e, String msg) {
System.err.println(msg + ": " + e); System.exit(1);
}
public Server(int port) {
if (port == 0) port = Default_Port;
this.port = port;
try { listen_socket = new ServerSocket(port); }
catch (IOException e) { fail(e, "Exception creating server socket"); }
System.out.println("Server: listening on port " + port);
this.start();
}
public void run() {
try {
while (true) {
Socket client_socket = listen_socket.accept();
new Connection(client_socket);
}
} catch (IOException e) { fail(e, "Exception while listening for connections"); }
}
public static void main(String args[]) {
int port = 0;
if (args.length == 1) {
try { port = Integer.parseInt(args[0]); }
catch (NumberFormatException e) { port = 0; }
}
new Server(port);
}
}
class Connection extends Thread {
protected Socket client;
protected DataInputStream in;
protected PrintStream out;
public Connection(Socket client_socket) {
client = client_socket;
try {
in = new DataInputStream(client.getInputStream());
out = new PrintStream(client.getOutputStream());
} catch (IOException e) {
try { client.close(); } catch (IOException e2) {}
System.err.println("Exception while getting socket stream: " + e);
return;
}
this.start();
}
public void run() {
try {
for (;;) {
String line = in.readLine();
if (line == null) break;
int len = line.length();
StringBuffer revline = new StringBuffer(len);
for (int i = len-1; i >= 0; i--)
revline.append(line.charAt(i));
out.println(revline.toString());
}
} catch (IOException e) {}
finally {
try { client.close(); } catch (IOException e2) {}
}
}
}
// Client.java
import java.io.*; import java.net.*;
public class Client {
public static final int Default_Port = 6543;
public static void usage() {
System.out.println("Usage: java Client [port]");
System.exit(0);
}
public static void main(String args[]) {
int port = Default_Port;
Socket s = null;
if ((args.length != 1) && (args.length != 2)) usage();
if (args.length == 2) {
try { port = Integer.parseInt(args[1]); }
catch (NumberFormatException e) { usage(); }
}
try {
s = new Socket(args[0], port);
DataInputStream sin = new DataInputStream(s.getInputStream());
PrintStream sout = new PrintStream(s.getOutputStream());
DataInputStream in = new DataInputStream(System.in);
System.out.println("Connected to " + s.getInetAddress() + ":" + s.getPort());
for (;;) {
System.out.print(" > "); System.out.flush();
String line = in.readLine();
if (line == null) break;
sout.println(line);
line = sin.readLine();
if (line == null) {
System.out.println("Connection closed by server.");
break;
}
System.out.println(line);
}
} catch (IOException e) { System.err.println(e); }
finally {
try { if (s != null) s.close(); } catch (IOException e2) {}
}
}
}
執行客戶端程式需指定伺服器主機名為第一參數,埠號為第二參數(可省略)。本程式在 NT 4.0 TCP/IP 環境下經 JDK 1.1 測試通過。