TCP Socket send&recv 範例摘錄 (Server:C++ App, Client:UE4)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TCP Client (UE4) | |
// send FString | |
FString message = "welcome to here"; | |
TCHAR* seriallizedChar = message.GetCharArray().GetData(); | |
int32 size = FCString::Strlen(seriallizedChar) + 1; | |
int32 sentByte = 0; | |
HostSock->Send((uint8*)TCHAR_TO_UTF8(seriallizedChar), size, sentByte); | |
// recv FString | |
TArray<uint8> ReceiveData; | |
uint32 size = 1024u; | |
HostSock->HasPendingData(size); | |
ReceiveData.Init(0, FMath::Min(size, 65507u)); | |
int32 readByte = 0; | |
HostSock->Recv(ReceiveData.GetData(), ReceiveData.Num(), readByte); | |
UE_LOG(LogTemp, Warning, TEXT("Data Bytes Read : %s Byte."), *FString::FromInt(readByte)); | |
FString ReceivedUE4String = "Data : " + FString(ANSI_TO_TCHAR(reinterpret_cast<const char*>(ReceiveData.GetData()))); // byte to char | |
UE_LOG(LogTemp, Warning, TEXT("%s"), *ReceivedUE4String); | |
// send TArray<float> | |
TArray<float> data; | |
int32 ByteSent = 0; | |
FArrayWriter writer; | |
for (int i=0; i<data.Num(); i++) | |
{ | |
writer << data[i]; | |
} | |
HostSock->Send(writer.GetData(), writer.Num(), ByteSent) | |
// recv float | |
TArray<uint8> ReceiveData; | |
uint32 size = 1024u; | |
HostSock->HasPendingData(size); | |
ReceiveData.Init(0, FMath::Min(size, 65507u)); | |
int32 readByte = 0; | |
float decodeFloat[8]; // how many float data | |
HostSock->Recv(ReceiveData.GetData(), ReceiveData.Num(), readByte); | |
UE_LOG(LogTemp, Warning, TEXT("Data Bytes Read : %s Byte."), *FString::FromInt(readByte)); | |
for (int i = 0; i< readByte / 4; i++) | |
{ | |
TArray<uint8> binaryData; | |
binaryData.Add(ReceiveData.GetData()[(0 + i * 4) + 0]); | |
binaryData.Add(ReceiveData.GetData()[(0 + i * 4) + 1]); | |
binaryData.Add(ReceiveData.GetData()[(0 + i * 4) + 2]); | |
binaryData.Add(ReceiveData.GetData()[(0 + i * 4) + 3]); | |
decodeFloat[i] = *reinterpret_cast<float*>(binaryData.GetData()); // byte to float | |
ReceivedUE4String += FString::SanitizeFloat(decodeFloat[i]) + ", "; | |
} | |
UE_LOG(LogTemp, Warning, TEXT("%s"), *ReceivedUE4String); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TCP Server (C++ console application) | |
// send string | |
char* text = "PlayerName"; | |
char sendBuff[50]; | |
sprintf_s(sendBuff, "welcome %s to here", text); | |
send(clientSock, sendBuff, strlen(sendBuff) + 1, 0); | |
//recv string | |
char recvBuff[50]; | |
recv(clientSock, recvBuff, 50, 0); | |
printf(" %s \n\n", recvBuff); | |
// send float | |
float fp = 1.1f; | |
char* fp_char = (char*)&fp; | |
send(clientSock, fp_char, sizeof fp, 0); | |
// recv float | |
char recvBuff[256]; | |
int floatNum = 1; | |
recv(clientSock, recvBuff, sizeof(float) * floatNum, 0); | |
float decode_f = *reinterpret_cast<float*>(recvBuff); // byte to float | |
printf(" %f \n\n", decode_f); | |
// note: decode several flaot | |
//char f1[4] = { recvBuff[0], recvBuff[1], recvBuff[2], recvBuff[3] }; | |
//char f2[4] = { recvBuff[4], recvBuff[5], recvBuff[6], recvBuff[7] }; | |
//printf(" %f \n\n", *reinterpret_cast<float*>(f1)); | |
//printf(" %f \n\n", *reinterpret_cast<float*>(f2)); |