标签:
UE4 dedicated server是一个很好的游戏服务器,但是对于大厅和聊天服务器来说,我们要自行开发,通过UE4的socket组建很方便的和其他服务器建立连接
FSocket* Socket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream, TEXT("default"), false);
CreateSocket参数
FString address = TEXT("127.0.0.1"); int32 port = 19834; FIPv4Address ip; FIPv4Address::Parse(address, ip);
TSharedRef addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr(); addr->SetIp(ip.GetValue()); addr->SetPort(port);
连接服务器
bool connected = Socket->Connect(*addr);
如果返回True说明连接服务器成功.
FString serialized = TEXT("loadPlayer|1");
TCHAR *serializedChar = serialized.GetCharArray().GetData();
int32 size = FCString::Strlen(serializedChar);
int32 sent = 0;
发送内容FString转换成TCHAR*,格式化发送数据
bool successful = Socket->Send((uint8*)TCHAR_TO_UTF8(serializedChar), size, sent);
接受数据我们用到 HasPendingData 和Recv 函数在 FSocket 类中.
在工程中引用Sockets模块
PublicDependencyModuleNames.AddRange(
new string[]
{
"Core",
"CoreUObject",
"Engine",
"Sockets"
}
);
标签:
原文地址:http://www.cnblogs.com/bodboy/p/5104726.html