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"

FSocket->Recv no data response

I have gathered as much data as I could find in the documentation as well as around the web on UE4's socket networking ability. I have successfully connected to a server and sent data. The server echos the sent data back to the client. However, FSocket->Recv(blah, blah, blah) isn't reading any data back from the server. I have another client written in C# that does receive the data echo.

 void ANetwork_Test::TestTransmit(FString outData){
     
     // Stores the final server IP.
     FIPv4Address serverIP;
     // The ip address of the serverList Server.
     FString serverIpAddress = "192.168.1.11"; 
     // Port for connection to the serverList Server.
     int32 serverListPort = 24575;
     // Turn the string Ip into a usable ip and store it in serverIP.
     FIPv4Address::Parse(serverIpAddress, serverIP);
 
     // Create a network socket to connect to the serverList Server.
     sendSock = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream, TEXT("sendSock"), false);
 
     // Get a socketable internet address for use in the socket.
     TSharedRef < FInternetAddr > intAddr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
 
     // Set the IP and PORT of the new socketable internet address.
     intAddr->SetIp(serverIP.GetValue());
     intAddr->SetPort(serverListPort);
 
     // Attempt a connection and store result in the connectionAttempt bool.
     bool connectionAttempt = sendSock->Connect(*intAddr);
 
     if (connectionAttempt){
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Connect attempt SUCCESS!"));
         SendData(sendSock,outData);
     }
     else{
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Connect attempt failed."));
         return;
     }
 }

This is the SendData code:

 void ANetwork_Test::SendData(FSocket* client, FString data){
     TCHAR *sendData = data.GetCharArray().GetData();
     int32 size = FCString::Strlen(sendData);
     int32 sent = 0;
     uint8 *inData = 0;
     FString inDataString = "NO DATA";
     int32 byteCount = 0;
     uint32 dataSize = 0;
 
     client->Send((uint8*)TCHAR_TO_UTF8(sendData), size, sent);
     GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Data away!"));
     client->HasPendingData(dataSize);
     if (dataSize != 0){
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Got something."));
         client->Recv(inData, dataSize, byteCount);
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::FromInt(byteCount));
     }
 }


I haven't been able to get the data back from the server in UE4 but it works in the C# socket client I have. Any thoughts?

Thanks, -StewVanB

Product Version: Not Selected
Tags:
more ▼

asked Oct 23 '14 at 02:22 AM in C++ Programming

avatar image

StewVanB
6 1 1 1

avatar image ProfessionsQuest Oct 29 '14 at 08:46 PM

I would be interested in figuring this out as well. :| Having this same issue.

avatar image Moynzy Oct 29 '15 at 06:55 PM

Hi, did you find an answer? im able to recieve data from my server, but it prints it as 0.

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

3 answers: sort voted first

Hello, StewVanB

I am sorry to hear about your problem.

Please note that the common practice in this case is to put your send and response-handling logic in separate functions. This way, you can add a function like this:

 void ReceiveMessage()
 {
     // assuming that variables are initialized correctly
     client->HasPendingData(dataSize);
     if (dataSize != 0){
      GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Got something."));
       client->Recv(inData, dataSize, byteCount);
       GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::FromInt(byteCount));
     }

Now you can call it in timer:

  FTimerHandle MyTimerHandle;
  GetWorldTimerManager().SetTimerMyTimerHandle, this, &AMyActor::ReceiveMessage, 0.01, true);

Thus, you should get appropriate messages.

Hope this helped!

Have a great day!

more ▼

answered Nov 04 '15 at 04:43 PM

avatar image

Andrii Melnyk
1.9k 60 20 112

avatar image Moynzy Nov 05 '15 at 09:43 AM

Hello Andrew, I have tested your code in my project, and I am having the same problem as Stew.

Using the send method, my server crashes when it receives data.

Sending data from the server to the client, for example a string of test, prints out as -1.

Have you ever encountered this problem?

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

Same problem, again. Try the method, Not solved

more ▼

answered Mar 29 '18 at 01:29 AM

avatar image

guo13080319157
21 1

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

I was having the same problem.

The problem is that the buffer is null. with the changes the bytecount and the dataSize should be the same size.

void HandleRecieve() { uint32 dataSize = 0; client->HasPendingData(dataSize);

 if (dataSize != 0) 
 {
     uint8 *inData = new uint8[dataSize];
     int32 byteCount = 0;

     GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, TEXT("Recieved data from server."));
     socket->Recv(inData, dataSize, byteCount);
     GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Yellow, FString::FromInt(byteCount));
 }

}

more ▼

answered Jan 23 '19 at 08:20 PM

avatar image

Magesturm
1

(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