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;总是返回FALSE;我是这样写的: if ScanTCPPort('222.73.63.166',80) then showmessage('ok') else showmessage('no');大师帮忙检查下 哪里有错误吗? 编译时还有2个小提示..
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;总是返回FALSE;我是这样写的: if ScanTCPPort('222.73.63.166',80) then showmessage('ok') else showmessage('no');大师帮忙检查下 哪里有错误吗? 编译时还有2个小提示..
解决方案 »
- 怎么还原最小化的程序窗口
- 国庆小礼物:帮你翻译
- 菜单图标的原来是灰色的,有鼠标焦点后变成彩色的效果怎么做...
- Delphi6专业版没法在WinXP以非管理员身份安装,如何解决,个人版可以么?
- 急,帮忙看看这段代码:OleContainer1调用excel 的问题
- Delphi7中怎么没有Tserversockt和Tclientsocket?怎样安装这些组件?
- 如何给一个存储过程(Oracle数据库)的输出参数(游标)赋值,详细内容...
- delphi7怎么设置其快捷键
- 请问ps为后缀的的文件用什么软件打开?
- 急:在DBEdit,DBMemo等编辑框如何获得刚刚敲的字符
- delphi 打开 cvs更新下来的 代码中文显示 ??
- C/S数据库软件网络中断的处理办法
Result := connect(TcpSock, InAddr, sizeof(InAddr)) = 0;
Return Value
If no error occurs, connect returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.
你刚好搞反了.
InAddr.sin_addr.S_addr := 你看的IP是多少?
轉錯了。