Java学习:一个基础的代理服务器类
2008-01-05 20:13:36 来源:WEB开发网 /*************************************
* 一个基础的代理服务器类
*************************************
*/
import java.net.*;
import java.io.*;
public class HttpPRoxy extends Thread {
static public int CONNECT_RETRIES=5;
static public int CONNECT_PAUSE=5;
static public int TIMEOUT=50;
static public int BUFSIZ=1024;
static public boolean logging = false;
static public OutputStream log=null;
// 传入数据用的Socket
protected Socket socket;
// 上级代理服务器,可选
static private String parent=null;
static private int parentPort=-1;
static public void setParentProxy(String name, int pport) {
parent=name;
parentPort=pport;
}
// 在给定Socket上创建一个代理线程。
public HttpProxy(Socket s) { socket=s; start(); }
public void writeLog(int c, boolean browser) throws IOException {
log.write(c);
}
public void writeLog(byte[] bytes,int offset, int len, boolean browser) throws IOException {
for (int i=0;i<len;i++) writeLog((int)bytes[offset+i],browser);
}
// 默认情况下,日志信息输出到
// 标准输出设备
// 派生类可以覆盖它
public String processHostName(String url, String host, int port, Socket sock) {
java.text.DateFormat cal=java.text.DateFormat.getDateTimeInstance();
System.out.println(cal.format(new java.util.Date()) + " - " + url + " "
+ sock.getInetAddress()+"\n");
return host;
}
// 执行操作的线程
public void run() {
String line;
String host;
int port=80;
Socket outbound=null;
try {
socket.setSoTimeout(TIMEOUT);
更多精彩
赞助商链接