目錄:
本次僅作簡單測試,尚未進行多執行緒測試。
環境:
服務端測試**:
#include "stdafx.h"
#include
#include
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
using
namespace
std;
int main()
serversock = socket(af_inet, sock_stream, 0);
serveraddr.sin_family = af_inet;
serveraddr.sin_port = htons(4399);
serveraddr.sin_addr.s_un.s_addr = inaddr_any;
int retval = bind(serversock, (sockaddr*)&serveraddr, sizeof(serveraddr));
if (socket_error == retval)
retval = listen(serversock, 5);
if (socket_error == retval)
int addrclientlen = sizeof(clientaddr);
clientsock = accept(serversock, (sockaddr*)&clientaddr, &addrclientlen);
while (1)
;inet_ntop(af_inet, (void*)&clientaddr.sin_addr, ipdotdec, 16);
sprintf_s(sendbuff, "from server: welcome %s to here", ipdotdec);
send(clientsock, sendbuff, strlen(sendbuff) + 1, 0);
char recvbuff[50];
recv(clientsock, recvbuff, 50, 0);
printf(" %s \n\n", recvbuff);
sleep(1000);
}closesocket(serversock);
wsacleanup();
return0;}
客戶端測試**:
#include "stdafx.h"
#define _winsock_deprecated_no_warnings
#include
#include
#include
#pragma comment(lib,"ws2_32.lib")
using
namespace
std;
int main()
hostsock= socket(af_inet, sock_stream, ipproto_tcp);
serveraddr.sin_family = af_inet;
serveraddr.sin_addr.s_un.s_addr = inet_addr("127.0.0.1");
serveraddr.sin_port = htons(4399);
int serveraddrlen = sizeof(serveraddr);
retval = connect(hostsock, (sockaddr*)&serveraddr, serveraddrlen);
if (socket_error == retval)
int index = 0;
while (true)
getchar();
closesocket(hostsock);
wsacleanup();
return0;}
效果:
建立socketactor c++ class
ue4 c++工程的 .build.cs
using unrealbuildtool;
public
class socketandjsontest : modulerules
); privatedependencymodulenames.addrange(new
string );}}
socketactor.h
#pragma once
#include "gameframework/actor.h"
#include "networking.h"
#include "socketactor.generated.h"
uclass()
class socketandjsontest_api asocketactor : public aactor
;
socketactor.cpp
#include
"socketandjsontest.h"
#include
"socketactor.h"
// sets default values
asocketactor::asocketactor()
// called when the game starts or when spawned
void asocketactor::beginplay()
// called every frame
void asocketactor::tick(float deltatime)
bool asocketactor::socketcreate(fstring ipstr, int32 port)
else*/}
else
}void asocketactor::socketsend(fstring meesage)
else
}bool asocketactor::socketreceive()
tarray receivedata;
uint32 size;
uint8 element =
0; while (host->haspendingdata(size))
if (receivedata.num()<=
0)
fstring log
="total data read! num: "
+fstring::fromint(receivedata.num() <=
0); gengine->addonscreendebugmessage(1, 2.0f, fcolor::green, log);
ue_log(logtemp, warning, text("recv log: %s"), *
log);
const fstring receivedue4string = stringfrombinaryarray(receivedata);
log="server:"
+ receivedue4string;
gengine->addonscreendebugmessage(1, 2.0f, fcolor::green, log);
ue_log(logtemp, warning, text("*** %s"), *
log);
return
true;
}fstring asocketactor::stringfrombinaryarray(tarray binaryarray)
ue4 藍圖
效果
參考
ue4部分:
UE4學習平台
自虛幻開源以來,ue4在中國都沒有存粹技術交流的社群 滿眼望去各種令人眼花繚亂的廣告以及套路深厚的商業模式 為了營造國內良好的技術氛圍 我們打算認真的把這個論壇做起來 改善中國 ue4的交流方式,互相解答各自的疑惑 精力有限,已經關閉了 精力有限,已經關閉了 精力有限,已經關閉了 精力有限,已經關閉...
UE4學習總結
標頭檔案 include components staticmeshcomponent.h 任意類 include camera cameracomponent.h 攝像機 include components inputcomponent.h 按鈕控制獲取 include gameframewor...
UE4學習筆記 Gameplay Classes
增加類 編輯器中有乙個類嚮導,可以生成類所需要的檔案和基礎巨集 標頭檔案由於ue4對c 做了擴充套件,實現了如序列化反射等特性,跟qt的做法類似 在 中加入一些巨集關鍵字,在編譯期間生成相應的c 因此,對於每個類來說,都有會生成乙個classname.generated.h的檔案 類宣告uclass...