while (true) {
try {
if ((ir=is0.read(bytes))>0) {
os0.write(bytes,0,ir);
if (logging) writeLog(bytes,0,ir,true);
}
else if (ir<0)
break;
} catch (InterruptedIOException e) { }
try {
if ((ir=is1.read(bytes))>0) {
os1.write(bytes,0,ir);
if (logging) writeLog(bytes,0,ir,false);
}
else if (ir<0)
break;
} catch (InterruptedIOException e) { }
}
} catch (Exception e0) {
System.out.println("Pipe异常: " e0);
}
}
static public void startProxy(int port,Class clobj) {
ServerSocket ssock;
Socket sock;
try {
ssock=new ServerSocket(port);
while (true) {
Class [] sarg = new Class[1];
Object [] arg= new Object[1];
sarg[0]=Socket.class;
try {
java.lang.reflect.Constructor cons = clobj.getDeclaredConstructor(sarg);
arg[0]=ssock.accept();
cons.newInstance(arg); // 创建HttpProxy或其派生类的实例
} catch (Exception e) {
Socket esock = (Socket)arg[0];
try { esock.close(); } catch (Exception ec) {}
}
}
} catch (IOException e) {
}
}
// 测试用的简单main方法
static public void main(String args[]) {
System.out.println("在端口8080启动代理服务器\n");
HttpProxy.log=System.out;
HttpProxy.logging=false;
HttpProxy.startProxy(8080,HttpProxy.class);
}
}