Delphi programming based on IP address to get host name

Source: Internet
Author: User

In the process of designing a network program, it is often necessary to obtain the name of the host according to the IP address obtained, this example will demonstrate how to obtain the name of the corresponding host according to the IP address entered by the user.

Add two Tlabel components, two tedit components, and a TButton component to the form, as shown in Figure 1 of the finished design.

Figure 1 Main interface

In the process of running the program first need to add Winsock to the uses section of the program, so that you can invoke the Winsock related functions to operate.

Attributes and filename are two global variables. Where the word type's attributes variable is used to store the properties of the file, and the string FileName property is used to store the path and file name of the file.

Additionally, add the following code to the Formcreate procedure:

procedure TfrmMain.btnConversionClick(Sender: TObject);
  var
   WSAData: TWSAData;
   HostEnt: PHostEnt;
   IPAddress: string;
   addr: dword;
  begin
   edtName.Clear;
   WSAStartup(2, WSAData);
   IPAddress:=edtAddress.Text;
   try
    addr := inet_addr(PChar(IPAddress));
    HostEnt:= GetHostByAddr(@addr, Length(IPAddress), PF_INET);
    edtName.Text:=HostEnt.h_name;
   except
    ShowMessage('无效的IP地址');
   end;
   WSACleanup;
end;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.