?? proxyserver.java
字號:
import java.io.*;
import java.net.*;
import java.util.*;
// main() 方法啟動代理服務器。具體由內嵌的Proxy類實現
public class ProxyServer {
public static void main(String[] args) {
try {
// 檢查args參數數
if ((args.length == 0) || (args.length % 3 != 0))
throw new IllegalArgumentException("Wrong number of args");
// 創建Server對象
Server s = new Server(null, 12);
// 對每個循環創建一個 Proxy并把它添加到 server.
int i = 0;
while(i < args.length) {
String host = args[i++];
int remoteport = Integer.parseInt(args[i++]);
int localport = Integer.parseInt(args[i++]);
s.addService(new Proxy(host, remoteport), localport);
}
}
catch (Exception e) { // 若出錯打印錯誤信息
System.err.println(e);
System.err.println("Usage: java ProxyServer " +"<host> <remoteport> <localport> ...");
System.exit(1);
}
}
// 接下來是一個實現了代理服務的類。當有客戶端連接時調用serve()方法
// 此時必須首先建立一個連接并在客戶和服務器端之間傳輸數據
// 這個類實現了兩個相似線程作為匿名類, 一個線程從客戶端拷貝數據到服務器端
// 另一個從服務器端到客戶端拷貝數據。線程調用serve()方法創建和啟動這些線程,然后等待
// 它們退出
public static class Proxy implements Server.Service {
String host;
int port;
// 記住代理的主機和端口
public Proxy(String host, int port) {
this.host = host;
this.port = port;
}
// 當有一個客戶端連接時服務器端調用這個方法
public void serve(InputStream in, OutputStream out) {
final InputStream from_client = in;
final OutputStream to_client = out;
final InputStream from_server;
final OutputStream to_server;
// 建立到某一個服務器端端口的連接并返回出錯信息
final Socket server;
try {
server = new Socket(host, port);
from_server = server.getInputStream();
to_server = server.getOutputStream();
}
catch (Exception e) {
PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
pw.print("Proxy server could not connect to " + host +":" + port + "\n");
pw.flush();
pw.close();
try { in.close(); } catch (IOException ex) {}
return;
}
// 建立一個數組保存兩個線程。
final Thread[] threads = new Thread[2];
// 建立一個線程從客戶端到服務器端拷貝數據
Thread c2s = new Thread() {
public void run() {
byte[] buffer = new byte[2048];
int bytes_read;
try {
while((bytes_read=from_client.read(buffer))!=-1) {
to_server.write(buffer, 0, bytes_read);
to_server.flush();
}
}
catch (IOException e) {}
finally {
// 當線程任務結束時
try {
server.close();
to_client.close();
from_client.close();
}
catch (IOException e) {}
}
}
};
// 建立一個線程從服務器端到客戶端拷貝數據.
// 這些線程和上面那個工作機理一樣
Thread s2c = new Thread() {
public void run() {
byte[] buffer = new byte[2048];
int bytes_read;
try {
while((bytes_read=from_server.read(buffer))!=-1) {
to_client.write(buffer, 0, bytes_read);
to_client.flush();
}
}
catch (IOException e) {}
finally {
try {
server.close(); // 關閉服務器
to_client.close();
from_client.close();
} catch (IOException e) {}
}
}
};
// 把線程保存在 threads[]數組中以便匿名類之間可以相互引用
threads[0] = c2s; threads[1] = s2c;
// 啟動線程
c2s.start(); s2c.start();
// 等待他們退出
try { c2s.join(); s2c.join(); } catch (InterruptedException e) {}
}
}
}
// 下面的多線程server類框架,實現對端口的監聽和數據傳輸的實時控制
class Server { // 定義Server 類
// server的狀態量
Map services;
Set connections;
int maxConnections;
ThreadGroup threadGroup;
PrintWriter logStream;
public Server(OutputStream logStream, int maxConnections) { // 構造函數
setLogStream(logStream);
log("Starting server"); // 記錄日志信息
threadGroup = new ThreadGroup(Server.class.getName());
this.maxConnections = maxConnections;
services = new HashMap();
connections = new HashSet(maxConnections);
}
// 一個公共的方法 來設置當前登陸流,傳遞null來關閉登陸
public synchronized void setLogStream(OutputStream out) {
if (out != null) logStream = new PrintWriter(out);
else logStream = null;
}
// 記錄日志函數
protected synchronized void log(String s) {
if (logStream != null) {
logStream.println("[" + new Date() + "] " + s);
logStream.flush();
}
}
protected void log(Object o) { log(o.toString()); } // 把某個對象object寫入log
// 使server在特定端口開特定服務
public synchronized void addService(Service service, int port) throws IOException
{
Integer key = new Integer(port);
// 檢查在某個端口是否已經有其它service
if (services.get(key) != null)
throw new IllegalArgumentException("端口 " + port +" 已經被使用.");
// 創建監聽類來監聽端口的連接情況
Listener listener = new Listener(threadGroup, port, service);
// 保存在哈希表中
services.put(key, listener);
// 寫log
log("啟動服務: " + service.getClass().getName() + " 端口為: " + port);
// 開始監聽
listener.start();
}
// 使server停止某個端口上的服務
public synchronized void removeService(int port) {
Integer key = new Integer(port); // 哈希表關鍵字
// 在哈希表中查找對某個端口的監聽對象
final Listener listener = (Listener) services.get(key);
if (listener == null) return;
// 使得監聽器停止
listener.pleaseStop();
// 從哈希表中刪除
services.remove(key);
// 寫log.
log("停止服務: " + listener.service.getClass().getName() + " 端口: " + port);
}
}
// 下面是一個監聽器,它監聽來自某一個特定端口的連接。
// 當它獲得一個連接請求時,它調用服務器的addConnection()方法來接受或拒絕連接
public class Listener extends Thread {
ServerSocket listen_socket; // 監聽連接的套接字
int port; // 端口
Service service; // 在端口上實現的服務
volatile boolean stop = false; // 標志是否被請求停止
// 創建一個服務器套接字來監聽某個特定端口的連接
public Listener(ThreadGroup group, int port, Service service) throws IOException
{
super(group, "Listener:" + port);
listen_socket = new ServerSocket(port);
// 給出一個非零超時信號使得accept()被中斷
listen_socket.setSoTimeout(600000);
this.port = port;
this.service = service;
}
// 停止接收連接
public void pleaseStop() {
this.stop = true; // 設置停止標志位
this.interrupt(); // 停止阻斷accept()
try { listen_socket.close(); } // 停止監聽.
catch(IOException e) {}
}
// 等待連接請求,接受,接著把socket傳給sever的addConnection方法
public void run() {
while(!stop) { // 循環直到要求停止
try {
Socket client = listen_socket.accept();
addConnection(client, service);
}
catch (InterruptedIOException e) {}
catch (IOException e) {log(e);}
}
}
// 當接受客戶端連接以后這個方法將會被監聽器調用
// 或者創建一個連接類,或者把這個連接加入到現有的連接列表中,抑或關閉連接
protected synchronized void addConnection(Socket s, Service service) {
// 如果到達連接上限
if (connections.size() >= maxConnections) {
try {
// 則告訴客戶端正在被拒絕.
PrintWriter out = new PrintWriter(s.getOutputStream());
out.print("連接被拒絕; " +"服務器忙,請稍后再試.\n");
out.flush();
//關閉連接
s.close();
// 寫log
log("連接被拒絕:" +s.getInetAddress().getHostAddress() +
":" + s.getPort() + ": 達到最大連接數.");
} catch (IOException e) {log(e);}
}
else { // 如果連接數未到上限
// 創建一個線程處理連接
Connection c = new Connection(s, service);
// 并把它添加到當前連接列表中
connections.add(c);
// 把這個新的連接寫入log
log("Connected to " + s.getInetAddress().getHostAddress() +":" + s.getPort() + " on port " +
s.getLocalPort() +" for service " + service.getClass().getName());
// 開始連接線程提供服務
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -