找回密码
 立即注册
查看: 357|回复: 0

unreal-c++教程-第八章:TCP Socket

[复制链接]
发表于 2023-4-13 09:14 | 显示全部楼层 |阅读模式
基本数据类型byte与int

    1. 添加Socket库和NetWorking库2. 一个实用网络类


1. 添加Socket库和NetWorking库



打开该文件


添加Sockets和Networking


关闭引擎,退出,重新开始
此时,你将会成功 的引入网络库

2. 一个实用网络类
  1. // Fill out your copyright notice in the Description page of Project Settings.#pragmaonce#include"HAL/Runnable.h"#include"CoreMinimal.h"#include"GameFramework/Actor.h"#include"Sockets.h"#include"Sockets.h"#include"SocketSubsystem.h"#include"Networking/Public/Interfaces/IPv4/IPv4Address.h"#include"SocketSubsystem.h"
  2. class FRunableThread;
  3. class AClient : public FRunnable
  4. {
  5. public:// Sets default values for this actor's propertiesAClient();
  6.         virtual bool Init();
  7.         virtual uint32 Run() override;
  8.         virtual voidStop() override;
  9.         virtual voidExit() override;
  10.         FSocket* socket;
  11.         bool SocketCreate(FString IPStr, int32 port);//int byte2 Buffer();
  12.         FIPv4Address ip;~AClient();
  13.         TQueue<int32> messages;
  14.         FRunnableThread* Thread = nullptr;
  15.         int32 stop = false;};
复制代码
  1. // Fill out your copyright notice in the Description page of Project Settings.#include"Client.h"
  2. AClient::AClient(){
  3.         this->SocketCreate("127.0.0.1",9201);
  4.         Thread = FRunnableThread::Create(this,TEXT("This is my thread example"));
  5.         stop =1;}
  6. bool AClient::Init(){
  7.         stop =1;return true;}
  8. uint32 AClient::Run(){
  9.         TArray<uint8> buffer;
  10.         uint8 element =0;while(stop ==1){
  11.                 int32 len =0;
  12.                 buffer.Init(element,1024);
  13.                 socket->Recv(buffer.GetData(),buffer.Num(),len);auto val =FString(ANSI_TO_TCHAR(reinterpret_cast<constchar*>(buffer.GetData())));UE_LOG(LogTemp, Warning,TEXT("server info %s,%d,%d"),*val,len,stop)
  14.                 FPlatformProcess::Sleep(0.1);}UE_LOG(LogTemp, Warning,TEXT("thread end..."));return1;}void AClient::Stop(){
  15.         stop =0;
  16.         socket->Close();}void AClient::Exit(){}
  17. bool AClient::SocketCreate(FString IPStr, int32 port){
  18.         FIPv4Address::Parse(IPStr, ip);
  19.         TSharedPtr<FInternetAddr> addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
  20.         addr->SetIp(ip.Value);
  21.         addr->SetPort(port);
  22.         socket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream,TEXT("default"), false);if(socket->Connect(*addr)){UE_LOG(LogTemp, Warning,TEXT("Connect Succeed!"));return true;}else{UE_LOG(LogTemp, Warning,TEXT("Connect Failed!"));return false;}}
  23. AClient::~AClient(){UE_LOG(LogTemp,Warning,TEXT("Client Destruct"))
  24.         stop =0;}
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-11-23 08:35 , Processed in 0.089093 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表