原来做用socket直接用ip地址连接异地的的程序,加了个路由器,就不行了?(300分)

  • 主题发起人 主题发起人 fly555
  • 开始时间 开始时间
F

fly555

Unregistered / Unconfirmed
GUEST, unregistred user!
原来做用socket直接用ip地址连接异地的的程序,一直没问题。 &nbsp; <br>&nbsp; 现在客户某地加了过路由器,它可以连接别地地方,但其它地方 &nbsp; <br>&nbsp; 却联不上它。怎么解决。
 
肯定三,如果连上就怪了,需要P2P
 
p2p怎么搞呀?说祥细点呀。
 
让客户在路由上设置端口映射,把socket要访问的端口映射到客户机上
 
如果你的地址是外网,用反弹连接就行了
 
一般来说需要在路由器上做端口映射的
 
nat才要做端口映射,路由是双向的
 
加了路由器,就成p2p了,老兄。
 
我知道端口映射。<br>比如我的路由器上了网。IP地址为:62.45.63.43<br>一个路由器里有许多台电脑。在端口映射中,是这样的:<br>局域网地址及端口 &nbsp; &nbsp;广域网端口 <br>192.168.1.2:80 &nbsp; &nbsp; &nbsp; &nbsp; 5800<br>是不是对62.45.63.43:5800的访问就是对192.168.1.2的机器的访问了。<br>那么在tsockconnection 中的属性host中的IP地址中写入什么呢?<br>写入62.45.63.43不行。写入62.45.63.43:5800的话,还是连接不上呀。
 
端口映射的问题.你看看是不是映射的不对?<br>路由型号!
 
联想天工无线路由器。<br>我估计映射是成功了。但是我输入静态的IP地址就成功了。<br>但是我输入动态的IP地址时,提示:<br>windows socket error:由于目标机器积极拒绝,无法连接。(10061), on api 'connect'<br>我如果在动态的IP地址后面加个:5880,就提示windows socket error:在其上下文中,<br>请求的地址无效。(10049),on api 'connect'
 
看一下路由器是不是支持UPnP,若支持,需要在服务端写点程序实现自动NAT,这样才能实现Socket的双向访问。手工NAT也行,就是麻烦些。
 
另外,如果路由器支持DMZ,而且安全方面允许,可以把服务器置于DMZ区域
 
是支持DMZ的。我也把这个IP地址设置好了。<br>那么在tsockconnection 中的属性host中的IP地址中写入什么呢?<br>我写入路由器动态生成的IP地址时,还是提示失败。<br>我想,应该还要在此IP地址的后面加上端口号。才表示路由器下面的某一台机器。<br>但是这个端口号写在IP地址后面加上冒号端口号的话,是不认这个格式的。<br>另外:需要在服务端写点程序实现自动NAT,写点什么程序呢。<br>wpy020327:你有QQ吗?
 
nat的问题了.双方没有建立互信的连接,你从外部访问nat后面的机器,nat认为这是不请自来的,肯定丢掉了.<br>如果没有独立的IP地址,就要使用p2p穿越nat来做了.
 
用p2p穿越nat来做了?<br>能说得祥细一些吗?
 
下面是我写的一个自动NAT的delphi控件,楼主自己琢磨吧:<br>unit AutoNAT;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Scktcomp, NMUDP, extctrls;<br><br>//<br>// 定义事件类...<br>type<br>&nbsp; &nbsp;TTaskSuccessEvent = procedure (Sender: TObject) of object; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{任务执行成功事件}<br>&nbsp; &nbsp;TTaskFailEvent = procedure (Sender: TObject) of object; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {任务执行失败事件}<br><br>type<br>&nbsp; TAutoNAT = class(TComponent)<br>&nbsp; private<br>//<br>// 只读属性...<br>&nbsp; &nbsp; fTimeout: integer; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {任务超时值}<br>&nbsp; &nbsp; fTaskType: integer; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{任务列别: 1-搜索 2-取控制页地址 3-增加端口 4-取外网地址 5-删除端口}<br>&nbsp; &nbsp; fLocalIp: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{本机内网IP}<br>&nbsp; &nbsp; fRouterIp: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {路由器内网IP}<br>&nbsp; &nbsp; fRouterPort: integer; &nbsp; &nbsp; &nbsp; &nbsp;{路由器控制端口}<br>&nbsp; &nbsp; fRouterLocation: string; &nbsp; &nbsp; {路由器设备位置URL}<br>&nbsp; &nbsp; fRouterName: string; &nbsp; &nbsp; &nbsp; &nbsp; {路由器设备名称}<br>&nbsp; &nbsp; fRouterUSN: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{路由器设备标识名}<br>&nbsp; &nbsp; fRouterURL: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{路由器URL}<br>&nbsp; &nbsp; fExternalIp: string; &nbsp; &nbsp; &nbsp; &nbsp; {路由器外网IP}<br>&nbsp; &nbsp; fControlURL: string; &nbsp; &nbsp; &nbsp; &nbsp; {控制页URL}<br>&nbsp; &nbsp; fURLbase: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{控制页基地址}<br>//<br>// 事件...<br>&nbsp; &nbsp; FOnTaskSuccess: TNotifyEvent; &nbsp; &nbsp;{任务执行成功的事件}<br>&nbsp; &nbsp; FOnTaskFail: TNotifyEvent; &nbsp; &nbsp; &nbsp; {任务执行失败的事件}<br>//<br>// 普通变量...<br>&nbsp; &nbsp; request: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{socket请求数据包}<br>&nbsp; &nbsp; requested: boolean; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {请求是否已发送}<br>&nbsp; &nbsp; response: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {socket应答数据包}<br>&nbsp; &nbsp; //<br>&nbsp; &nbsp; UDP: TNMUDP; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {UDP对象}<br>&nbsp; &nbsp; Sock: TClientSocket; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {Socket对象}<br>&nbsp; &nbsp; TaskTimer: TTimer; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {任务定时器}<br>&nbsp; &nbsp; //<br>&nbsp; &nbsp; TaskExecuting: boolean; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{当前是否有任务在执行}<br>&nbsp; &nbsp; Taskfinished: boolean; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {任务是否已经完成}<br>&nbsp; &nbsp; Tasksuccess: boolean; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{任务是否成功}<br>//<br>// 其它私有过程...<br>&nbsp; &nbsp; procedure SetTimeout(Timeout: integer);<br>&nbsp; &nbsp; procedure SuccessEvent;<br>&nbsp; &nbsp; procedure FailEvent;<br>&nbsp; &nbsp; procedure TaskTimerTimer(Sender: TObject); &nbsp; &nbsp; &nbsp; &nbsp;{超时事件}<br>&nbsp; &nbsp; procedure UDPInvalidHost(var handled: Boolean); &nbsp; {主机无效时的事件}<br>&nbsp; &nbsp; procedure UDPBufferInvalid(var handled: Boolean; var Buff: array of Char; var length: Integer);<br>&nbsp; &nbsp; procedure UDPStreamInvalid(var handled: Boolean; Stream: TStream);<br>&nbsp; &nbsp; procedure UDPDataReceived(Sender: TComponent; NumberBytes: Integer; FromIP: String; Port: Integer);<br>&nbsp; &nbsp; procedure ClientSocketWrite(Sender: TObject; Socket: TCustomWinSocket);<br>&nbsp; &nbsp; procedure ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);<br>&nbsp; &nbsp; function ResponseFinished(ResponseData: string): boolean;<br>&nbsp; &nbsp; procedure ClientSocketError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);<br>&nbsp; &nbsp; procedure ClientSocketDisconnect(Sender: TObject; Socket: TCustomWinSocket);<br>//<br>&nbsp; protected<br>&nbsp; &nbsp; { Protected declarations }<br><br>&nbsp; public<br>&nbsp; &nbsp; //<br>&nbsp; &nbsp; // 创建对象...<br>&nbsp; &nbsp; Constructor Create(AOwner: TComponent); override; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {对象创建时的方法}<br>&nbsp; &nbsp; //<br>&nbsp; &nbsp; // 撤消对象...<br>&nbsp; &nbsp; Destructor Destroy(); override; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {对象撤消的方法}<br>&nbsp; &nbsp; //<br>&nbsp; &nbsp; // 搜索路由器设备(任务1)...<br>&nbsp; &nbsp; function SearchRouter(): boolean;<br>&nbsp; &nbsp; //<br>&nbsp; &nbsp; // 得到路由器控制页{任务2}...<br>&nbsp; &nbsp; function GetControlURL(): boolean;<br>&nbsp; &nbsp; //<br>&nbsp; &nbsp; // 增加端口映射项(任务3)...<br>&nbsp; &nbsp; function AddNatMapping(NatPortName: string; ExternalPort: integer; LocalIp: string; LocalPort: integer; Protocol: string): boolean;<br>&nbsp; &nbsp; //<br>&nbsp; &nbsp; // 取外网IP地址(任务4)...<br>&nbsp; &nbsp; function GetExternalIp: boolean;<br>&nbsp; &nbsp; //<br>&nbsp; &nbsp; // 删除端口映射项(任务5)...<br>&nbsp; &nbsp; function DeleteNatMapping(ExternalPort: integer; Protocol: string): boolean;<br>&nbsp; &nbsp; //<br>&nbsp; published<br>&nbsp; &nbsp; //<br>&nbsp; &nbsp; // 控件属性...<br>&nbsp; &nbsp; property Timeout: integer read fTimeout write settimeout; {任务超时值}<br>&nbsp; &nbsp; property TaskType: integer read fTaskType; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{任务列别: 1-搜索 2-取控制页地址 3-增加端口 4-取外网地址 5-删除端口}<br>&nbsp; &nbsp; property LocalIp: string read fLocalIp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {本机内网IP}<br>&nbsp; &nbsp; property RouterIp: string read fRouterIp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {路由器内网IP}<br>&nbsp; &nbsp; property RouterPort: integer read fRouterPort; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{路由器控制端口}<br>&nbsp; &nbsp; property RouterLocation: string read fRouterLocation; &nbsp; &nbsp; {路由器设备位置URL}<br>&nbsp; &nbsp; property RouterName: string read fRouterName; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {路由器设备名称}<br>&nbsp; &nbsp; property RouterUSN: string read fRouterUSN; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {路由器设备标识名}<br>&nbsp; &nbsp; property RouterURL: string read fRouterURL; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {路由器URL}<br>&nbsp; &nbsp; property ExternalIp: string read fExternalIp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {路由器外网IP}<br>&nbsp; &nbsp; property ControlURL: string read fControlURL; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {控制页URL}<br>&nbsp; &nbsp; property URLBase: string read fURLBase; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {基地址}<br>&nbsp; &nbsp; //<br>&nbsp; &nbsp; // 事件...<br>&nbsp; &nbsp; property OnTaskSuccess: TNotifyEvent read FOnTaskSuccess write FOnTaskSuccess; &nbsp;{任务执行成功的事件}<br>&nbsp; &nbsp; property OnTaskFail: TNotifyEvent read FOnTaskFail write FOnTaskFail; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {任务执行失败的事件}<br>&nbsp; &nbsp; //<br>&nbsp; end;<br><br>procedure Register;<br><br>implementation<br><br>//<br>// 控件注册的过程...<br>procedure Register;<br>begin<br>&nbsp; RegisterComponents('AutoNAT', [TAutoNAT]);<br>end;<br><br>//<br>// 创建对象的过程...<br>constructor TAutoNAT.Create(AOwner: TComponent);<br>begin<br>//<br>// 调用TComponent的方法,创建...<br>&nbsp; &nbsp;inherited;<br>//<br>// 创建内部变量...<br>&nbsp; &nbsp;UDP:=TNMUDP.Create(self); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {UDP对象}<br>&nbsp; &nbsp;udp.RemoteHost:='239.255.255.250'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{广播地址}<br>&nbsp; &nbsp;udp.RemotePort:=1900; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {广播端口} &nbsp;<br>&nbsp; &nbsp;udp.LocalPort:=1900;<br>&nbsp; &nbsp;UDP.OnInvalidHost:=UDPInvalidHost; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{主机无效事件}<br>&nbsp; &nbsp;UDP.OnBufferInvalid:=UDPBufferInvalid; &nbsp; &nbsp; &nbsp;{缓冲区无效事件}<br>&nbsp; &nbsp;UDP.OnStreamInvalid:=UDPStreamInvalid; &nbsp; &nbsp; &nbsp;{流无效事件}<br>&nbsp; &nbsp;UDP.OnDataReceived:=UDPDataReceived; &nbsp; &nbsp; &nbsp; &nbsp;{收到应答数据时的事件}<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;sock:=TClientSocket.create(self); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {ClientSocket对象}<br>&nbsp; &nbsp;sock.ClientType:=ctNonBlocking; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {非阻塞型}<br>&nbsp; &nbsp;sock.OnWrite:=clientsocketwrite; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{提交请求事件}<br>&nbsp; &nbsp;sock.OnRead:=clientsocketread; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{应答响应事件}<br>&nbsp; &nbsp;sock.OnError:=clientsocketerror; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{异常事件}<br>&nbsp; &nbsp;sock.OnDisconnect:=clientsocketdisconnect; &nbsp;{断开事件}<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;TaskTimer:=TTimer.Create(self); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {超时定时器}<br>&nbsp; &nbsp;TaskTimer.Interval:=1000*5; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {默认5秒超时}<br>&nbsp; &nbsp;TaskTimer.enabled:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {不激活定时器}<br>&nbsp; &nbsp;TaskTimer.OnTimer:=TaskTimerTimer; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{指定超时处理事件}<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;taskexecuting:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {没有任务在执行}<br>&nbsp; &nbsp;taskfinished:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{未完成}<br>&nbsp; &nbsp;tasksuccess:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {未成功}<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;fexternalip:='0.0.0.0'; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {外网IP暂时未知}<br>&nbsp; &nbsp;ftimeout:=5000; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {默认超时值=5秒}<br>end;<br><br>//<br>// 撤消对象的过程...<br>destructor TAutoNAT.Destroy;<br>begin<br>//<br>// 释放对象...<br>&nbsp; UDP.free;<br>&nbsp; tasktimer.free;<br>&nbsp; sock.free;<br>//<br>// 调用TComponent的方法,撤消...<br>&nbsp; inherited;<br>end;<br><br>//<br>// 设置任务超时值的过程...<br>procedure TAutoNAT.SetTimeout(Timeout: integer);<br>begin<br>&nbsp; &nbsp;if (timeout&lt;1000) or (timeout&gt;30*1000) then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;messagebox(0,'对不起,超时值范围1000-30000,重新输入!','提示',mb_ok+mb_iconinformation);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp;ftimeout:=timeout;<br>end;<br><br>//<br>// 激发一个成功事件的过程...<br>procedure TAutoNAT.SuccessEvent;<br>begin<br>&nbsp; &nbsp;tasktimer.enabled:=false;<br>//<br>// 设置任务已经完成...<br>&nbsp; &nbsp;taskexecuting:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {任务不在执行了}<br>&nbsp; &nbsp;taskfinished:=true; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {任务已经完成}<br>&nbsp; &nbsp;tasksuccess:=true; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{任务成功}<br>//<br>// 激发一个成功事件...<br>&nbsp; &nbsp;if assigned(fontasksuccess) then<br>&nbsp; &nbsp; &nbsp; fontasksuccess(self);<br>end;<br><br>//<br>// 激发一个失败事件的过程...<br>procedure TAutoNAT.FailEvent;<br>begin<br>&nbsp; &nbsp;tasktimer.enabled:=false;<br>//<br>// 设置任务已经完成...<br>&nbsp; &nbsp;taskexecuting:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {任务不在执行了}<br>&nbsp; &nbsp;taskfinished:=true; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {任务已经完成}<br>&nbsp; &nbsp;tasksuccess:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {任务失败}<br>//<br>// 激发一个失败事件...<br>&nbsp; &nbsp;if assigned(fontaskfail) then<br>&nbsp; &nbsp; &nbsp; fontaskfail(self);<br>end;<br><br>//<br>// UDP的主机无效事件...<br>procedure TAutoNAT.UDPInvalidHost(var handled: Boolean);<br>begin<br>&nbsp; &nbsp;failevent; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{激发失败事件}<br>&nbsp; &nbsp;handled:=true;<br>end;<br><br>//<br>// UDP的缓冲区无效事件...<br>procedure TAutoNAT.UDPBufferInvalid(var handled: Boolean; var Buff: array of Char; var length: Integer);<br>begin<br>&nbsp; &nbsp;failevent; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{激发失败事件}<br>&nbsp; &nbsp;handled:=true;<br>end;<br><br>//<br>// UDP的流无效事件...<br>procedure TAutoNAT.UDPStreamInvalid(var handled: Boolean; Stream: TStream);<br>begin<br>&nbsp; &nbsp;failevent; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{激发失败事件}<br>&nbsp; &nbsp;handled:=true;<br>end;<br><br>//<br>// 数据到达事件(搜索设备的应答)...<br>procedure TAutoNAT.UDPDataReceived(Sender: TComponent; NumberBytes: Integer; FromIP: String; Port: Integer);<br>var<br>&nbsp; &nbsp;tmpstr: string;<br>&nbsp; &nbsp;buffer: array [0..4096] of char;<br>&nbsp; &nbsp;j: integer;<br>begin<br>//<br>// 无效数据,丢弃...<br>&nbsp; &nbsp;if (numberbytes&lt;=0) or (numberbytes&gt;4096) then<br>&nbsp; &nbsp; &nbsp; exit;<br>//<br>// 读取数据...<br>&nbsp; &nbsp;udp.ReadBuffer(buffer,numberbytes);<br>&nbsp; &nbsp;setlength(tmpstr,numberbytes);<br>&nbsp; &nbsp;strlcopy(pchar(tmpstr),buffer,numberbytes);<br>//<br>// 如果不是有效数据,丢弃...<br>&nbsp; &nbsp;if uppercase(copy(tmpstr,1,5))&lt;&gt;'HTTP/' then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 激发一个任务失败事件...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ftasktype=1 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; failevent; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{激发失败事件}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; end;<br>//<br>// 得到Location...<br>&nbsp; &nbsp;fRouterLocation:=tmpstr;<br>&nbsp; &nbsp;j:=pos('LOCATION:',uppercase(fRouterLocation));<br>&nbsp; &nbsp;if j&lt;0 then<br>&nbsp; &nbsp; &nbsp; fRouterLocation:=''<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;delete(fRouterLocation,1,j+8);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j:=pos(#13#10,fRouterLocation);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fRouterLocation:=trim(copy(fRouterLocation,1,j-1));<br>&nbsp; &nbsp; &nbsp; end;<br>//<br>// 得到Server...<br>&nbsp; &nbsp;fRouterName:=tmpstr;<br>&nbsp; &nbsp;j:=pos('SERVER:',uppercase(fRouterName));<br>&nbsp; &nbsp;if j&lt;0 then<br>&nbsp; &nbsp; &nbsp; fRouterName:=''<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;delete(fRouterName,1,j+6);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j:=pos(#13#10,fRouterName);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fRouterName:=trim(copy(fRouterName,1,j-1));<br>&nbsp; &nbsp; &nbsp; end;<br>//<br>// 得到USN...<br>&nbsp; &nbsp;fRouterUSN:=tmpstr;<br>&nbsp; &nbsp;j:=pos('USN:',uppercase(fRouterUSN));<br>&nbsp; &nbsp;if j&lt;0 then<br>&nbsp; &nbsp; &nbsp; fRouterUSN:=''<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;delete(fRouterUSN,1,j+3);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j:=pos(#13#10,fRouterUSN);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fRouterUSN:=trim(copy(fRouterUSN,1,j-1));<br>&nbsp; &nbsp; &nbsp; end;<br>//<br>// 得到路由器IP地址...<br>&nbsp; &nbsp;tmpstr:=fRouterLocation;<br>&nbsp; &nbsp;if copy(uppercase(tmpstr),1,7)='HTTP://' then<br>&nbsp; &nbsp; &nbsp; delete(tmpstr,1,7);<br>&nbsp; &nbsp;j:=pos(':',tmpstr);<br>&nbsp; &nbsp;if j&lt;=0 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 激发一个任务失败事件...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ftasktype=1 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; failevent; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{激发失败事件}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp;fRouterIp:=copy(tmpstr,1,j-1);<br>&nbsp; &nbsp;delete(tmpstr,1,j);<br>//<br>// 得到路由器端口、控制页URL地址...<br>&nbsp; &nbsp;j:=pos('/',tmpstr);<br>&nbsp; &nbsp;if j&gt;1 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fRouterPort:=strtoint(copy(tmpstr,1,j-1));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fRouterPort:=-1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;delete(tmpstr,1,j-1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fRouterURL:=tmpstr;<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j:=pos(#13#10,tmpstr);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if j&lt;=1 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 激发一个任务失败事件...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ftasktype=1 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; failevent; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{激发失败事件}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fRouterPort:=strtoint(copy(tmpstr,1,j-1));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fRouterPort:=-1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fRouterURL:='/';<br>&nbsp; &nbsp; &nbsp; end;<br>//<br>// 得到默认的URLBase...<br>&nbsp; &nbsp;tmpstr:='http://'+frouterip+':'+inttostr(frouterport)+frouterurl;<br>&nbsp; &nbsp;furlbase:='';<br>&nbsp; &nbsp;j:=pos('/',tmpstr);<br>&nbsp; &nbsp;while j&gt;0 do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;furlbase:=furlbase+copy(tmpstr,1,j);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;delete(tmpstr,1,j);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;j:=pos('/',tmpstr);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp;delete(furlbase,length(furlbase),1);<br>//<br>// 若数据无效...<br>&nbsp; &nbsp;if (trim(fRouterIp)='') or (fRouterPort&lt;=0) then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 激发一个任务失败事件...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ftasktype=1 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; failevent; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{激发失败事件}<br>&nbsp; &nbsp; &nbsp; end<br>//<br>// 若成功...<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 激发一个任务成功事件...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ftasktype=1 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; successevent; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {激发成功事件}<br>&nbsp; &nbsp; &nbsp; end;<br>end;<br><br>//<br>// 超时时间到的事件...<br>procedure TAutoNAT.TaskTimerTimer(Sender: TObject);<br>begin<br>&nbsp; &nbsp;failevent;<br>&nbsp; &nbsp;ftasktype:=-1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{撤消任务号}<br>end;<br><br>//<br>// 判定Socket的应答数据是否全部得到的函数...<br>function TAutoNAT.ResponseFinished(ResponseData: string): boolean;<br>var<br>&nbsp; &nbsp;head: string;<br>&nbsp; &nbsp;contentlength,j,headlength: integer;<br>begin<br>&nbsp; &nbsp;result:=false;<br>//<br>// 得到头信息...<br>&nbsp; &nbsp;j:=pos(#13#10#13#10,responsedata);<br>&nbsp; &nbsp;if j&lt;=0 then<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp;head:=copy(responsedata,1,j-1);<br>&nbsp; &nbsp;headlength:=j+3;<br>//<br>// 得到内容长度...<br>&nbsp; &nbsp;j:=pos('CONTENT-LENGTH:',uppercase(head));<br>&nbsp; &nbsp;if j&lt;=0 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp;delete(head,1,j+14);<br>&nbsp; &nbsp;j:=pos(#13#10,head);<br>&nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; contentlength:=strtoint(copy(head,1,j-1));<br>&nbsp; &nbsp;except<br>&nbsp; &nbsp; &nbsp; contentlength:=9999999;<br>&nbsp; &nbsp;end;<br>//<br>// 判定是否结束...<br>&nbsp; &nbsp;if (length(responsedata)-headlength)&gt;=contentlength then<br>&nbsp; &nbsp; &nbsp; result:=true;<br>end;<br><br>//<br>// 提交查询请求...<br>procedure TAutoNAT.ClientSocketWrite(Sender: TObject; Socket: TCustomWinSocket);<br>begin<br>//<br>// 若已经发送请求,不再发送...<br>&nbsp; &nbsp;if requested then<br>&nbsp; &nbsp; &nbsp; exit;<br>//<br>// 发送前,先停止定时器...<br>&nbsp; &nbsp;tasktimer.enabled:=false;<br>//<br>// 发出请求...<br>&nbsp; &nbsp;response:=''; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{还没有应答数据}<br>&nbsp; &nbsp;socket.SendBuf(request[1],length(request));<br>&nbsp; &nbsp;requested:=true; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {已发送}<br>end;<br><br>//<br>// 得到Socket应答数据时(取控制页信息、增加NAT、删除NAT,以及取外网IP时的应答)...<br>procedure TAutoNAT.ClientSocketRead(Sender: TObject; Socket: TCustomWinSocket);<br>var<br>&nbsp; &nbsp;tmpstr: string;<br>&nbsp; &nbsp;j: integer;<br>begin<br>//<br>// 接收数据,放到Response变量中...<br>&nbsp; &nbsp;j:=socket.ReceiveLength;<br>&nbsp; &nbsp;setlength(tmpstr,j);<br>&nbsp; &nbsp;socket.ReceiveBuf(tmpstr[1],j);<br>&nbsp; &nbsp;response:=response+tmpstr;<br>//<br>// 若应答数据未全部得到,等待下个读事件...<br>&nbsp; &nbsp;if not responsefinished(response) then<br>&nbsp; &nbsp; &nbsp; exit;<br>//<br>// 若已经全部得到,进行结果解析...<br>&nbsp; &nbsp;case ftasktype of<br>&nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; // 取控制页地址时...<br>&nbsp; &nbsp; &nbsp; 2: begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpstr:=response;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 得到基地址...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j:=pos(uppercase('&lt;URLBASE&gt;'),uppercase(tmpstr));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if j&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delete(tmpstr,1,j+length('&lt;URLBASE&gt;')-1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j:=pos(uppercase('&lt;/URLBASE&gt;'),uppercase(tmpstr));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if j&gt;1 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;furlbase:=copy(tmpstr,1,j-1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 查找设备urn:schemas-upnp-org:device:InternetGatewayDevice:1的描述段...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j:=pos(uppercase('&lt;deviceType&gt;urn:schemas-upnp-org:device:InternetGatewayDevice:1&lt;/deviceType&gt;'),uppercase(tmpstr));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if j&lt;=0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; failevent;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket.Close; &nbsp; &nbsp; &nbsp;{关闭套接字}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delete(tmpstr,1,j+length('&lt;deviceType&gt;urn:schemas-upnp-org:device:InternetGatewayDevice:1&lt;/deviceType&gt;')-1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 再查找urn:schemas-upnp-org:device:WANDevice:1的描述段...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j:=pos(uppercase('&lt;deviceType&gt;urn:schemas-upnp-org:device:WANDevice:1&lt;/deviceType&gt;'),uppercase(tmpstr));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if j&lt;=0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; failevent;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket.Close; &nbsp; &nbsp; &nbsp;{关闭套接字}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delete(tmpstr,1,j+length('&lt;deviceType&gt;urn:schemas-upnp-org:device:WANDevice:1&lt;/deviceType&gt;')-1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 再查找urn:schemas-upnp-org:device:WANConnectionDevice:1的描述段...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j:=pos(uppercase('&lt;deviceType&gt;urn:schemas-upnp-org:device:WANConnectionDevice:1&lt;/deviceType&gt;'),uppercase(tmpstr));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if j&lt;=0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; failevent;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket.Close; &nbsp; &nbsp; &nbsp;{关闭套接字}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delete(tmpstr,1,j+length('&lt;deviceType&gt;urn:schemas-upnp-org:device:WANConnectionDevice:1&lt;/deviceType&gt;')-1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 最后找到服务urn:schemas-upnp-org:service:WANIPConnection:1的描述段...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j:=pos(uppercase('&lt;serviceType&gt;urn:schemas-upnp-org:service:WANIPConnection:1&lt;/serviceType&gt;'),uppercase(tmpstr));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if j&lt;=0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; failevent;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket.Close; &nbsp; &nbsp; &nbsp;{关闭套接字}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delete(tmpstr,1,j+length('&lt;serviceType&gt;urn:schemas-upnp-org:service:WANIPConnection:1&lt;/serviceType&gt;')-1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 得到ControlURL...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j:=pos(uppercase('&lt;controlURL&gt;'),uppercase(tmpstr));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if j&lt;=0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; failevent;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket.Close; &nbsp; &nbsp; &nbsp;{关闭套接字}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delete(tmpstr,1,j+length('&lt;controlURL&gt;')-1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j:=pos(uppercase('&lt;/controlURL&gt;'),uppercase(tmpstr));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if j&lt;=0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; failevent;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; socket.Close; &nbsp; &nbsp; &nbsp;{关闭套接字}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fcontrolurl:=copy(tmpstr,1,j-1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (copy(urlbase,length(urlbase),1)='/') and (copy(fcontrolurl,1,1)='/') then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;delete(fcontrolurl,1,1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (copy(urlbase,length(urlbase),1)&lt;&gt;'/') and (copy(fcontrolurl,1,1)&lt;&gt;'/') then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fcontrolurl:='/'+fcontrolurl;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fcontrolurl:=urlbase+fcontrolurl;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 激发成功事件...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; successevent;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; // 增加NAT项时...<br>&nbsp; &nbsp; &nbsp; 3: begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpstr:=response;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j:=pos(#13#10,tmpstr);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpstr:=uppercase(copy(tmpstr,1,j-1));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 假如成功...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if pos('200 OK',tmpstr)&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;successevent<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 假如失败...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;failevent;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; // 取外网地址时...<br>&nbsp; &nbsp; &nbsp; 4: begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpstr:=response;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j:=pos(#13#10,tmpstr);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpstr:=uppercase(copy(tmpstr,1,j-1));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 假如成功...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if pos('200 OK',tmpstr)&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 得到外网IP...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j:=pos(uppercase('&lt;NewExternalIPAddress&gt;'),uppercase(response));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if j&lt;=0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fexternalip:='0.0.0.0'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fexternalip:=response;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delete(fexternalip,1,j+length('&lt;NewExternalIPAddress&gt;')-1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j:=pos(uppercase('&lt;/NewExternalIPAddress&gt;'),uppercase(fexternalip));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if j&lt;=0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fexternalip:='0.0.0.0'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fexternalip:=copy(fexternalip,1,j-1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 激发成功事件...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; successevent;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 假如失败...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;failevent;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; // 删除NAT项时...<br>&nbsp; &nbsp; &nbsp; 5: begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpstr:=response;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j:=pos(#13#10,tmpstr);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tmpstr:=uppercase(copy(tmpstr,1,j-1));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 假如成功...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if pos('200 OK',tmpstr)&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;successevent<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 假如失败...<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;failevent;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp;end;<br>//<br>// 断开...<br>&nbsp; &nbsp;socket.Close; &nbsp; &nbsp; &nbsp;{关闭套接字}<br>end;<br><br>//<br>// 屏蔽可能出现的错误...<br>procedure TAutoNAT.ClientSocketError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);<br>begin<br>&nbsp; &nbsp;if taskexecuting then<br>&nbsp; &nbsp; &nbsp; failevent;<br>&nbsp; &nbsp;errorcode:=0;<br>end;<br><br>//<br>// 断开时...<br>procedure TAutoNAT.ClientSocketDisconnect(Sender: TObject; Socket: TCustomWinSocket);<br>begin<br>&nbsp; &nbsp;if taskexecuting then<br>&nbsp; &nbsp; &nbsp; failevent;<br>end;<br><br>//<br>// 搜索路由器设备(任务1)...<br>function TAutoNAT.SearchRouter(): boolean;<br>var<br>&nbsp; &nbsp;tmpstr: string;<br>&nbsp; &nbsp;buffer: array [0..4096] of char;<br>&nbsp; &nbsp;j: integer;<br>begin<br>//<br>// 假如定时器处于开启状态,异常返回...<br>&nbsp; &nbsp;if tasktimer.enabled then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result:=false;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; end;<br>//<br>// 生成路由器搜索请求...<br>&nbsp; &nbsp;tmpstr:='M-SEARCH * HTTP/1.1'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +'HOST: 239.255.255.250:1900'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +'MAN: "ssdp:discover"'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +'MX: 3'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; +'ST: upnp:rootdevice'#13#10#13#10;<br>&nbsp; &nbsp;j:=length(tmpstr);<br>&nbsp; &nbsp;strplcopy(buffer,tmpstr,j);<br>//<br>// 设置各标志...<br>&nbsp; &nbsp;ftasktype:=1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {设备搜索任务}<br>&nbsp; &nbsp;taskexecuting:=true; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{任务在执行}<br>&nbsp; &nbsp;taskfinished:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{任务未完成}<br>&nbsp; &nbsp;tasksuccess:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {任务未成功}<br>&nbsp; &nbsp;requested:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {未提交请求}<br>//<br>// 启动延时定时器...<br>&nbsp; &nbsp;tasktimer.interval:=ftimeout; &nbsp; {超时值}<br>&nbsp; &nbsp;tasktimer.enabled:=true;<br>//<br>// 发送请求,搜索路由器...<br>&nbsp; &nbsp;udp.SendBuffer(buffer,j); &nbsp; &nbsp; &nbsp; {发送设备搜索数据包}<br>//<br>// 返回成功...<br>&nbsp; &nbsp;result:=true;<br>end;<br><br>//<br>// 得到路由器控制页{任务2}...<br>function TAutoNAT.GetControlURL(): boolean;<br>begin<br>//<br>// 假如有任务在执行,返回失败...<br>&nbsp; &nbsp;if tasktimer.enabled then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result:=false;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; end;<br>//<br>// 生成请求串...<br>&nbsp; &nbsp;request:='GET '+frouterurl+' HTTP/1.1'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'Host: '+frouterip+':'+inttostr(frouterport)+#13#10#13#10;<br>//<br>// 设置各标志...<br>&nbsp; &nbsp;ftasktype:=2; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {设备搜索任务}<br>&nbsp; &nbsp;taskexecuting:=true; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{任务在执行}<br>&nbsp; &nbsp;taskfinished:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{任务未完成}<br>&nbsp; &nbsp;tasksuccess:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {任务未成功}<br>&nbsp; &nbsp;requested:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {未提交请求}<br>//<br>// 启动延时定时器...<br>&nbsp; &nbsp;tasktimer.interval:=ftimeout; &nbsp; {超时值}<br>&nbsp; &nbsp;tasktimer.enabled:=true;<br>//<br>// 连接路由器...<br>&nbsp; &nbsp;sock.host:=frouterip; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {路由器IP地址}<br>&nbsp; &nbsp;sock.port:=frouterport; &nbsp; &nbsp; &nbsp; &nbsp; {路由器端口号}<br>&nbsp; &nbsp;sock.active:=true; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{连接路由器}<br>//<br>// 返回成功...<br>&nbsp; &nbsp;result:=true;<br>end;<br><br>//<br>// 增加端口映射项(任务3)...<br>function TAutoNAT.AddNatMapping(NatPortName: string; ExternalPort: integer; LocalIp: string; LocalPort: integer; Protocol: string): boolean;<br>var<br>&nbsp; &nbsp;url,body: string;<br>&nbsp; &nbsp;j: integer;<br>begin<br>//<br>// 假如有任务在执行,返回失败...<br>&nbsp; &nbsp;if tasktimer.enabled then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result:=false;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; end;<br>//<br>// 生成请求串...<br>&nbsp; &nbsp;body:='&lt;?xml version="1.0"?&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;s:Body&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;u:AddPortMapping xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;NewRemoteHost&gt;&lt;/NewRemoteHost&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;NewExternalPort&gt;'+inttostr(externalport)+'&lt;/NewExternalPort&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;NewProtocol&gt;'+protocol+'&lt;/NewProtocol&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;NewInternalPort&gt;'+inttostr(localport)+'&lt;/NewInternalPort&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;NewInternalClient&gt;'+localip+'&lt;/NewInternalClient&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;NewEnabled&gt;1&lt;/NewEnabled&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;NewPortMappingDescription&gt;'+natportname+'&lt;/NewPortMappingDescription&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;NewLeaseDuration&gt;0&lt;/NewLeaseDuration&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;/u:AddPortMapping&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;/s:Body&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;/s:Envelope&gt;'#13#10;<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;url:=fcontrolurl;<br>&nbsp; &nbsp;delete(url,1,7);<br>&nbsp; &nbsp;j:=pos('/',url);<br>&nbsp; &nbsp;delete(url,1,j-1);<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;request:='POST '+url+' HTTP/1.1'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'Host: '+frouterip+':'+inttostr(routerport)+#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'SoapAction: "urn:schemas-upnp-org:service:WANIPConnection:1#AddPortMapping"'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'Content-Type: text/xml; charset="utf-8"'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'Content-Length: '+inttostr(length(body))+#13#10#13#10+body;<br>//<br>// 设置各标志...<br>&nbsp; &nbsp;ftasktype:=3; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {设备搜索任务}<br>&nbsp; &nbsp;taskexecuting:=true; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{任务在执行}<br>&nbsp; &nbsp;taskfinished:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{任务未完成}<br>&nbsp; &nbsp;tasksuccess:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {任务未成功}<br>&nbsp; &nbsp;requested:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {未提交请求}<br>//<br>// 启动延时定时器...<br>&nbsp; &nbsp;tasktimer.interval:=ftimeout; &nbsp; {超时值}<br>&nbsp; &nbsp;tasktimer.enabled:=true;<br>//<br>// 连接路由器...<br>&nbsp; &nbsp;sock.host:=frouterip; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {路由器IP}<br>&nbsp; &nbsp;sock.port:=frouterport; &nbsp; &nbsp; &nbsp; &nbsp; {端口}<br>&nbsp; &nbsp;sock.active:=true;<br>//<br>// 返回成功...<br>&nbsp; &nbsp;result:=true;<br>end;<br><br>//<br>// 取外网IP地址(任务4)...<br>function TAutoNAT.GetExternalIp(): boolean;<br>var<br>&nbsp; &nbsp;url,body: string;<br>&nbsp; &nbsp;j: integer;<br>begin<br>//<br>// 假如有任务在执行,返回失败...<br>&nbsp; &nbsp;if tasktimer.enabled then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result:=false;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; end;<br>//<br>// 生成请求串...<br>&nbsp; &nbsp;body:='&lt;?xml version="1.0"?&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;s:Body&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;u:GetExternalIPAddress xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;/u:GetExternalIPAddress&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;/s:Body&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;/s:Envelope&gt;'#13#10;<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;url:=fcontrolurl;<br>&nbsp; &nbsp;delete(url,1,7);<br>&nbsp; &nbsp;j:=pos('/',url);<br>&nbsp; &nbsp;delete(url,1,j-1);<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;request:='POST '+url+' HTTP/1.0'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'Host: '+frouterip+':'+inttostr(frouterport)+#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'SoapAction: "urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress"'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'Content-Type: text/xml; charset="utf-8"'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'Content-Length: '+inttostr(length(body))+#13#10#13#10+body;<br>//<br>// 设置各标志...<br>&nbsp; &nbsp;ftasktype:=4; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {设备搜索任务}<br>&nbsp; &nbsp;taskexecuting:=true; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{任务在执行}<br>&nbsp; &nbsp;taskfinished:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{任务未完成}<br>&nbsp; &nbsp;tasksuccess:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {任务未成功}<br>&nbsp; &nbsp;requested:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {未提交请求}<br>//<br>// 启动延时定时器...<br>&nbsp; &nbsp;tasktimer.interval:=ftimeout; &nbsp; {超时值}<br>&nbsp; &nbsp;tasktimer.enabled:=true;<br>//<br>// 连接路由器...<br>&nbsp; &nbsp;sock.host:=frouterip;<br>&nbsp; &nbsp;sock.port:=frouterport;<br>&nbsp; &nbsp;sock.active:=true;<br>//<br>// 返回成功...<br>&nbsp; &nbsp;result:=true;<br>end;<br><br>//<br>// 删除端口映射项(任务5)...<br>function TAutoNAT.DeleteNatMapping(ExternalPort: integer; Protocol: string): boolean;<br>var<br>&nbsp; &nbsp;url,body: string;<br>&nbsp; &nbsp;j: integer;<br>begin<br>//<br>// 假如有任务在执行,返回失败...<br>&nbsp; &nbsp;if tasktimer.enabled then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result:=false;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp; end;<br>//<br>// 生成请求串...<br>&nbsp; &nbsp;body:='&lt;?xml version="1.0"?&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;s:Body&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;u:DeletePortMapping xmlns:u="urn:schemas-upnp-org:service:WANIPConnection:1"&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;NewRemoteHost&gt;&lt;/NewRemoteHost&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;NewExternalPort&gt;'+inttostr(externalport)+'&lt;/NewExternalPort&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;NewProtocol&gt;'+protocol+'&lt;/NewProtocol&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;/u:DeletePortMapping&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;/s:Body&gt;'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; +'&lt;/s:Envelope&gt;'#13#10;<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;url:=fcontrolurl;<br>&nbsp; &nbsp;delete(url,1,7);<br>&nbsp; &nbsp;j:=pos('/',url);<br>&nbsp; &nbsp;delete(url,1,j-1);<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;request:='POST '+url+' HTTP/1.0'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'Host: '+routerip+':'+inttostr(routerport)+#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'SoapAction: "urn:schemas-upnp-org:service:WANIPConnection:1#DeletePortMapping"'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'Content-Type: text/xml; charset="utf-8"'#13#10<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+'Content-Length: '+inttostr(length(body))+#13#10#13#10+body;<br>//<br>// 设置各标志...<br>&nbsp; &nbsp;ftasktype:=5; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {设备搜索任务}<br>&nbsp; &nbsp;taskexecuting:=true; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{任务在执行}<br>&nbsp; &nbsp;taskfinished:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{任务未完成}<br>&nbsp; &nbsp;tasksuccess:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {任务未成功}<br>&nbsp; &nbsp;requested:=false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {未提交请求}<br>//<br>// 启动延时定时器...<br>&nbsp; &nbsp;tasktimer.interval:=ftimeout; &nbsp; &nbsp;{超时值}<br>&nbsp; &nbsp;tasktimer.enabled:=true;<br>//<br>// 连接路由器...<br>&nbsp; &nbsp;sock.host:=frouterip;<br>&nbsp; &nbsp;sock.port:=frouterport;<br>&nbsp; &nbsp;sock.active:=true;<br>//<br>// 返回成功...<br>&nbsp; &nbsp;result:=true;<br>end;<br><br>end.
 
使用这个控件可以:<br>1、查找路由器<br>2、查找路由器的控制页地址<br>3、增加一个NAT项<br>4、删除一个NAT项<br>5、读取路由器外的外网IP地址<br><br>象楼主这种需要用Socket进行双向访问的场合,可以用它来写程序。<br>象电驴eMule等软件里,就有实现自动NAT的功能。<br>我的QQ:776545524
 
后退
顶部