关于socks5 udp不能发收数据,高手帮忙看下代码。。。(25)

X

xernet

Unregistered / Unconfirmed
GUEST, unregistred user!
//使用winsock2.pas单元。procedure UDPSocks(skt: tsocket;var buf;len:integer);var saproxy:TSockAddr; pSocksAddr:pChar; Re:integer; ErrCode:Integer; AuthSucc:boolean; serverip,serverport:String; NameLen: Integer; UDPBuf: array[0..255] of Byte;//请求和发送数据用。 HeadBuf : array[0..1024] of Byte;//接收数据用。 saLocal: TSockAddrIn; saiLocal:TSockAddrIn; saiProxy:TSockAddrIn; ReBind:integer; ProxyAddr: Pchar; //保存Proxy的映射地址 ProxyPort: Word; //保存Proxy的映射端口号 hostaddr: Pchar; //保存Proxy的绑定地址 hostport: Word; //保存Proxy的绑定端口号 nLen:integer;begin serverip:='192.168.0.102';//服务器地址 serverport:='1080';//服务器端口 NameLen:= SizeOf(saLocal); getsockname(skt, @saLocal, NameLen);//得到当前socket状态。 skt:=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); if(skt=INVALID_SOCKET) then begin Exit; end; ZeroMemory(@saproxy,sizeof(saproxy)); saproxy.sin_family := AF_INET; GetMem(pSocksAddr,Length(serverip)+1); ZeroMemory(pSocksAddr,Length(serverip)+1); StrPCopy(pSocksAddr,serverip); saproxy.sin_addr.S_addr :=inet_addr(pSocksAddr); FreeMem(pSocksAddr); saproxy.sin_port := htons(strtoint(serverport)); Re:=connect(skt,@saproxy,sizeof(saproxy)); if Re = SOCKET_ERROR then begin ErrCode:=WSAGetLastError(); Exit; end; AuthSucc:=PublicAuth(skt);//这是密码和名字验证函数,测试代理服务器上能看到验//测试代理服务器上能看到验证。 if not AuthSucc then begin CloseSocket(skt); Exit; end; //发送UDP请求。 FillChar(UDPBuf, 256, $0); UDPBuf[0]:= $05; //协议版本Socks5 UDPBuf[1]:= $03; //Socks命令:UDP UDPBuf[2]:= $00; //保留 UDPBuf[3]:= $01; //地址类型IPv4 CopyMemory(@UDPBuf[4], @saLocal.sin_addr, 4); CopyMemory(@UDPBuf[8], @saLocal.sin_port, 2); send(Skt, UDPBuf, 10, 0); FillChar(UDPBuf, 256, #0); recv(Skt, UDPBuf, 256, 0); if (UDPBuf[0] <> $05) and (UDPBuf[1] <> $00)then begin Exit; end; CopyMemory(@ProxyAddr, @UDPBuf[4], 4); //获取Proxy的映射地址 CopyMemory(@ProxyPort, @UDPBuf[8], 2); //获取Proxy的映射端口号 Skt:=WSASocket(AF_INET,SOCK_DGRAM,0,nil,0,WSA_FLAG_OVERLAPPED); ZeroMemory(@saiLocal,sizeof(saiLocal)); saiLocal.sin_family := AF_INET; saiLocal.sin_port := 0; saiLocal.sin_addr.S_addr := INADDR_ANY; REbind:=bind(skt, @saiLocal, sizeof(saiLocal)); if REbind= SOCKET_ERROR then begin exit; end; NameLen:= SizeOf(saiLocal); getsockname(skt, @saiLocal, NameLen); hostaddr:=inet_ntoa(saiLocal.sin_addr);//本机绑定地址 hostport:=ntohs(saiLocal.sin_port); //本机绑定端口 ZeroMemory(@saiProxy,sizeof(saiProxy)); saiProxy.sin_family := AF_INET; saiProxy.sin_addr.S_addr := inet_addr(ProxyAddr) ;// 代理绑定的udp地址 saiProxy.sin_port :=ntohs(ProxyPort); // 代理绑定的udp端口//实际发送UDP数据 FillChar(UDPBuf, 256, $0); UDPBuf[0]:= $00; //保留 UDPBuf[1]:= $00; //保留 UDPBuf[2]:= $00; //是否分段重组(此处不用) UDPBuf[3]:= $01; //IPv4 CopyMemory(@UDPBuf[4], @hostaddr, 4); //代理服务器地址 CopyMemory(@UDPBuf[8], @hostport, 2); //代理服务器端口 CopyMemory(@UDPBuf[10], @buf, len); //实际数据 sendto(skt,UDPbuf,Len+10,0,@saiProxy,sizeof(saiProxy));//接收数据并剔除头信息。 nLen := sizeof(saiProxy); FillChar(HeadBuf, 1025, $0); recvfrom(SKT,HeadBuf,1024,0,@saiProxy,nLen); Assert(HeadBuf[0] = $00); //保留 Assert(HeadBuf[1] = $00); //保留 Assert(HeadBuf[2] = $00); //是否分段重组 Assert(HeadBuf[3] = $01); //IPv4 CopyMemory(@saiProxy.sin_addr, @HeadBuf[4], 4); //代理服务器地址 CopyMemory(@saiProxy.sin_port, @HeadBuf[8], 2); //代理服务器端口 CopyMemory(@buf, @HeadBuf[10], len); //实际数据end;
 
怎么都没有人回答,连个建议都没有,大富翁的高手都哪里去了???
 
用 CommView 截包看看。
 
接受答案了.
 
还是自己解决了!!!!!!
 
顶部