从这个中篇开始就没说session和sessions到底是什么
《 {用Delphi设计代理服务器(中) 》
{//主窗口建立
procedure TForm1.FormCreate(Sender: TObject);
begin
Service_Enabled:=false;
timer2.Enabled:=true;{窗口建立时,打开定时器}
end;
//窗口关闭时
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
timer1.Enabled:=false;{关闭定时器}
if Service_Enabled then
serversocket1.Active:=false;{退出程序时关闭服务}
end;
//退出程序按钮
procedure TForm1.N01Click(Sender: TObject);
begin
form1.Close;{退出程序}
end;
//开启代理服务后
procedure TForm1.ServerSocket1Listen(Sender: TObject;
Socket: TCustomWinSocket);
begin
Service_Enabled:=true;{置正在服务标志}
N11.Enabled:=false;
N21.Enabled:=true;
end;
//被代理端连接到代理服务器后,建立一个会话,并与套接字绑定...
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j: integer;
begin
j:=-1;
for i:=1 to sessions do{查找是否有空白项}
if not session[i-1].Used and not session[i-1].CSocket.active then
begin
j:=i-1;{有,分配它}
session[j].Used:=true;{置为在用}
break;
end
else
if not session[i-1].Used and session[i-1].CSocket.active then
session[i-1].CSocket.active:=false;
if j=-1 then
begin{无,新增一个}
j:=sessions;
inc(sessions);
setlength(session,sessions);
session[j].Used:=true;{置为在用}
session[j].CSocket:=TClientSocket.Create(nil);
session[j].CSocket.OnConnect:=ClientSocket1Connect;
session[j].CSocket.OnDisconnect:=ClientSocket1Disconnect;
session[j].CSocket.OnError:=ClientSocket1Error;
session[j].CSocket.OnRead:=ClientSocket1Read;
session[j].CSocket.OnWrite:=ClientSocket1Write;
session[j].Lookingup:=false;
end;
session[j].SS_Handle:=socket.socketHandle; {保存句柄,实现绑定}
session[j].Request:=false;{无请求}
session[j].client_connected:=true;{客户机已连接}
session[j].remote_connected:=false;{远程未连接}
edit1.text:=inttostr(sessions);
end;
//被代理端断开时
procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
var
i,j,k: integer;
begin
for i:=1 to sessions do
if (session[i-1].SS_Handle=socket.SocketHandle) and session[i-1].Used then
begin
session[i-1].client_connected:=false; {客户机未连接}
if session[i-1].remote_connected then
session[i-1].CSocket.active:=false {假如远程尚连接,断开它}
else
session[i-1].Used:=false;{假如两者都断开,则置释放资源标志}
break;
end;
j:=sessions;
k:=0;
for i:=1 to j do{统计会话数组尾部有几个未用项}
begin
if session[j-i].Used then
break;
inc(k);
end;
if k>0 then{修正会话数组,释放尾部未用项}
begin
sessions:=sessions-k;
setlength(session,sessions);
end;
edit1.text:=inttostr(sessions);
end;
//通信错误出现时
procedure TForm1.ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
var
i,j,k: integer;
begin
for i:=1 to sessions do
if (session[i-1].SS_Handle=socket.SocketHandle) and session[i-1].Used then
begin
session[i-1].client_connected:=false;{客户机未连接}
if session[i-1].remote_connected then
session[i-1].CSocket.active:=false{假如远程尚连接,断开它}
else
session[i-1].Used:=false;{假如两者都断开,则置释放资源标志}
break;
end;
j:=sessions;
k:=0;
for i:=1 to j do
begin
if session[j-i].Used then
break;
inc(k);
end;
if k>0 then
begin
sessions:=sessions-k;
setlength(session,sessions);
end;
edit1.text:=inttostr(sessions);
errorcode:=0;
end;
//被代理端发送来页面请求时
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
tmp,line,host: string;
i,j,port: integer;
begin
for i:=1 to sessions do{判断是哪一个会话}
if session[i-1].Used and (session[i-1].SS_Handle=socket.sockethandle) then
begin
session[i-1].request_str:=socket.ReceiveText; {保存请求数据}
tmp:=session[i-1].request_str; {存放到临时变量}
memo1.lines.add(tmp);
j:=pos(char(13)+char(10),tmp);{一行标志}
while j>0 do{逐行扫描请求文本,查找主机地址}
begin
line:=copy(tmp,1,j-1);{取一行}
delete(tmp,1,j+1);{删除一行}
j:=pos('Host',line);{主机地址标志}
if j>0 then
begin
delete(line,1,j+5);{删除前面的无效字符}
j:=pos(':',line);
if j>0 then
begin
host:=copy(line,1,j-1);
delete(line,1,j);
try
port:=strtoint(line);
except
port:=80;
end;
end
else
begin
host:=trim(line);{获取主机地址}
port:=80;
end;
if not session[i-1].remote_connected then{假如远征尚未连接}
begin
session[i-1].Request:=true;{置请求数据就绪标志}
session[i-1].CSocket.host:=host;{设置远程主机地址}
session[i-1].CSocket.port:=port;{设置端口}
session[i-1].CSocket.active:=true;{连接远程主机}
session[i-1].Lookingup:=true;{置标志}
session[i-1].LookupTime:=0;{从0开始计时}
end
else
{假如远程已连接,直接发送请求}
session[i-1].CSocket.socket.sendtext(session[i-1].request_str);
break;{停止扫描请求文本}
end;
j:=pos(char(13)+char(10),tmp);{指向下一行}
end;
break;{停止循环}
end;
end;
//当连接远程主机成功时
procedure TForm1.ClientSocket1Connect(Sender: TObject;
Socket: TCustomWinSocket);
var
i: integer;
begin
for i:=1 to sessions do
if (session[i-1].CSocket.socket.sockethandle=socket.SocketHandle) and session[i-1].Used then
begin
session[i-1].CSocket.tag:=socket.SocketHandle;
session[i-1].remote_connected:=true;{置远程主机已连通标志}
session[i-1].Lookingup:=false;{清标志}
break;
end;
end;
(未完待续)}