Hi,
I’d like to allow my Android application to automatically connect to my game on a local network.
In order to do this I am trying to broadcast the host ip address on the local network using an udp FSocket.
I’m testing it between two computers on the same network. I can see the packets coming with Wireshark but I fail to catch them in unreal engine.
I tried several things:
- Creating an UDPReceiver with a callback. With this solution the callback is never called.
bool AUdpDataReceiver::StartUDPReceiver(const FString& YourChosenSocketName, const int32 ThePort)
{
int32 BufferSize = 2 * 1024 * 1024;
ListenSocket = FUdpSocketBuilder(*YourChosenSocketName)
.AsNonBlocking()
.AsReusable()
.BoundToAddress(FIPv4Address::Any)
.BoundToPort(ThePort)
.WithReceiveBufferSize(BufferSize)
.Build();
FTimespan ThreadWaitTime = FTimespan::FromMilliseconds(100);
UDPReceiver = new FUdpSocketReceiver(ListenSocket, ThreadWaitTime, TEXT("UDP RECEIVER"));
UDPReceiver->OnDataReceived().BindUObject(this, &AUdpDataReceiver::Recv);
UDPReceiver->Start();
return true;
}
void AUdpDataReceiver::Recv(const FArrayReaderPtr& ArrayReaderPtr, const FIPv4Endpoint& EndPt)
{
FString data;
ScreenMsg("Recv data");
*ArrayReaderPtr << data; //Now de-serializing
ScreenMsg(data);
UE_LOG(LogTemp, Warning, TEXT("DATADATADATA %s"), *data);
}
I tried this solution without BoundToAddress(FIPv4Address::Any) and with WithBroadcast().
- Calling Recv or RecvFrom in Tick to catch the data.
void AUdpDataReceiver::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
int bytesRead;
int32 BufferSize = 2 * 1024 * 1024;
uint8* data = nullptr;
TSharedRef < FInternetAddr > addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
addr->SetAnyAddress();
ListenSocket->RecvFrom(data, BufferSize, bytesRead, *addr);
ScreenMsg("DATADATADATA",(float) bytesRead);
UE_LOG(LogTemp, Warning, TEXT("DATADATADATA %f"), (float)bytesRead);
}
bool AUdpDataReceiver::StartUDPReceiver(const FString& YourChosenSocketName, const int32 ThePort)
{
int32 BufferSize = 2 * 1024 * 1024;
ListenSocket = FUdpSocketBuilder(*YourChosenSocketName)
.AsNonBlocking()
.AsReusable()
.BoundToAddress(FIPv4Address::Any)
.BoundToPort(ThePort)
.WithReceiveBufferSize(BufferSize)
.Build();
return true;
}
I tried this solution with the alternative ways of creating the sockets I explained before.
I tried everything I could think of but never seem to receive anything, did I miss something?