To check the Internet connection state in Delphi, you can use the TIdHTTP
component from the Indy
library. This component provides a Head
method that allows you to send an HTTP HEAD request to a specified URL. You can use this method to check the connection to a known website, such as “www.google.com“.
Here is an example of how you can use the TIdHTTP
component to check the Internet connection state in Delphi:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
uses IdHTTP; procedure TForm1.Button1Click(Sender: TObject); var HTTP: TIdHTTP; Success: Boolean; begin HTTP := TIdHTTP.Create(nil); try try HTTP.Head(‘http://www.google.com’); Success := True; except Success := False; end; if Success then ShowMessage(‘Connected to the Internet’) else ShowMessage(‘Not connected to the Internet’); finally HTTP.Free; end; end; |
This code creates an instance of the TIdHTTP
component, and then uses the Head
method to send an HTTP HEAD request to “www.google.com“. If the request is successful, it means that the Internet connection is working and the message “Connected to the Internet” is displayed. If the request fails, it means that the Internet connection is not working and the message “Not connected to the Internet” is displayed.
Note that this approach may not work if the website you are trying to reach is down or if there are other issues with the connection. In that case, you may need to use other techniques to check the Internet connection state.
How can i do this without TIdHTTP ?
If you don’t want to use the TIdHTTP
component from the Indy
library, you can use the WinINet
API to check the Internet connection state in Delphi. The WinINet
API provides functions that allow you to perform various Internet-related tasks, such as checking the connection to a specific website or retrieving the list of available network connections.
Here is an example of how you can use the WinINet
API to check the Internet connection state in Delphi:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
uses Winapi.Windows, Winapi.WinINet; function CheckInternetConnection: Boolean; var hInternet: HINTERNET; begin hInternet := InternetOpen(‘Delphi’, INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0); try Result := InternetCheckConnection(‘http://www.google.com’, FLAG_ICC_FORCE_CONNECTION, 0); finally InternetCloseHandle(hInternet); end; end; procedure TForm1.Button1Click(Sender: TObject); begin if CheckInternetConnection then ShowMessage(‘Connected to the Internet’) else ShowMessage(‘Not connected to the Internet’); end; |
This code uses the InternetOpen
function to initialize the WinINet
library and the InternetCheckConnection
function to check the connection to “www.google.com“. If the connection is successful, the function returns True
and the message “Connected to the Internet” is displayed. If the connection fails, the function returns False
and the message “Not connected to the Internet” is displayed.
Note that this approach may not work if the website you are trying to reach is down or if there are other issues with the connection. In that case, you may need to use other techniques to check the Internet connection state.
How can i do this without TIdHTTP and WinINet ?
If you don’t want to use the TIdHTTP
component from the Indy
library or the WinINet
API, you can use the WinSock
API to check the Internet connection state in Delphi. The WinSock
API provides functions that allow you to perform various network-related tasks, such as creating and manipulating sockets.
Here is an example of how you can use the WinSock
API to check the Internet connection state in Delphi:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
uses Winapi.Windows, Winapi.WinSock; function CheckInternetConnection: Boolean; var WSAData: TWSAData; Socket: TSocket; Address: TSockAddr; begin Result := False; if WSAStartup($0101, WSAData) = 0 then try Socket := WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, nil, 0, 0); if Socket <> INVALID_SOCKET then try Address.sin_family := AF_INET; Address.sin_port := htons(80); Address.sin_addr.S_addr := inet_addr(‘64.233.167.99’); if WSAConnect(Socket, Address, SizeOf(Address), nil, nil, nil, nil) = 0 then Result := True; finally closesocket(Socket); end; finally WSACleanup; end; end; procedure TForm1.Button1Click(Sender: TObject); begin if CheckInternetConnection then ShowMessage(‘Connected to the Internet’) else ShowMessage(‘Not connected to the Internet’); end; |
This code uses the WSAStartup
function to initialize the WinSock
library and the WSASocket
function to create a socket. It then uses the WSAConnect
function to try to connect to the specified IP address (in this case, the IP address of “www.google.com“). If the connection is successful, the function returns True
and the message “Connected to the Internet” is displayed. If the connection fails, the function returns False
and the message “Not connected to the Internet” is displayed.
Note that this approach may not work if there are issues with the connection or if the IP address you are trying to reach is not reachable. In that case, you may need to use other techniques to check the Internet connection state.
How can i do this without TIdHTTP ,WinINet and Winsock ?
If you don’t want to use the TIdHTTP
component from the Indy
library, the WinINet
API, or the WinSock
API, you can use the GetAddrInfo
function from the WinSock2
API to check the Internet connection state in Delphi. The GetAddrInfo
function allows you to resolve a hostname to an IP address, which you can then use to check the connection to a specific website.
Here is an example of how you can use the GetAddrInfo
function to check the Internet connection state in Delphi:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
uses Winapi.Windows, Winapi.WinSock2; function CheckInternetConnection: Boolean; var Hints: TAddrInfoW; AddrInfo: PAddrInfoW; Res: Integer; begin FillChar(Hints, SizeOf(Hints), 0); Hints.ai_family := AF_INET; Hints.ai_socktype := SOCK_STREAM; Hints.ai_protocol := IPPROTO_TCP; Res := GetAddrInfoW(‘www.google.com’, ’80’, @Hints, AddrInfo); Result := Res = 0; if AddrInfo <> nil then FreeAddrInfoW(AddrInfo); end; procedure TForm1.Button1Click(Sender: TObject); begin if CheckInternetConnection then ShowMessage(‘Connected to the Internet’) else ShowMessage(‘Not connected to the Internet’); end; |
This code uses the GetAddrInfoW
function to resolve the hostname “www.google.com” to an IP address. If the function returns a value of 0
, it means that the hostname was successfully resolved and the connection is likely working. If the function returns a different value, it means that the hostname could not be resolved and the connection is not working.
Note that this approach may not work if the website you are trying to reach is down or if there are other issues with the connection. In that case, you may need to use other techniques to check the Internet connection state.
Leave a Reply