Juvarunst
(Juvarunst)
January 12, 2018, 4:22pm
1
I’m writing socket to receive data code
This my code:
TArray<uint8> ReceiveData;
uint8 element = 0;
while (true)
{
ReceiveData.Init(element, 1024u);
int32 read = 0;
Socket->Recv(ReceiveData.GetData(), ReceiveData.Num(), read);
}
I can receive data ,
I’m trying to receive an int data,
how to convert uint8 array to int?
Do you receiving int32 data or you want to convert bytes to int32? note that uint8 is blueprint/property editor compatible as Byte type so you can actually use it without any conversion
Juvarunst
(Juvarunst)
January 12, 2018, 7:49pm
4
I want to send the data I received to the blueprint
Juvarunst
(Juvarunst)
January 12, 2018, 7:52pm
5
My message format looks like this:
int PlayerID, string PlayerName
I received uint8
I am looking for a way to turn it into the original type
Maybe 1 int is equal to 4 uint8?
Juvarunst
(Juvarunst)
January 12, 2018, 8:25pm
6
I just found out that uint8 is unsigned char
Juvarunst
(Juvarunst)
January 12, 2018, 8:34pm
7
This is my code, but it’s still wrong:
TArray<uint8> ReceiveData;
uint8 element = 0;
TArray<uint8> Buffer;
while (true)
{
ReceiveData.Init(element, 4);
int32 read = 0;
Socket->Recv(ReceiveData.GetData(), ReceiveData.Num(), read);
int test_int = static_cast<int>(*(ReceiveData.GetData()));
NetworkMgr->RecvData(test_int);
}
I’m trying to modify it…
Juvarunst
(Juvarunst)
January 12, 2018, 8:55pm
8
I want to convert bytes to int
Juvarunst
(Juvarunst)
January 13, 2018, 12:14pm
9
This is my code:
uint8 recvBuffer[4];
uint8 element = 0;
while (true)
{
int32 read = 0;
Socket->Recv(recvBuffer, 4, read, ESocketReceiveFlags::WaitAll);
int test_int = (int) *recvBuffer;
NetworkMgr->RecvData(test_int);
}
It looks like it’s working,
But it still can not pass a negative number
Juvarunst
(Juvarunst)
January 13, 2018, 12:21pm
10
I sent this data from the server:
int PackageLength (4 bit) , int ProtoID (4 bit) , int LoginResult (4 bit) , string LoginKey (32 bit)
The value of the server is:
PackageLength = 44 , ProtoID = 1 , LoginResult = -1 , LoginKey ="\x00\x00\x00\x00…"
My ue4 client received data is: (Currently only consider the int type)
PackageLength = 44 , ProtoID = 1 , LoginResult = 255 , …
Juvarunst
(Juvarunst)
January 13, 2018, 9:55pm
11
This is my solution:
int ABaseNetworkMgr::GetIntByBuffer(TArray<uint8> Buffer, int Offset)
{
int int_value;
unsigned char char_list[4];
for (int i = 0; i < 4; i++)
{
char_list[i] = Buffer.GetData()[i+ Offset];
}
memcpy(&int_value, char_list, 4);
return int_value;
}
But I did not take into account the case of Big-Endian and Little-Endian of.
And in cross-platform, memory allocation may be a problem.
1 Like
Juvarunst
(Juvarunst)
January 13, 2018, 10:32pm
12
Cross-platform seems to be able to use FMemory :: Memcpy ()