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"

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.

Product Version: Not Selected
Tags:
more ▼

asked Jun 03 '14 at 03:00 PM in C++ Programming

avatar image

tthorpe
6 3 5 9

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jun 03 '14 at 03:33 PM

avatar image

Ryan Gerleve STAFF
1.1k 25 12 49

avatar image tthorpe Jun 03 '14 at 05:08 PM

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:

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

 int32 port = 5001;
 
 TSharedRef<FInternetAddr> addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
 addr->SetAnyAddress();
 addr->SetPort(port);

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

 if (Socket != NULL)
 {
     Socket->Listen(1);
     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.

avatar image Ryan Gerleve STAFF Jun 03 '14 at 05:21 PM

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.

avatar image tthorpe Jun 03 '14 at 07:38 PM

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!

avatar image Ryan Gerleve STAFF Jun 03 '14 at 07:49 PM

"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.

avatar image tthorpe Aug 27 '14 at 02:44 PM

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

(comments are locked)
10|2000 characters needed characters left
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