当前位置:   article > 正文

UE5 独立程序的网络TCP/UDP服务器与客户端基础流程_ue5 socket utp

ue5 socket utp

引擎源码版,复制\Engine\Source\Programs\路径下的BlankProgram空项目示例。

重命名BlankProgram,例如CustomTcpProgram,并修改项目名称。

 修改.Build.cs内容

修改Target.cs内容 

 

 修改Private文件夹内.h.cpp文件名并修改.cpp内容

刷新引擎

在项目.Build.cs中加入模块

  1. // Copyright Epic Games, Inc. All Rights Reserved.
  2. using UnrealBuildTool;
  3. public class TCPSocketServer : ModuleRules
  4. {
  5. public TCPSocketServer(ReadOnlyTargetRules Target) : base(Target)
  6. {
  7. PublicIncludePathModuleNames.Add("Launch");
  8. PrivateDependencyModuleNames.Add("Core");
  9. PrivateDependencyModuleNames.Add("Projects");
  10. //加入网络模块
  11. PrivateDependencyModuleNames.Add("Sockets");
  12. PrivateDependencyModuleNames.Add("ApplicationCore");
  13. // to link with CoreUObject module:
  14. // PrivateDependencyModuleNames.Add("CoreUObject");
  15. // to enable tracing:
  16. // AppendStringToPublicDefinition("UE_TRACE_ENABLED", "1");
  17. // to enable LLM tracing:
  18. // GlobalDefinitions.Add("LLM_ENABLED_IN_CONFIG=1");
  19. // GlobalDefinitions.Add("UE_MEMORY_TAGS_TRACE_ENABLED=1");
  20. }
  21. }

 .cpp

  1. // Copyright Epic Games, Inc. All Rights Reserved.
  2. #include "TCPSocketServer.h"
  3. #include "RequiredProgramMainCPPInclude.h"
  4. #include "Sockets.h"
  5. #include "SocketSubsystem.h"
  6. DEFINE_LOG_CATEGORY_STATIC(LogTCPSocketServer, Log, All);
  7. IMPLEMENT_APPLICATION(TCPSocketServer, "TCPSocketServer");
  8. INT32_MAIN_INT32_ARGC_TCHAR_ARGV()
  9. {
  10. //应用初始化设置
  11. GEngineLoop.PreInit(ArgC,ArgV);
  12. // 获取网络套接字子系统
  13. ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM);
  14. // 创建一个TCP类型的套接字,命名为"This TcpIp Type"
  15. FSocket* Socket = SocketSubsystem->CreateSocket(NAME_Stream,TEXT("This TcpIp Type"));
  16. // 创建一个互联网地址对象,用于指定服务器的IP地址和端口号
  17. TSharedPtr<FInternetAddr> Addr = SocketSubsystem->CreateInternetAddr();
  18. Addr->SetPort(8888);
  19. bool bIsValid = false;
  20. Addr->SetIp(TEXT("127.0.0.1"),bIsValid);
  21. Socket->Bind(*Addr);
  22. Socket->Listen(128);
  23. while (true)
  24. {
  25. // 接受一个新的客户端连接请求,并返回一个新的套接字对象来表示这个连接
  26. FSocket* ClinetSocket = Socket->Accept(TEXT("ClinetConnect"));
  27. // 创建一个字节数组用于存储接收到的数据
  28. uint8 Buf[1024]{0};
  29. //存储实际读取到的字节数
  30. int32 BytesRead = 0;
  31. // 从客户端套接字读取数据到Buf数组中,最多读取1024字节,并将实际读取的字节数存储在BytesRead中
  32. ClinetSocket->Recv(Buf,1024,BytesRead);
  33. // 将接收到的字节数组转换为TCHAR字符串(用于UE5的字符串处理)
  34. FString Msg = UTF8_TO_TCHAR(reinterpret_cast<char *>(Buf));
  35. UE_LOG(LogTCPSocketServer,Display,TEXT("Msg = %s||Len = %d"),*Msg,BytesRead);
  36. }
  37. }
  38. /*
  39. *TArray<FSocket*> Sockets; // 假设这是你的套接字列表
  40. // 遍历套接字列表
  41. for (int i = 0; i < Sockets.Num(); i++)
  42. {
  43. FSocket* Socket = Sockets[i];
  44. FInternetAddr* RemoteAddr = Socket->GetRemoteAddress();
  45. // 获取远程地址的IP和端口号
  46. FString IP;
  47. uint16 Port;
  48. RemoteAddr->GetIP(IP);
  49. RemoteAddr->GetPort(Port);
  50. // 使用IP和端口号进行进一步的处理或记录等操作
  51. UE_LOG(LogTCPSocketServer, Display, TEXT("Client IP: %s, Port: %d"), *IP, Port);
  52. }
  53. */

客户端.cpp

  1. // Copyright Epic Games, Inc. All Rights Reserved.
  2. #include "TCPSocketClient.h"
  3. #include "RequiredProgramMainCPPInclude.h"
  4. #include "Sockets.h"
  5. #include "SocketSubsystem.h"
  6. DEFINE_LOG_CATEGORY_STATIC(LogTCPSocketClient, Log, All);
  7. IMPLEMENT_APPLICATION(TCPSocketClient, "TCPSocketClient");
  8. INT32_MAIN_INT32_ARGC_TCHAR_ARGV()
  9. {
  10. GEngineLoop.PreInit(ArgC,ArgV);
  11. ISocketSubsystem* SocketSubsystem = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM);
  12. FSocket* Socket = SocketSubsystem->CreateSocket(NAME_Stream,TEXT("This Tcp Client"));
  13. TSharedPtr<FInternetAddr> Addr = SocketSubsystem->CreateInternetAddr();
  14. Addr->SetPort(8888);
  15. bool bIsValid = false;
  16. Addr->SetIp(TEXT("127.0.0.1"),bIsValid);
  17. if (Socket->Connect(*Addr))
  18. {
  19. FString Msg = TEXT("Hello Server");
  20. int32 SendSize = 0;
  21. Socket->Send(reinterpret_cast<uint8*>(TCHAR_TO_UTF8(*Msg)),Msg.Len(),SendSize);
  22. }
  23. Socket->Close();
  24. SocketSubsystem->DestroySocket(Socket);
  25. UE_LOG(LogTCPSocketClient, Display, TEXT("Hello World"));
  26. FEngineLoop::AppExit();
  27. return 0;
  28. }

未完待续。。。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/174168
推荐阅读
相关标签
  

闽ICP备14008679号