Getting data through UDP connection, no data received

Unreal Application will be the server. I need to get data to this server from client.

Code to create and bind UDP socket:

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

FString address = TEXT("127.0.0.1");
	int32 port = 8888;
	FIPv4Address ip;
	FIPv4Address::Parse(address, ip);
	auto addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
	addr->SetIp(ip.GetValue());
	addr->SetPort(port);
	
	Socket->Bind(*addr);

Code to get information:

auto addr_in = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
uint8 data[512];  int32 bytes_read = 0;
FString ret;
if (Socket->RecvFrom(data, sizeof(data), bytes_read, *addr_in)){
			UE_LOG(LogTemp, Warning, TEXT("PEND"));
			data[bytes_read] = 0;
			FString s((const char*)data);
			ret = s;
		}

No information((( bytes_read = -1…

Wanna try another way with

Socket = FUdpSocketBuilder(TEXT("SomeDescription"))
	 .AsNonBlocking()
	 .AsReusable()
	 .BoundToAddress(FIPv4Address(127, 0, 0, 1))
	 .BoundToPort(8888)
	 .WithMulticastLoopback();

......

But i really dont know how to use FUdpSocketReceiver

Client is fine!! Tested it with Server with winsockets.

Need any example with FULL source with headers and functions. I’ve never experience with sockets before.
Help me, please!

Is the sender on the same computer? Your UDP socket is bound to localhost (127.0.0.1) - if you intend to receive packets from other machines you should bind to one of your local IP addresses or to 0.0.0.0 for “any adapter”.

Make sure that port 8888 is not blocked by a personal firewall on your PC.

You also don’t need WithMulticastLoopback(), that’s only if you use multicast packets, but your socket is not set up to use multicast.

can you try .BindToAddress(FIPv4Address::Any)

Can you post/pastebin the whole class?

localhost, yes; Second fragment from Answerhab question, i found. I will go first way. Port ok, I have a pair Windows apps(Client/Server), made with Visual Studio 2013 for test. So, this App in Unreal use Servers port. AppServer is off. Port clear. But no message recieved.

yes, now i’m using addr->SetAnyAddress();

CLASS()
class AClientGameMode : public AGameMode
{
GENERATED_UCLASS_BODY()

	UFUNCTION(BlueprintCallable, meta = (FriendlyName = "Cognitive Power", CompactNodeTitle = "Cognitive", Keywords = "cognitive"), Category = "Emotiv")
	FString GetMindPower();
	UFUNCTION(BlueprintCallable, meta = (FriendlyName = "Init UDP", CompactNodeTitle = "Cognitive", Keywords = "udp"), Category = "Emotiv")
		void initUDP();
	UFUNCTION(BlueprintCallable, meta = (FriendlyName = "Close UDP", CompactNodeTitle = "Cognitive", Keywords = "udp close"), Category = "Emotiv")
		void closeUDP();
	private:
		FSocket* Socket;
		FInternetAddr* addr_in;
		uint8 data[512];
	//	auto addr_in;
	
};

#include “ClientTest.h”
#include “ClientGameMode.h”
#include “Sockets.h”
#include “SocketSubsystem.h”
#include “Networking.h”
#include “SharedPointer.h”

AClientGameMode::AClientGameMode(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
	Socket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_DGram, TEXT("default"), false);
}

void AClientGameMode::initUDP(){
	
	auto addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
	addr->SetAnyAddress(); //setIP
	addr->SetPort(7777);

	if (!Socket->Bind(*addr)){
		UE_LOG(LogTemp, Warning, TEXT("ppc"));
	}
	else{
		UE_LOG(LogTemp, Warning, TEXT("Bounded"));
	}
	//Socket->SetBroadcast(true);
}

void AClientGameMode::closeUDP(){
	Socket->Close();
}

FString AClientGameMode::GetMindPower(){
	  int32 bytes_read = 0;
	//uint32 datas;
	FString ret;
	memset(data, '\0', 512);

		if (Socket->RecvFrom(data, 512, bytes_read, *addr_in)){
			UE_LOG(LogTemp, Warning, TEXT("PEND"));
			data[bytes_read] = 0;
			FString s((const char*)data);
			ret = s;
		}

		ret = FString::FromInt(bytes_read);

	return ret;
	
	//else return "";
}

initUDP BP calling after EventBeginPlay; GetMindPower after EventTick if successfull inited

port 7777 is already used by the Engine. can you try a different port, i.e. 4000?

The address is no longer needed after the socket is bound. It’s a shared pointer, yes, and it will self destruct. I can’t see anything obviously wrong with your code :confused:

yes, i used different ports, tried 4000 now, may be the problem is in global variable FInternetAddr* addr_in? In this variable we get address of process, which sent information. Must be some kind of TSharedRef, but it’s not work. Halt.

Is your other test server shut down while you’re testing this? Only one server can be bound to that port at a time.

yes, I initialized it like FInternetAddr* addr_in;
When i tried TSharedPtr addr_in; it was breakdown in execution.

TSharedPtr<FInternetAddr, ESPMode::ThreadSafe> addr_in;

with this global initialisation there is break

yes, shuted

As i understand, if i send information from client to adress 127.0.0.1 and same port, information will be recieved in server, which was bounded with anyAdress? With standart windows sockets it is. Here - not.

Yeah, it should just work. I cannot see anything wrong with your code, but there has to be a bug somewhere. We are using similar code in the Engine, i.e. in FUdpMessageTransport

Can you show me alternative way, full?? With FUdpMessageTransport? or something else? please… working sample!!!