Third party Server to Client - Recv function question

I have created a client to receive information from a third party server, but I am unsure of how to use the Recv function.

I have created and initialized the socket parameters, and I’m attempting to receive the data from the server. Here is my current code for receiving information:

uint8 * data = NULL;
uint32 size = 1000;
int32 bytes_read;
ESocketReceiveFlags::Type flags;

Socket->Recv( data, size, bytes_read, flags);

The code errors with ‘flags’ stating that the variable is uninitialized and that “no ‘object’ file generated”. How do I set up this flags variable to use the Recv function? Are there any future problems that anyone could see occurring?

Thanks for your help and feedback.

In the vast majority of cases ESocketReceiveFlags::None will be what you want to pass to the Recv functions. This is also the default argument, so you may omit it from the call. The comments in ESocketReceiveFlags describe what each one does, but briefly, None will fill the Data buffer up to BufferSize and remove the data from the internal queue, Peek fills the buffer in the same way but leaves the data in the internal queue (you’d get the same data if you call Recv again), and WaitAll will cause the call to block until certain conditions are met.

There is another issue I see with your code - Recv does not allocate memory for the caller. Make sure that the “data” value passed in can store size number of bytes. The Unreal socket functions closely resemble BSD sockets, it may be useful to go through a good BSD socket tutorial.

Also a general comment on networking - any case where you receive data over the network is a potential security risk. This is a huge topic beyond the scope here, but briefly it’s good practice to sanity-check the data received: range-check numbers, make sure strings are terminated, and so on.

Hi. Thanks for replying back. I got rid of the errors. But, I am not able to receive packet information. Just to narrow down the problem, I wrote a C++ UDP listener code. I was able to receive packets on that. But, I cant receive packets in Unreal. I am getting bytes read=-1. Here is the unreal code that I wrote:

Socket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream, TEXT(“default”), true);

int32 port = 5001;

TSharedRef<FInternetAddr> addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();

    bool connectedX = Socket->Connect(*addr);

if (Socket != NULL)
	Thread = FRunnableThread::Create(this, TEXT("FPrimeNumberWorker"), bAutoDeleteSelf, bAutoDeleteRunnable, 0, TPri_BelowNormal); 

Receive code in multithread function: (I am receiving bytes_read=-1)

uint8 data[100000];
uint32 size = 100;
int32 bytes_read;// = NULL;

if (Socket != NULL)
	Socket->Recv(data, size, bytes_read);

Thanks for your help. I appreciate it.

Your client code in Unreal will be slightly different depending on whether the server uses TCP or UDP. Typically Socket->Connect and Socket->Recv are used with TCP. For UDP, create the socket with NAME_DGram, there’s no need to use Connect, and use the RecvFrom function instead of Recv.

“DGram” stands for “Datagram” and is the traditional name for the distinct packets of data sent over UDP. The name “Stream” better describes TCP, since conceptually the protocol just transmits a continuous stream of bytes that is not broken up into “packets”.

“DGram” and “Stream” are the names used by BSD-style socket libraries, and I strongly recommend searching the Web for a BSD socket programming tutorial. Once you get familiar with BSD socket programming you’ll start to notice the similarities in the Unreal interface.

Very briefly, RecvFrom provides the caller with the address of the sender. This is useful in UDP because it’s a connectionless protocol: unlike TCP the data can come from anywhere - not just the address you Connect()ed to.

Ryan, thank you immensely for your help! We were able to confirm the packets were sending and receiving correctly.

Can you tell me where I can learn why the socket creation needed NAME_Dgram? I was also confused on why there is a difference between Recv and RecvFrom.

Thanks again Ryan!

Ryan, thank you immensely for the information. I greatly appreciate it!