阅读量:1
引擎源码版,复制\Engine\Source\Programs\路径下的BlankProgram空项目示例。
重命名BlankProgram,例如CustomTcpProgram,并修改项目名称。
修改.Build.cs内容
修改Target.cs内容
修改Private文件夹内.h.cpp文件名并修改.cpp内容
刷新引擎
在项目.Build.cs中加入模块
// Copyright Epic Games, Inc. All Rights Reserved. using UnrealBuildTool; public class TCPSocketServer : ModuleRules { public TCPSocketServer(ReadOnlyTargetRules Target) : base(Target) { PublicIncludePathModuleNames.Add("Launch"); PrivateDependencyModuleNames.Add("Core"); PrivateDependencyModuleNames.Add("Projects"); //加入网络模块 PrivateDependencyModuleNames.Add("Sockets"); PrivateDependencyModuleNames.Add("ApplicationCore"); // to link with CoreUObject module: // PrivateDependencyModuleNames.Add("CoreUObject"); // to enable tracing: // AppendStringToPublicDefinition("UE_TRACE_ENABLED", "1"); // to enable LLM tracing: // GlobalDefinitions.Add("LLM_ENABLED_IN_CONFIG=1"); // GlobalDefinitions.Add("UE_MEMORY_TAGS_TRACE_ENABLED=1"); } }
.cpp
// Copyright Epic Games, Inc. All Rights Reserved. #include "TCPSocketServer.h" #include "RequiredProgramMainCPPInclude.h" #include "Sockets.h" #include "SocketSubsystem.h" DEFINE_LOG_CATEGORY_STATIC(LogTCPSocketServer, Log, All); IMPLEMENT_APPLICATION(TCPSocketServer, "TCPSocketServer"); INT32_MAIN_INT32_ARGC_TCHAR_ARGV() { //应用初始化设置 GEngineLoop.PreInit(ArgC,ArgV); // 获取网络套接字子系统 ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM); // 创建一个TCP类型的套接字,命名为"This TcpIp Type" FSocket* Socket = SocketSubsystem->CreateSocket(NAME_Stream,TEXT("This TcpIp Type")); // 创建一个互联网地址对象,用于指定服务器的IP地址和端口号 TSharedPtr<FInternetAddr> Addr = SocketSubsystem->CreateInternetAddr(); Addr->SetPort(8888); bool bIsValid = false; Addr->SetIp(TEXT("127.0.0.1"),bIsValid); Socket->Bind(*Addr); Socket->Listen(128); while (true) { // 接受一个新的客户端连接请求,并返回一个新的套接字对象来表示这个连接 FSocket* ClinetSocket = Socket->Accept(TEXT("ClinetConnect")); // 创建一个字节数组用于存储接收到的数据 uint8 Buf[1024]{0}; //存储实际读取到的字节数 int32 BytesRead = 0; // 从客户端套接字读取数据到Buf数组中,最多读取1024字节,并将实际读取的字节数存储在BytesRead中 ClinetSocket->Recv(Buf,1024,BytesRead); // 将接收到的字节数组转换为TCHAR字符串(用于UE5的字符串处理) FString Msg = UTF8_TO_TCHAR(reinterpret_cast<char *>(Buf)); UE_LOG(LogTCPSocketServer,Display,TEXT("Msg = %s||Len = %d"),*Msg,BytesRead); } } /* *TArray<FSocket*> Sockets; // 假设这是你的套接字列表 // 遍历套接字列表 for (int i = 0; i < Sockets.Num(); i++) { FSocket* Socket = Sockets[i]; FInternetAddr* RemoteAddr = Socket->GetRemoteAddress(); // 获取远程地址的IP和端口号 FString IP; uint16 Port; RemoteAddr->GetIP(IP); RemoteAddr->GetPort(Port); // 使用IP和端口号进行进一步的处理或记录等操作 UE_LOG(LogTCPSocketServer, Display, TEXT("Client IP: %s, Port: %d"), *IP, Port); } */
客户端.cpp
// Copyright Epic Games, Inc. All Rights Reserved. #include "TCPSocketClient.h" #include "RequiredProgramMainCPPInclude.h" #include "Sockets.h" #include "SocketSubsystem.h" DEFINE_LOG_CATEGORY_STATIC(LogTCPSocketClient, Log, All); IMPLEMENT_APPLICATION(TCPSocketClient, "TCPSocketClient"); INT32_MAIN_INT32_ARGC_TCHAR_ARGV() { GEngineLoop.PreInit(ArgC,ArgV); ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM); FSocket* Socket = SocketSubsystem->CreateSocket(NAME_Stream,TEXT("This Tcp Client")); TSharedPtr<FInternetAddr> Addr = SocketSubsystem->CreateInternetAddr(); Addr->SetPort(8888); bool bIsValid = false; Addr->SetIp(TEXT("127.0.0.1"),bIsValid); if (Socket->Connect(*Addr)) { FString Msg = TEXT("Hello Server"); int32 SendSize = 0; Socket->Send(reinterpret_cast<uint8*>(TCHAR_TO_UTF8(*Msg)),Msg.Len(),SendSize); } Socket->Close(); SocketSubsystem->DestroySocket(Socket); UE_LOG(LogTCPSocketClient, Display, TEXT("Hello World")); FEngineLoop::AppExit(); return 0; }
未完待续。。。