久久中文视频-久久中文网-久久中文亚洲国产-久久中文字幕久久久久-亚洲狠狠成人综合网-亚洲狠狠婷婷综合久久久久

Rss & SiteMap

曙海教育集團論壇 http://www.bjzhda.cn

曙海教育集團論壇
共1 條記錄, 每頁顯示 10 條, 頁簽: [1]
[瀏覽完整版]

標題:用Delphi設計代理服務器設計sessioni-1endbeginprocedureTObjectSender:

1樓
wangxinxin 發表于:2010-12-14 11:24:40
<br>用Delphi設計自己的代理服務器
<br>
<br>  筆者在編寫一個上網計費軟件時,涉及到如何對局域網中各工作站上網計費問題。一般來講,這些工作站通過代理服務器上網,而采用現成的代理服務器軟件時,由于代理服務器軟件是封閉的系統,很難編寫程序獲取實時的上網計時信息。因此,考慮是否能編寫自己的代理服務器,一方面解決群體上網,另一方面又解決上網的計費問題呢?
<br>  經過實驗性編程,終于圓滿地解決了該問題�,F寫出來,與各位同行分享。
<br>
<br>1、思路
<br>當前流行的瀏覽器的系統選項中有一個參數,即“通過代理服務器連接”,經過編程測
<br>試,當局域網中一臺工作站指定了該屬性,再發出Internet請求時,請求數據將發送到所指定的代理服務器上,以下為請求數據包示例:
<br>         GEThttp://home.microsoft.com/intl/cn/HTTP/1.0
<br>         Accept:*/*
<br>         Accept-Language:zh-cn
<br>         Accept-Encoding:gzip,deflate
<br>         User-Agent:Mozilla/4.0(compatible;MSIE5.0;WindowsNT)
<br>         Host:home.microsoft.com
<br>         Proxy-Connection:Keep-Alive
<br>其中第一行為目標URL及相關方法、協議,“Host”行指定了目標主機的地址。
<br>由此知道了代理服務的過程:接收被代理端的請求、連接真正的主機、接收主機返回的數據、將接收數據發送到被代理端。
<br>為此可編寫一個簡單的程序,完成上述網絡通信重定向問題。
<br>用Delphi設計時,選用ServerSocket作為與被代理工作站通信的套接字控件,選用ClientSocket動態數組作為與遠程主機通信的套接字控件。
<br>編程時應解決的一個重要問題是多重連接處理問題,為了加快代理服務的速度和被代理端的響應速度,套接字控件的屬性應設為非阻塞型;各通信會話與套接字動態綁定,用套接字的SocketHandle屬性值確定屬于哪一個會話。
<br>通信的銜接過程如下圖所示:
<br>
<br>                 代理服務器
<br>                 
<br>                 Serversocket
<br>            (1)     接 收
<br>     被代理端         發 送            遠程主機
<br>            (6)    (2)   (5)
<br>     Browser         ClientSocket   (4)      WebServer
<br>                  接 收
<br>                  發 送     (3)
<br>
<br>
<br>(1)、被代理端瀏覽器發出Web請求,代理服務器的Serversocket接收到請求。
<br>(2)、代理服務器程序自動創建一個ClientSocket,并設置主機地址、端口等屬性,然后連接遠程主機。
<br>(3)、遠程連通后激發發送事件,將Serversocket接收到的Web請求數據包發送到遠程主機。
<br>(4)、當遠程主機返回頁面數據時,激發ClientSocket的讀事件,讀取頁面數據。
<br>(5)、代理服務器程序根據綁定信息確定屬于ServerSocket控件中的哪一個Socket應該將從主機接收的頁面信息發送到被代理端。
<br>(6)、ServerSocket中的對應Socket將頁面數據發送到被代理端。
<br>
<br>2、程序編寫
<br>使用Delphi設計以上通信過程非常簡單,主要是ServerSocket、ClientSocket的相關事
<br>件驅動程序的程序編寫。下面給出作者編寫的實驗用代理服務器界面與源程序清單,內含簡要功能說明:
<br>
<br>unitmain;
<br>
<br>interface
<br>
<br>uses
<br> Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,
<br> ExtCtrls,ScktComp,TrayIcon,Menus,StdCtrls;
<br>
<br>type
<br>  session_record=record
<br>   Used:boolean;           
<br>   SS_Handle:integer;         
<br>   CSocket:TClientSocket;       
<br>   Lookingup:boolean;         
<br>   LookupTime:integer;        
<br>   Request:boolean;          
<br>   request_str:string;        
<br>   client_connected:boolean;     
<br>   remote_connected:boolean;     
<br>end;
<br>
<br>type
<br> TForm1=class(TForm)
<br>  ServerSocket1:TServerSocket;
<br>  ClientSocket1:TClientSocket;
<br>  Timer2:TTimer;
<br>  TrayIcon1:TTrayIcon;
<br>  PopupMenu1:TPopupMenu;
<br>  N11:TMenuItem;
<br>  N21:TMenuItem;
<br>  N1:TMenuItem;
<br>  N01:TMenuItem;
<br>  Memo1:TMemo;
<br>  Edit1:TEdit;
<br>  Label1:TLabel;
<br>  Timer1:TTimer;
<br>  procedureTimer2Timer(Sender:TObject);
<br>  procedureN11Click(Sender:TObject);
<br>  procedureFormCreate(Sender:TObject);
<br>  procedureFormClose(Sender:TObject;varAction:TCloseAction);
<br>  procedureN21Click(Sender:TObject);
<br>  procedureN01Click(Sender:TObject);
<br>  procedureServerSocket1ClientConnect(Sender:TObject;
<br>   Socket:TCustomWinSocket);
<br>  procedureServerSocket1ClientDisconnect(Sender:TObject;
<br>   Socket:TCustomWinSocket);
<br>  procedureServerSocket1ClientError(Sender:TObject;
<br>   Socket:TCustomWinSocket;ErrorEvent:TErrorEvent;
<br>   varErrorCode:Integer);
<br>  procedureServerSocket1ClientRead(Sender:TObject;
<br>   Socket:TCustomWinSocket);
<br>  procedureClientSocket1Connect(Sender:TObject;
<br>   Socket:TCustomWinSocket);
<br>  procedureClientSocket1Disconnect(Sender:TObject;
<br>   Socket:TCustomWinSocket);
<br>  procedureClientSocket1Error(Sender:TObject;Socket:TCustomWinSocket;
<br>   ErrorEvent:TErrorEvent;varErrorCode:Integer);
<br>  procedureClientSocket1Write(Sender:TObject;
<br>   Socket:TCustomWinSocket);
<br>  procedureClientSocket1Read(Sender:TObject;Socket:TCustomWinSocket);
<br>  procedureServerSocket1Listen(Sender:TObject;
<br>   Socket:TCustomWinSocket);
<br>  procedureAppException(Sender:TObject;E:Exception);
<br>  procedureTimer1Timer(Sender:TObject);
<br> **
<br>  {**declarations}
<br> public
<br>  Service_Enabled:boolean;     
<br>  session:arrayofsession_record;   
<br>  sessions:integer;         
<br>  LookUpTimeOut:integer;     
<br>  InvalidRequests:integer;      
<br> end;
<br>
<br>var
<br> Form1:TForm1;
<br>
<br>implementation
<br>
<br>{$R*.DFM}
<br>
<br>file://系統啟動定時器,啟動窗顯示完成后,縮小到SystemTray…
<br>procedureTForm1.Timer2Timer(Sender:TObject);
<br>begin
<br>  timer2.Enabled:=false;  
<br>  sessions:=0;       
<br>  Application.OnException:=AppException;  
<br>  invalidRequests:=0;     
<br>  LookUpTimeOut:=60000;   
<br>  timer1.Enabled:=true;    
<br>  n11.Enabled:=false;     
<br>  n21.Enabled:=true;     
<br>  serversocket1.Port:=988;   
<br>  serversocket1.Active:=true;  
<br>  form1.hide;        {隱藏界面,縮小到SystemTray上}
<br>end;
<br>
<br>file://開啟服務菜單項…
<br>procedureTForm1.N11Click(Sender:TObject);
<br>begin
<br>  serversocket1.Active:=true;  
<br>end;
<br>
<br>
<br>file://停止服務菜單項…
<br>procedureTForm1.N21Click(Sender:TObject);
<br>begin
<br>  serversocket1.Active:=false;   
<br>  N11.Enabled:=True;
<br>  N21.Enabled:=False;
<br>  Service_Enabled:=false;     
<br>end;
<br>
<br>
<br>file://主窗口建立…
<br>procedureTForm1.FormCreate(Sender:TObject);
<br>begin
<br>  Service_Enabled:=false;
<br>  timer2.Enabled:=true;    
<br>end;
<br>
<br>file://窗口關閉時…
<br>procedureTForm1.FormClose(Sender:TObject;varAction:TCloseAction);
<br>begin
<br>  timer1.Enabled:=false;     
<br>  ifService_Enabledthen
<br>   serversocket1.Active:=false; 
<br>end;
<br>
<br>file://退出程序按鈕…
<br>procedureTForm1.N01Click(Sender:TObject);
<br>begin
<br>  form1.Close;          
<br>end;
<br>
<br>file://開啟代理服務后…
<br>procedureTForm1.ServerSocket1Listen(Sender:TObject;
<br> Socket:TCustomWinSocket);
<br>begin
<br>  Service_Enabled:=true;      
<br>  N11.Enabled:=false;
<br>  N21.Enabled:=true;
<br>end;
<br>
<br>file://被代理端連接到代理服務器后,建立一個會話,并與套接字綁定…
<br>procedureTForm1.ServerSocket1ClientConnect(Sender:TObject;
<br> Socket:TCustomWinSocket);
<br>var
<br>i,j:integer;
<br>begin
<br>  j:=-1;
<br>  fori:=1tosessionsdo       
<br>   ifnotsession[i-1].Usedandnotsession[i-1].CSocket.activethen
<br>     begin
<br>      j:=i-1;           
<br>      session[j].Used:=true;   
<br>      break;
<br>     end
<br>   else
<br>     ifnotsession[i-1].Usedandsession[i-1].CSocket.activethen
<br>        session[i-1].CSocket.active:=false;
<br>  ifj=-1then
<br>   begin               
<br>     j:=sessions;
<br>     inc(sessions);
<br>     setlength(session,sessions);
<br>     session[j].Used:=true;            
<br>     session[j].CSocket:=TClientSocket.Create(nil);
<br>     session[j].CSocket.OnConnect:=ClientSocket1Connect;
<br>     session[j].CSocket.OnDisconnect:=ClientSocket1Disconnect;
<br>     session[j].CSocket.OnError:=ClientSocket1Error;
<br>     session[j].CSocket.OnRead:=ClientSocket1Read;
<br>     session[j].CSocket.OnWrite:=ClientSocket1Write;
<br>     session[j].Lookingup:=false;
<br>   end;
<br>  session[j].SS_Handle:=socket.socketHandle;  
<br>  session[j].Request:=false;          
<br>  session[j].client_connected:=true;      
<br>  session[j].remote_connected:=false;     
<br>  edit1.text:=inttostr(sessions);
<br>end;
<br>
<br>file://被代理端斷開時…
<br>procedureTForm1.ServerSocket1ClientDisconnect(Sender:TObject;
<br> Socket:TCustomWinSocket);
<br>var
<br>i,j,k:integer;
<br>begin
<br>  fori:=1tosessionsdo
<br>   if(session[i-1].SS_Handle=socket.SocketHandle)andsession[i-1].Usedthen
<br>     begin
<br>      session[i-1].client_connected:=false; 
<br>      ifsession[i-1].remote_connectedthen
<br>        session[i-1].CSocket.active:=false 
<br>      else
<br>        session[i-1].Used:=false;     
<br>      break;
<br>     end;
<br>  j:=sessions;
<br>  k:=0;
<br>  fori:=1tojdo            
<br>   begin
<br>     ifsession[j-i].Usedthen
<br>      break;
<br>     inc(k);
<br>   end;
<br>  ifk%26gt;0then             
<br>   begin
<br>     sessions:=sessions-k;
<br>     setlength(session,sessions);
<br>   end;
<br>  edit1.text:=inttostr(sessions);
<br>end;
<br>
<br>file://通信錯誤出現時…
<br>procedureTForm1.ServerSocket1ClientError(Sender:TObject;
<br> Socket:TCustomWinSocket;ErrorEvent:TErrorEvent;
<br> varErrorCode:Integer);
<br>var
<br>i,j,k:integer;
<br>begin
<br>  fori:=1tosessionsdo
<br>   if(session[i-1].SS_Handle=socket.SocketHandle)andsession[i-1].Usedthen
<br>     begin
<br>      session[i-1].client_connected:=false; 
<br>      ifsession[i-1].remote_connectedthen
<br>        session[i-1].CSocket.active:=false 
<br>      else
<br>        session[i-1].Used:=false;     
<br>      break;
<br>     end;
<br>  j:=sessions;
<br>  k:=0;
<br>  fori:=1tojdo
<br>   begin
<br>     ifsession[j-i].Usedthen
<br>      break;
<br>     inc(k);
<br>   end;
<br>  ifk%26gt;0then
<br>   begin
<br>     sessions:=sessions-k;
<br>     setlength(session,sessions);
<br>   end;
<br>  edit1.text:=inttostr(sessions);
<br>  errorcode:=0;
<br>end;
<br>
<br>file://被代理端發送來頁面請求時…
<br>procedureTForm1.ServerSocket1ClientRead(Sender:TObject;
<br> Socket:TCustomWinSocket);
<br>var
<br>tmp,line,host:string;
<br>i,j,port:integer;
<br>begin
<br>  fori:=1tosessionsdo        
<br>   ifsession[i-1].Usedand(session[i-1].SS_Handle=socket.sockethandle)then
<br>     begin
<br>       session[i-1].request_str:=socket.ReceiveText; 
<br>       tmp:=session[i-1].request_str;        
<br>       memo1.lines.add(tmp);
<br>       j:=pos(char(13)+char(10),tmp);        
<br>       whilej%26gt;0do           
<br>        begin
<br>          line:=copy(tmp,1,j-1);         
<br>          delete(tmp,1,j+1);           
<br>          j:=pos('Host',line);          
<br>          ifj%26gt;0then
<br>           begin
<br>             delete(line,1,j+5);       
<br>             j:=pos(':',line);
<br>             ifj%26gt;0then
<br>              begin
<br>                host:=copy(line,1,j-1);
<br>                delete(line,1,j);
<br>                try
<br>                 port:=strtoint(line);
<br>                except
<br>                 port:=80;
<br>                end;
<br>              end
<br>             else
<br>              begin
<br>                host:=trim(line);        
<br>                port:=80;
<br>              end;
<br>             ifnotsession[i-1].remote_connectedthen 
<br>              begin
<br>                session[i-1].Request:=true;   
共1 條記錄, 每頁顯示 10 條, 頁簽: [1]

Copyright © 2000 - 2009 曙海教育集團
Powered By 曙海教育集團 Version 2.2
Processed in .03125 s, 2 queries.
主站蜘蛛池模板: 一级女性全黄生活片免费 | 亚洲三级黄 | 久久一本精品久久精品66 | 精品国产日韩亚洲一区在线 | 亚洲日韩精品欧美一区二区一 | 99热久久国产精品免费观看 | 中文字幕乱码中文乱码综合 | 国产小网站 | 久久成年人 | 亚洲欧洲一二三区机械有限公司 | 中文成人在线 | 国产精品久久久久国产精品三级 | 男人的亚洲天堂 | 国产成人aa在线观看视频 | xxxwww欧美性 | 99视频只有精品 | 日本一级毛片高清免费观看视频 | 欧美成人免费观看的 | 亚洲伊人久久综合影院2021 | 欧美最新的精品videoss | 免费观看毛片视频 | 天堂一区二区三区在线观看 | 91男女视频 | 欧美视频一区二区 | 九九热视频精品在线观看 | 欧美色视频在线观看 | 亚洲国产tv | 久久99国产精品久久99无号码 | 亚洲欧美日本人成在线观看 | 久草视频福利 | 一级特一级特色生活片 | 人人公开免费超级碰碰碰视频 | 国产成人高清亚洲一区91 | 手机看片神马午夜 | 免费观看一级特黄欧美大片 | 欧美性高清视频免费看www | 视频一区中文字幕 | 亚洲男人天 | 亚洲欧美国产日韩天堂在线视 | 国产成人综合在线视频 | 国产90后美女露脸在线观看 |