user10752826
user10752826

Reputation:

How check internet connection using Winsock api?

I built this code below (based in this example) to check if exists connection with internet using Winsock api and trying make a GET request to a specific website (google.com in my case).

But WSAGetLastError returns the error code: 10051.

How fix this?

program Project1;

uses
  Windows, SysUtils, WinSock;

function GetIPFromHostName(const HostName: string): string;
var
  WSAData: TWSAData;
  R: PHostEnt;
  A: TInAddr;
begin
  Result := '0.0.0.0';
  WSAStartup($101, WSAData);
  R := GetHostByName(PAnsiChar(AnsiString(HostName)));
  if Assigned(R) then
  begin
    A := PInAddr(R^.h_Addr_List^)^;
    Result := WinSock.inet_ntoa(A);
  end;
end;

function IsConnectedToInternet: Boolean;
var
  WSAData1: TWSAData;
  SockAddr1: TSockAddr;
  Socket1: TSocket;
begin
  Result := False;

  if WSAStartup(MAKEWORD(2, 2), WSAData1) <> 0 then
  begin
    Writeln('WinSock error');
    Exit;
  end;

  Socket1 := Socket(AF_INET, SOCK_STREAM, 0);
  if Socket1 = INVALID_SOCKET then
  begin
    Writeln('socket error');
    Exit;
  end;

  SockAddr1.sin_family := AF_INET;
  SockAddr1.sin_addr.S_addr := inet_addr(PAnsiChar(GetIPFromHostName('google.com')));
  SockAddr1.sin_port := htons(80);

  if Connect(Socket1, SockAddr1, SizeOf(SockAddr1)) <> 0 then
  begin
    Writeln('connection error #', WSAGetLastError);
    Exit;
  end;

  Result := True;

  if CloseSocket(Socket1) <> 0 then
    Writeln('error closing socket');
end;

begin
  IsConnectedToInternet;
  readln;

end.

Upvotes: 4

Views: 1098

Answers (1)

Sertac Akyuz
Sertac Akyuz

Reputation: 54772

The compiler is warning you for your error, on the line you are filling in_addr of the sockaddr_in:

W1044 Suspicious typecast of string to PAnsiChar

inet_addr expects an ANSI character string. You're typecasting a unicode string to a PAnsiChar and thus passing a malformed string, leading to connect unable to find the destination.

replace

SockAddr1.sin_addr.S_addr := inet_addr(PAnsiChar(GetIPFromHostName('google.com')));

with

SockAddr1.sin_addr.S_addr :=
    inet_addr(PAnsiChar(AnsiString(GetIPFromHostName('google.com'))));


You may use one of the APIs designed for this purpose, like InternetCheckConnection, or InternetGetConnectedState, or INetworkListManager interface (Vista and later).

E.g.

const
  FLAG_ICC_FORCE_CONNECTION = 1;
begin
  if not InternetCheckConnection('http://google.com', FLAG_ICC_FORCE_CONNECTION, 0) then
    RaiseLastOSError;

Upvotes: 10

Related Questions