?? server.java
字號:
import java.net.*;
import java.io.*;
import java.util.Date.*;
import java.awt.*;
import java.awt.event.*;
public class Server implements Runnable
{
private abc frame;
//在放棄之前嘗試連接遠程主機的次數
public int CONNECT_RETRIES=5;
//在兩次連接嘗試之間的暫停時間
public int CONNECT_PAUSE=5;
//等待Socket輸入的等待時間
public int TIMEOUT=50;
//Socket輸入的緩沖大小
public int BUFSIZ=1024;
//是否要求代理服務器在日志中記錄所有已傳輸的數據(true表示“是”)。
static public boolean logging = false;
//一個OutputStream對象,默認日志例程將向該OutputStream對象輸出日志信息
static public OutputStream log=null;
//允許最大連接
static public int MAXUSER=20;
//用戶連接數量
static public int count=0;
//Socket對象
protected Socket socket;
private String parent=null;
private int parentPort=-1;
public void setParentProxy(String name, int pport)
{
parent=name;
parentPort=pport;
}
public Server(Socket s,abc frame)
{
socket=s;
this.frame=frame;
}
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");
frame.jLog.append(cal.format(new java.util.Date()) + " - " + url + " "
+ sock.getInetAddress()+"\n");
return host;
}
public void run()
{
if(count>MAXUSER)
{
//用戶超過指定連接數目,向用戶發送用戶過多網頁
try
{
System.out.println("用戶超過指定");
PrintStream out =new PrintStream(socket.getOutputStream());
out.println("HTTP/1.0 200 OK");
out.println("MIME_version:1.0");
out.println("Content_Type:text/html");
String msg="<html> <head</head><body> <hl> 連接用戶數量過多,請稍后再試!</jl></Body></html>";
out.println("Content_Length:"+msg.length());
out.println(" ");
out.print(msg);
}catch (IOException ex){}
try
{
socket.close();
}
catch (Exception e1) {}
}
else
{
count++;
String line;
String host;
int port=80;
Socket outbound=null;
try
{
//Socket的超時時間
socket.setSoTimeout(TIMEOUT);
//獲得輸入流
InputStream is=socket.getInputStream();
OutputStream os=null;
try
{
// 獲取請求行的內容
line="";
host="";
int state=0;
boolean space;
while (true)
{
//字節流結束返回-1
int c=is.read();
if (c==-1)
break;
if (logging)
writeLog(c,true);
space=Character.isWhitespace((char)c);
switch (state)
{
case 0:
if (space)
// 跳過多個空白字符
continue;
state=1;
case 1:
if (space)
{
state=2;
continue;
}
line=line+(char)c;
break;
case 2:
if (space)
// 跳過多個空白字符
continue;
state=3;
case 3:
if (space)
{
state=4;
// 只取出主機名稱部分
String host0=host;
int n;
n=host.indexOf("//");
if (n!=-1)
host=host.substring(n+2);
n=host.indexOf('/');
if (n!=-1)
host=host.substring(0,n);
// 分析可能存在的端口號
n=host.indexOf(":");
if (n!=-1)
{
port=Integer.parseInt(host.substring(n+1));
host=host.substring(0,n);
}
host=processHostName(host0,host,port,socket);
if (parent!=null)
{
host=parent;
port=parentPort;
}
int retry=CONNECT_RETRIES;
while (retry--!=0)
{
try
{
outbound=new Socket(host,port);
break;
}
catch (Exception e) { }
// 等待
Thread.sleep(CONNECT_PAUSE);
}
if (outbound==null)
{
//請求的網頁無法獲得的情況
System.out.println("獲取網頁不存在或者服務器無法取得網頁");
PrintStream out =new PrintStream(socket.getOutputStream());
out.println("HTTP/1.0 200 OK");
out.println("MIME_version:1.0");
out.println("Content_Type:text/html");
String msg="<html> <head</head><body> <hl> 你所請求的網頁不存在,或服務器無法獲得網頁,請與管理員聯系!</jl></Body></html>";
out.println("Content_Length:"+msg.length());
out.println(" ");
out.print(msg);
try
{
socket.close();
count--;
}
catch (Exception e1) {}
break;
}
//打開Socket之后,run先把部分的請求寫入Socket,然后調用pipe方法。
outbound.setSoTimeout(TIMEOUT);
os=outbound.getOutputStream();
os.write(line.getBytes()); //line=get
os.write(' ');
os.write(host0.getBytes()); //獲得的ie信息
os.write(' ');
pipe(is,outbound.getInputStream(),os,socket.getOutputStream());
break;
}
host=host+(char)c;
break;
}
}
}
catch (IOException e) { }
}
catch (Exception e) { }
finally
{
try
{
socket.close();
count--;
}
catch (Exception e1) {}
try
{
outbound.close();
}
catch (Exception e2) {}
}
}
}
//pipe方法直接在兩個Socket之間以最快的速度執行讀寫操作。
void pipe(InputStream is0, InputStream is1,
OutputStream os0, OutputStream os1) throws IOException
{
try
{
int ir;
byte bytes[]=new byte[BUFSIZ];
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("代理服務器的轉換出現了異常 " + e0);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -