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"

Converting multiple integers into an integer that represents their byte arrays combined?

I'm trying to replicate what this C# code is doing:

 public bool SendCommand(ushort command) {
 
     // Make three byte arrays representing the values of thee integers (two unsigned shorts and an unsigned int to be exact, but it doesn't matter) called command, versionNumber and payloadSize
     byte[] commandB = BitConverter.GetBytes(command);
     ushort versionNumber = 1;
     byte[] versionNumberB = BitConverter.GetBytes(versionNumber);
     uint payloadSize = 0;
     byte[] payloadSizeB = BitConverter.GetBytes(payloadSize);
     
     // Concatenate the byte arrays into one block:
     byte[] datablock = new byte[commandB.Length + versionNumberB.Length + payloadSizeB.Length];
     System.Buffer.BlockCopy(commandB, 0, datablock, 0, commandB.Length);
     System.Buffer.BlockCopy(versionNumberB, 0, datablock, commandB.Length, versionNumberB.Length);
     System.Buffer.BlockCopy(payloadSizeB, 0, datablock, commandB.Length + versionNumberB.Length, payloadSizeB.Length);
     
     // Send the datablock through TCP, don't mind what this is m_stream is doing. Just trust me, that's what it does.
     m_stream.Write(datablock, 0, datablock.Length);
 
 }

It is using .NET features, namely BitConverter.GetBytes, to convert the integers "command", "versionNumber" and "payloadSize" into byte arrays, then combines them into one byte array, and sends that.

In UE4, to send things over TCP the Socket->Send functionality is used. However, it takes a uint8* as a parameter for the data it is sending, and not a byte array. I could simply use uint8(myInteger) to convert that "command" integer into a thing that is sendable, but I need to combine the three integers before sending them, like what System.Buffer.BlockCopy does, so that the raw binary data sent by TCP would be the same as the one sent by that C# script.

How do I do that?

p.s. and how do I determine the size of the data to send? I'm an absolute noob in this entire networking/tcp/whatever thing.


EDIT: byte is apparently non-existent in UE4, and i need to use chars instead. how? please help I'm drowning here

Product Version: UE 4.9
Tags:
more ▼

asked Sep 28 '15 at 01:52 PM in C++ Programming

avatar image

Kassym123
95 7 8 12

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

1 answer: sort voted first

uint8* is exactly array. In C++ any pointer can function as array and there really not array type, when you place [1] at the end it will read next byte (since uint8 is equivalent of byte) in next memory address. Thats why raw byta functions require you to input size of what he need to read, because you can't read size of array in C++ (or else you program it to do so, thats why we have TArray). In C++ it will look like this:

 bool SendCommand(uint16 command)  {
 
      uint16 versionNumber = 1;
      uint32 payloadSize = 0; //i don't know what size you use here, i assume it higher size then short so 32
 
      uint8* datablock = new uint8[8]; //16+16+32 in bits means, 2+2+4 in bytes
 
      //we copy varables to datablock
 
      FMemory::Memcpy(datablock, &command, 2);
      FMemory::Memcpy(datablock + 2, &versionNumber, 2);
      FMemory::Memcpy(datablock + 4, &payloadSize, 4);
 
     Socket->Send(datablock, 8);

     delete datablock; //for safety
 
 }

There you got, but in C++ you can make it a lot nicer, just make struct, because C++ place them in order (if i'm not mistaken, but i seen lot of binery protocol codes do that)

 struct FNetCommand {
      uint16 Command
      uint16 VersionNumber;
      uint32 PayloadSize;
 }
 
 bool SendCommand(FNetCommand command)  {
 
      uint8* datablock = new uint8[8]; 
      FMemory::Memcpy(datablock, &command, 8);
      Socket->Send(datablock, 8);
      delete datablock; 
 
 }
more ▼

answered Sep 28 '15 at 03:10 PM

avatar image

Shadowriver
37.2k 936 172 1116

avatar image Kassym123 Sep 28 '15 at 05:29 PM

thanks so much for this in-depth explanation and the code! this solves my problem! turns out i misunderstood the way C++ handles arrays, and I'm coming from a non-programmer background too. you, sir, are amazing! can't describe how thankful I am

(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