x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

UDP Listener not getting messages

I am trying to create a UDP listener but I do not seem to be getting any messages and I am not sure what step I am missing

 UNetworkManager* UNetworkManager::ConstructNetworkManager(const FString& Description, const FString& UDPSocketName,
     const FString& TheIP, const int32 ThePort, const int32 BufferSize,
     const bool AllowBroadcast, const bool Bound, const bool Reusable,
     const bool Blocking)
 {
     UNetworkManager* wrapper = Constructor();
 
     wrapper->SocketSubsystem = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM);
     FIPv4Address::Parse(TheIP, wrapper->RemoteAdress);
 
     wrapper->RemoteEndPoint = FIPv4Endpoint(wrapper->RemoteAdress, ThePort);
 
     // First set our socket null
     wrapper->UDPSocket = nullptr;
     UE_LOG(LogTemp, Warning, TEXT("ConstructNetworkManager step 1"));
     if (wrapper->SocketSubsystem != nullptr) // If socket subsytem is good
     {
         UE_LOG(LogTemp, Warning, TEXT("ConstructNetworkManager step 2"));
         wrapper->UDPSocket = wrapper->SocketSubsystem->CreateSocket(NAME_DGram, *Description, true);
 
         if (wrapper->UDPSocket != nullptr) // Is our socket created
         {
             UE_LOG(LogTemp, Warning, TEXT("ConstructNetworkManager step 3"));
             // Setup the socket 
             bool Error = !wrapper->UDPSocket->SetNonBlocking(!Blocking) ||
                 !wrapper->UDPSocket->SetReuseAddr(Reusable) ||
                 !wrapper->UDPSocket->SetBroadcast(AllowBroadcast) ||
                 !wrapper->UDPSocket->SetRecvErr();
 
             if (!Error)
             {
                 UE_LOG(LogTemp, Warning, TEXT("ConstructNetworkManager step 4"));
                 if (Bound)
                 {
                     Error = !wrapper->UDPSocket->Bind(*wrapper->RemoteEndPoint.ToInternetAddr());
                 }
             }
 
             if (!Error)
             {
                 UE_LOG(LogTemp, Warning, TEXT("ConstructNetworkManager step 5"));
                 int32 OutNewSize;
 
                 wrapper->UDPSocket->SetReceiveBufferSize(BufferSize, OutNewSize);
                 wrapper->UDPSocket->SetSendBufferSize(BufferSize, OutNewSize);
                 wrapper->UDPSocket->Listen(10);
             }
 
             if (Error)
             {
                 UE_LOG(LogTemp, Warning, TEXT("ConstructNetworkManager step 6"));
                 GLog->Logf(TEXT("FUdpSocketBuilder: Failed to create the socket %s as configured"), *Description);
 
                 wrapper->SocketSubsystem->DestroySocket(wrapper->UDPSocket);
                 wrapper->UDPSocket = nullptr;
             }
         }
 
     }
 
 
     return wrapper;
 }
 
 FString UNetworkManager::GrabWaitingMessage()
 {
     uint32 Size;
 
     TSharedRef<FInternetAddr> Sender = SocketSubsystem->CreateInternetAddr();
 
     while (UDPSocket->HasPendingData(Size))
     {
         int32 Read = 0;
         //ReceivedData.Init(FMath::Min(Size, 65507u));
         ReceivedData.SetNum(Size);
         UDPSocket->RecvFrom(ReceivedData.GetData(), ReceivedData.Num(), Read, *Sender);
     }
 
 
     return StringFromBinaryArray(ReceivedData);
 
 }
 
 FString UNetworkManager::StringFromBinaryArray(const TArray<uint8>& BinaryArray)
 {
     //Create a string from a byte array!
     const std::string cstr(reinterpret_cast<const char*>(BinaryArray.GetData()), BinaryArray.Num());
 
     //FString can take in the c_str() of a std::string
     return FString(cstr.c_str());
 }
 
 
 bool UNetworkManager::anyMessages()
 {
     uint32 Size;
     if (UDPSocket != NULL && UDPSocket->HasPendingData(Size))
     {
         return true;
     }
 
     return false;
 }

Here are the property declarations FSocket* UDPSocket; // Description for debugging FString SocketDescription;

 // Remote Address
 FIPv4Endpoint RemoteEndPoint;
 FIPv4Address RemoteAdress;

 // Socket Subsystem
 ISocketSubsystem* SocketSubsystem;

 // The data
 TArray<uint8> ReceivedData;
 TArray<uint8> SendData;


I can use these setup to send UDP messages but not to recieve.

I call the factor method with 192.168.1.255, 6800, allowbroadcast true, bound false, reusable true, blocking true. But whenever Ic all the anymessage function there is no waiting data.

Any help Is appreciated

Product Version: UE 4.15
Tags:
more ▼

asked Oct 12 '17 at 12:18 PM in C++ Programming

avatar image

aspartamed
120 26 34 40

avatar image aspartamed Oct 13 '17 at 07:42 PM

This code worked as is. The issue was the means I was using to send the data. The website wont allow me to delete it

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

Hi. You have to bound UDP socket to a local address to receive anything.

     // Bind socket to our port.
     LocalAddr = SocketSubsystem->GetLocalBindAddr(*GLog);
     
     LocalAddr->SetPort(bInitAsClient ? GetClientPort() : URL.Port);
     
     int32 AttemptPort = LocalAddr->GetPort();
     int32 BoundPort = SocketSubsystem->BindNextPort( Socket, *LocalAddr, MaxPortCountToTry + 1, 1 );
more ▼

answered Oct 12 '17 at 07:08 PM

avatar image

c4tnt
1k 13 8 31

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

bind to 0.0.0.0 to listen for broadcast messages on the default nic

more ▼

answered Oct 13 '17 at 07:44 PM

avatar image

aspartamed
120 26 34 40

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question