WinSock问题:写了一个自定义超时连接的function.可不知道问题出在哪?走过路过不要错过阿,可能是很简单的问题 ( 积分: 200 )

  • 主题发起人 主题发起人 行到水穷处
  • 开始时间 开始时间

行到水穷处

Unregistered / Unconfirmed
GUEST, unregistred user!
TcpConnection 在不能連通的情況下要延遲20秒之多,因此另外改進為下一個TCPConnection2<br><br>50個綫程連續調用 TcpConnection 掃描不同的ip(從192.168.1.1到192.168.1.255升序地時候,192.168.1.255:86 的結果是錯誤的,而反过来降序: 從192.168.1.255到192.168.1.1 扫描的結果是正確的,意味着,开始扫描的结果是正确的,越靠后越不对。<br>192.168.1.255:86是可以連接上的,從 192.168.1.1開始掃描到1.255的時候就不行了,暈阿<br><br>function &nbsp; TcpConnection(_Host:String;_Port:Integer):integer;<br>var i:integer;<br> &nbsp; &nbsp;sin:sockaddr_in;<br> &nbsp; &nbsp;mint :integer ;<br>begin<br> &nbsp;sin.sin_family:=AF_INET;<br> &nbsp;sin.sin_port:=htons(_Port);<br> &nbsp;i:=inet_addr(PChar(_Host));<br> &nbsp;sin.sin_addr.S_addr:=i;<br> &nbsp;mint := 0;<br> &nbsp;try<br> &nbsp; mint &nbsp;:= socket(PF_INET,SOCK_STREAM,0);<br> &nbsp;if Connect(mint,sin,sizeof(sin))=SOCKET_ERROR then<br>//上面這句在不能連通的情況下要延遲20秒之多,因此另外改進為下一個TCPConnection2<br> &nbsp;Result := 0<br> &nbsp;else<br> &nbsp; &nbsp;Result :=1 ;<br> &nbsp;finally<br> &nbsp; &nbsp;CloseSocket(mint);<br> &nbsp;end;<br>end;<br>//-----------------改進后的TcpConnection2---------------<br>function TcpConnection2(_Host:String;_Port:Integer;_TimeOut: integer):integer;<br>var i:integer;<br> &nbsp; &nbsp;sin:sockaddr_in;<br> &nbsp; &nbsp;mint :integer ;<br> &nbsp; &nbsp;fs: TFDSet;<br> &nbsp; &nbsp;timeout: timeval;<br> &nbsp; &nbsp;ulb: integer;<br> &nbsp; &nbsp;RC:integer;<br>begin<br> &nbsp;sin.sin_family:=AF_INET;<br> &nbsp;sin.sin_port:=htons(_Port);<br> &nbsp;i:=inet_addr(PChar(_Host));<br> &nbsp;sin.sin_addr.S_addr:=i;<br> &nbsp;mint := 0;<br> &nbsp;FD_ZERO(fs);<br> &nbsp;try<br> &nbsp; &nbsp;mint &nbsp;:= socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);<br> &nbsp; &nbsp;FD_SET(mint,fs);<br> &nbsp; &nbsp;timeout.tv_sec := _TimeOut;//连接超时<br> &nbsp; &nbsp;//延时_TimeOut 為 10秒 &nbsp;<br> &nbsp; &nbsp;timeout.tv_usec := 500;<br> &nbsp; &nbsp;ulb := 1;<br> &nbsp; &nbsp;ioctlsocket(mint,FIONBIO,ulb);<br> &nbsp; &nbsp;if Connect(mint,sin,sizeof(sin))=SOCKET_ERROR then<br> &nbsp; &nbsp;begin//<br> &nbsp; &nbsp; &nbsp;if (WSAGetLastError() = WSAEWOULDBLOCK) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;FD_ZERO(fs);<br> &nbsp; &nbsp; &nbsp; &nbsp;FD_SET(mint,fs);<br> &nbsp; &nbsp; &nbsp; &nbsp;RC := select(0,nil,@fs,nil,@TimeOut);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;if RC = 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := 0<br> &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := 1;<br> &nbsp; &nbsp;end// &nbsp;返回的Result 有问题???????<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp;Result := 1;<br> &nbsp;finally<br> &nbsp; &nbsp;ulb := 0;<br> &nbsp; &nbsp;ioctlsocket(mint,FIONBIO,ulb);<br> &nbsp; &nbsp;CloseSocket(mint);<br> &nbsp;end;<br>end;
 
TcpConnection 在不能連通的情況下要延遲20秒之多,因此另外改進為下一個TCPConnection2<br><br>50個綫程連續調用 TcpConnection 掃描不同的ip(從192.168.1.1到192.168.1.255升序地時候,192.168.1.255:86 的結果是錯誤的,而反过来降序: 從192.168.1.255到192.168.1.1 扫描的結果是正確的,意味着,开始扫描的结果是正确的,越靠后越不对。<br>192.168.1.255:86是可以連接上的,從 192.168.1.1開始掃描到1.255的時候就不行了,暈阿<br><br>function &nbsp; TcpConnection(_Host:String;_Port:Integer):integer;<br>var i:integer;<br> &nbsp; &nbsp;sin:sockaddr_in;<br> &nbsp; &nbsp;mint :integer ;<br>begin<br> &nbsp;sin.sin_family:=AF_INET;<br> &nbsp;sin.sin_port:=htons(_Port);<br> &nbsp;i:=inet_addr(PChar(_Host));<br> &nbsp;sin.sin_addr.S_addr:=i;<br> &nbsp;mint := 0;<br> &nbsp;try<br> &nbsp; mint &nbsp;:= socket(PF_INET,SOCK_STREAM,0);<br> &nbsp;if Connect(mint,sin,sizeof(sin))=SOCKET_ERROR then<br>//上面這句在不能連通的情況下要延遲20秒之多,因此另外改進為下一個TCPConnection2<br> &nbsp;Result := 0<br> &nbsp;else<br> &nbsp; &nbsp;Result :=1 ;<br> &nbsp;finally<br> &nbsp; &nbsp;CloseSocket(mint);<br> &nbsp;end;<br>end;<br>//-----------------改進后的TcpConnection2---------------<br>function TcpConnection2(_Host:String;_Port:Integer;_TimeOut: integer):integer;<br>var i:integer;<br> &nbsp; &nbsp;sin:sockaddr_in;<br> &nbsp; &nbsp;mint :integer ;<br> &nbsp; &nbsp;fs: TFDSet;<br> &nbsp; &nbsp;timeout: timeval;<br> &nbsp; &nbsp;ulb: integer;<br> &nbsp; &nbsp;RC:integer;<br>begin<br> &nbsp;sin.sin_family:=AF_INET;<br> &nbsp;sin.sin_port:=htons(_Port);<br> &nbsp;i:=inet_addr(PChar(_Host));<br> &nbsp;sin.sin_addr.S_addr:=i;<br> &nbsp;mint := 0;<br> &nbsp;FD_ZERO(fs);<br> &nbsp;try<br> &nbsp; &nbsp;mint &nbsp;:= socket(PF_INET,SOCK_STREAM,IPPROTO_TCP);<br> &nbsp; &nbsp;FD_SET(mint,fs);<br> &nbsp; &nbsp;timeout.tv_sec := _TimeOut;//连接超时<br> &nbsp; &nbsp;//延时_TimeOut 為 10秒 &nbsp;<br> &nbsp; &nbsp;timeout.tv_usec := 500;<br> &nbsp; &nbsp;ulb := 1;<br> &nbsp; &nbsp;ioctlsocket(mint,FIONBIO,ulb);<br> &nbsp; &nbsp;if Connect(mint,sin,sizeof(sin))=SOCKET_ERROR then<br> &nbsp; &nbsp;begin//<br> &nbsp; &nbsp; &nbsp;if (WSAGetLastError() = WSAEWOULDBLOCK) then<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;FD_ZERO(fs);<br> &nbsp; &nbsp; &nbsp; &nbsp;FD_SET(mint,fs);<br> &nbsp; &nbsp; &nbsp; &nbsp;RC := select(0,nil,@fs,nil,@TimeOut);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;if RC = 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := 0<br> &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := 1;<br> &nbsp; &nbsp;end// &nbsp;返回的Result 有问题???????<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp;Result := 1;<br> &nbsp;finally<br> &nbsp; &nbsp;ulb := 0;<br> &nbsp; &nbsp;ioctlsocket(mint,FIONBIO,ulb);<br> &nbsp; &nbsp;CloseSocket(mint);<br> &nbsp;end;<br>end;
 
有什么问题
 
掃描到254试一试。
 
if Connect(mint,sin,sizeof(sin))=SOCKET_ERROR then<br> &nbsp; &nbsp;begin//<br> &nbsp; &nbsp; &nbsp;if (WSAGetLastError() = WSAEWOULDBLOCK) then//不成立RC=?前面要为 RC赋个初始值吧<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;FD_ZERO(fs);<br> &nbsp; &nbsp; &nbsp; &nbsp;FD_SET(mint,fs);<br> &nbsp; &nbsp; &nbsp; &nbsp;RC := select(0,nil,@fs,nil,@TimeOut);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;if RC = 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := 0<br> &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := 1;<br> &nbsp; &nbsp;end// &nbsp;返回的Result 有问题???????<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp;Result := 1;
 
leaber:<br>你好,见到你真高兴!<br>問題是這樣的:<br> TcpConnection,这个在连接不通的情况下要延时20多秒(另外一个TcpConnection2就是对此改进:可以自定义延时时间,但是我在多线程里面调用这个改进后的TcpConnection2,得出的结果却没有TcpConnection准确,<br>下列測試代碼説明了問題所在:<br>假設 從192.168.1.1到192.168.1.235 中只有 192.168.1.235:86是能連接通的 ,for i := 100 to 235 do 這個循環 執行后的結果 j为0<br>但若使用for i := 235 downto 100 do這個循環的話,结果是为1;<br>意味著這個改進后的函數只能在有限的次數下有效。<br>這可怎麽辦?<br> &nbsp;for i := 100 to 235 do<br>// &nbsp;for i := 235 downto 100 do<br> &nbsp;begin<br> &nbsp; &nbsp;k := TcpConnection2('192.168.253.'+IntToStr(i),86,1);<br> &nbsp; &nbsp;j := j + k;<br> &nbsp;end;
 
呵,应该没问题的,检查每次执行的返回。<br>Rc最好初始化一下
 
RC赋个初始值:修改后已经赋值,但是没效果。<br><br>来自:ak_2005, 时间:2005-6-13 10:34:17, ID:3102608<br> if Connect(mint,sin,sizeof(sin))=SOCKET_ERROR then<br> &nbsp; &nbsp;begin//<br> &nbsp; &nbsp; &nbsp;if (WSAGetLastError() = WSAEWOULDBLOCK) then//不成立RC=?前面要为 RC赋个初始值吧<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;FD_ZERO(fs);<br> &nbsp; &nbsp; &nbsp; &nbsp;FD_SET(mint,fs);<br> &nbsp; &nbsp; &nbsp; &nbsp;RC := select(0,nil,@fs,nil,@TimeOut);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;if RC = 0 then<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := 0<br> &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := 1;<br> &nbsp; &nbsp;end// &nbsp;返回的Result 有问题???????<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp;Result := 1;
 
RC在begin后赋初始值为0,但依旧没什么效果阿<br><br>怎么办????<br><br><br>给点主意啊?
 
不要只是想着这个超时的地方,看看别的地方。比如你的服务端,算法..
 
to leaber:下面的测试程序仅仅是更改了一个升降序,结果却完全不同,我认为肯定是程序哪儿出了问题了,有可能是资源释放问题,也有可能是TcpConnection2内部的问题<br>请你你帮忙测试一下,跟踪跟踪,到底哪儿有问题呢?哪儿出错了到底<br><br><br> &nbsp;for i := 100 to 235 do<br>// &nbsp;for i := 235 downto 100 do<br> &nbsp;begin<br> &nbsp; &nbsp;k := TcpConnection2('192.168.253.'+IntToStr(i),86,1);<br> &nbsp; &nbsp;j := j + k;<br> &nbsp;end;
 
端口号能不能设得高一点.[:(]
 
wlmmlw:<br>实际测试中的端口是8890呢<br><br>晕。<br><br>哪位能看出bug来啊?<br>急死了
 
下列测试代码就说明,这个TCPconnection2是有问题的。<br>aiirii(ari-淘金坑)<br>你说呢?<br><br> &nbsp;for i := 0 to 50 do<br> &nbsp;begin<br> &nbsp; &nbsp;k := TcpConnection('192.168.253.231',887);<br> &nbsp; &nbsp;j := j + k;<br> &nbsp;end;<br>///运行后j为51<br><br> &nbsp;for i := 0 to 50 do<br> &nbsp;begin<br> &nbsp; &nbsp;k := TcpConnection2('192.168.253.231',887,5);<br> &nbsp; &nbsp;j := j + k;<br> &nbsp;end;<br><br>///运行后j为40
 
上边的现象这样解释好象是合理的:<br>select(0,nil,@fs,nil,@TimeOut)=0 只能说明在指定的时间没有接受到连接已经建立的消息,但并不能说明对方不能连接。所以设置超时的判据不可靠
 
迷糊的说法是正确的。<br>谢谢
 
后退
顶部