Download

Host->Connect(*addr) seems wait a very long time

I am writing a tcp client to connect to a tcp server.Part of codes are as following:
FIPv4Address::Parse(IPStr, ip); //将传入的IPStr转为IPv4地址 //创建一个addr存放ip地址和端口 TSharedPtr<FInternetAddr> addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr(); addr->SetIp(ip.Value); addr->SetPort(port); //创建客户端socket Host = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream, TEXT(“default”), false); //连接成功 if (Host->Connect(*addr)) { UE_LOG(LogTemp, Warning, TEXT(“Connect Succeed!”)); return true; } //连接失败 else { UE_LOG(LogTemp, Warning, TEXT(“Connect Failed!”)); return false; } The problem is that when the connection failed(if the server did not run),Host->Connect(*addr) seems wait a very long time.
It takes about 20s for the program to go to the ‘else’ part when the connection failed.

The socket is likely blocking your execution until it times out, you could put that connection logic in another thread so it’s not blocking your main thread.

Thanks a lot!