Download

Socket Connection Fails Everytime

Hi,
I am trying to find a way to see if the PC is connected to internet before hosting a session, If i do so without the internet connect I get a FATAL ERROR crash.

Here is my code that i have written in Blueprint Function library, It always returns false.

	FSocket* Socket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket("PingSocket", TEXT("Default"), false);

		FIPv4Address Ip(172,217,19,4);
		TSharedRef<FInternetAddr> Addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
		Addr->SetIp(Ip.Value);
		Addr->SetPort(7776);

		Socket->Connect(*Addr);

		if(Socket->GetConnectionState() != ESocketConnectionState::SCS_Connected)
		{
			GEngine->AddOnScreenDebugMessage(-1, 2.0f, FColor::Red, TEXT("Not Connected To Internet"));
			return false;
		}

I am trying to put in google IP to see if it “Pings” back thus confirming the connectivity with internet.

If anyone has a better way of doing this please share.

That’s not a particularly good/well-known address. It could be re-mapped at any time.

You could, for example, attempt to resolve “www.steampowered.com” using DNS, and try to connect to port 443 on that IP address (just disconnect after connecting, no need to read anything.)

Another option is to send a pre-generated DNS lookup using a UDP packet to one of the well-known addreses 8.8.8.8 (Google global DNS) or 1.1.1.1 (Cloudflare global DNS) and look for an answer. You can really copy the payload of any outgoing DNS request, but generating an unlikely-to-exist address procedurally is slightly better because it won’t be locally cached already.

C:\Users\Eric>telnet 172.217.19.4 7776
Connecting To 172.217.19.4…Could not open connection to the host, on port 7776: Connect failed

… there’s nothing on port 7776 on that host.

1 Like

Ops My bad i forgot to change that, port 80 works wonders so the problem is solved.
Are there any resources that show how to connect to something using URL?