请问这个判断端口的函数为何返回FALSE(看看吧, 不超过20行)? 在线等候,(200)

  • 主题发起人 主题发起人 mrniu
  • 开始时间 开始时间
M

mrniu

Unregistered / Unconfirmed
GUEST, unregistred user!
function ScanTCPPort(ipstr : string; Port: DWORD): Boolean;var option: DWORD; TcpSock: TSocket; InAddr: TSockAddrIn; IP : DWORD;begin result := False; IP := ntohl(inet_addr(PChar(ipstr))); if IP = INADDR_NONE then //invalid IP address! exit; // Create/open a socket (stream, not datagram) TcpSock := socket(AF_INET, SOCK_STREAM, 0); if TcpSock = INVALID_SOCKET then //socket error exit; try // Set socket options option := 0; setsockopt(TcpSock, SOL_SOCKET, SO_KEEPALIVE, @option, sizeof(option)); option := 1; setsockopt(TcpSock, SOL_SOCKET, SO_DONTLINGER, @option, sizeof(option)); //if winsock 1.1, including the next sentence, otherwise, skip it. setsockopt(TcpSock, IPPROTO_TCP, TCP_NODELAY, @option, sizeof(option)); //Initialize address structure ZeroMemory(@InAddr, sizeof(InAddr)); InAddr.sin_family := AF_INET; InAddr.sin_addr.S_addr := ntohl(IP); InAddr.sin_port := htons(Port); //Try to connect Result := connect(TcpSock, InAddr, sizeof(InAddr)) = 0; finally //Close the socket closesocket(TcpSock); end;end;if ScanTCPPort('222.73.63.166',80) then showmessage('ok') else showmessage('no');总是no? 是不是代码本身错误? 我测试多次都不可以, 编译时, 还有些问题.请大师帮忙修改下. 谢谢了.
 
真乱。。没看你的。。直接写一个function ScanTCPPort(ipstr : string; Port: DWORD): Boolean;var wVersionrequested:Word; wData:WSADATA; err:integer; sockClient:TSocket; addrSrv:sockaddr_in;begin Result:=False; wVersionrequested:=MAKEWORD(1,1); err:=WSAStartup(wVersionrequested,wData); if err<>0 then Exit; if ((LOBYTE(wData.wVersion)<>1) or (HIBYTE(wData.wVersion)<>1)) then begin WSACleanup(); exit; end; try sockClient:=socket(AF_INET,SOCK_STREAM,0); addrSrv.sin_family:=AF_INET; addrSrv.sin_port:=htons(port); addrSrv.sin_addr.S_addr:=inet_addr(PChar(ipstr)); Result:=connect(sockClient,addrSrv,SizeOf(addrSrv))=0; finally closesocket(sockClient) ; WSACleanup(); end;end;
 
非常感谢。。。。你是好人。
 
后退
顶部