Delphi based on IP address to get the host name

Source: Internet
Author: User
Tags response code

This example demonstrates how to use the Tudpsocket component to get the IP address and host name.

Add a Tudpsocket component to the form, as shown in Figure 1.

Figure 1 Main interface

There are two properties--localhostname and localhostaddr in the Tudpsocket component that store the host name and IP address of the local computer, so add the following response code in the initialization of the form to display the host name and IP address:

procedure TForm1.FormCreate(Sender: TObject);
  begin
   self.Caption:=UdpSocket1.LocalHostName;
   self.Caption:=self.Caption+’:’+UdpSocket1.LocalHostAddr;
end;

The program code is as follows:

unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Sockets, ExtCtrls;
type
  TForm1 = class(TForm)
  UdpSocket1: TUdpSocket;
  procedure FormCreate(Sender: TObject);
private
  { Private declarations }
public
  { Public declarations }
end;
var
  Form1: TForm1;
  implementation
  {$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
  begin
   self.Caption:=UdpSocket1.LocalHostName;
   self.Caption:=self.Caption+’:’+UdpSocket1.LocalHostAddr;
  end;
end.

Save the file, and then press F9 to run the program, and the results of the program run as shown in Figure 2.

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.