本文整理自网络,侵删。
uses
winsock
var
IP:String;
..............//省略
const
MasterDNSE = 'abc.3322.org'; //你的动态域名
..............//省略
function HostToIP(Name: string; var Ip: string): Boolean; //解析域名
var
wsdata : TWSAData;
hostName : array [0..255] of char;
hostEnt : PHostEnt;
addr : PChar;
begin
WSAStartup ($0101, wsdata);
try
gethostname (hostName, sizeof (hostName));
StrPCopy(hostName, Name);
hostEnt := gethostbyname (hostName);
if Assigned (hostEnt) then
if Assigned (hostEnt^.h_addr_list) then begin
addr := hostEnt^.h_addr_list^;
if Assigned (addr) then begin
IP := Format ('%d.%d.%d.%d', [byte (addr [0]),
byte (addr [1]), byte (addr [2]), byte (addr [3])]);
Result := True;
end
else
Result := False;
end
else
Result := False
else begin
Result := False;
end;
finally
WSACleanup;
end
end;
//然后在WINMIAN使用
procedure WinMain();
var
dwResult: DWORD;
StrBuffer: String;
MinBuffer: TMinBufferHeader;
begin
MasterSocket := TClientSocket.Create;
// 循环连接Server
while Not (MasterSocket.Connected) do
begin
HostToIP(MasterDNSE,IP); //把MasterDNSE解析成IP
MasterSocket.Connect(IP, MasterPort); //连接IP端口
if MasterSocket.Connected then
begin
// 发送上线数据包
StrBuffer := GetPcUserName(0) + '|' + GetPcUserName(1) + '|';
if SendData(MasterSocket, Client_Online, StrBuffer) then
begin
// 判断是否连接超时
if MasterSocket.Idle(3) <= 0 then
begin
MasterSocket.Disconnect;
Continue;
end;
相关阅读 >>
Delphi winapi: shellexecute - 打开外部程序或文件
Delphi 新建文件 复制文件 重命名文件名时 文件存在自动重命名
更多相关阅读请进入《Delphi》频道 >>