How to connect to Socket?

I work for socket communication test with UE 4.2.1.

so, i used FSocket, reference The tutorial post about FSocket in Wiki.

The post is https://wiki.unrealengine.com/Third_Party_Socket_Server_Connection.

but, the post is wrong as i think.

First, ISocketSubSystem::Get() is not work. not be shown Code Hint in VS. Of course not be built.

maybe, is there alternative way for UE 4.2.1?

You may want to thread the socket code eventualy so I am sharing the threaded code.

SocketTask.h



class X_API SocketTask : public FRunnable
{
	FRunnableThread* RunnableThread;
	FSocket* Socket;
	TSharedPtr<FInternetAddr> InternetAddress;
	FString Address;
	int32 Port;


public:

	SocketTask(const FString& InAddress, int32 InPort);
	~SocketTask();


public: // FRunnable Interface

	virtual bool Init() override;
	virtual uint32 Run() override;
	virtual void Stop() override;


private:

	/** Processes the socket message */
	void ProcessSocketMessage(uint8 InSocketMessage[16]);
};


SocketTask.cpp



#include "X.h"
#include "SocketTask.h"
#include "Networking.h"

SocketTask::SocketTask(const FString& InAddress, int32 InPort)
{
	Address = InAddress;
	Port = InPort;

	Socket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream, InAddress, false);

	FIPv4Address ip;
	FIPv4Address::Parse(InAddress, ip);

	InternetAddress = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
	InternetAddress->SetIp(ip.GetValue());
	InternetAddress->SetPort(InPort);

	RunnableThread = FRunnableThread::Create(this, TEXT("ControllerDeviceTask"), 0, TPri_BelowNormal);
}

SocketTask::~SocketTask()
{
	delete Socket;
	Socket = nullptr;

	delete RunnableThread;
	RunnableThread = nullptr;
}

bool SocketTask::Init()
{
	return true;
}

uint32 SocketTask::Run()
{
	if (Socket != nullptr)
	{
		if (Socket->Connect(*InternetAddress) == false)
		{
			return 1;
		}

		// Continue updating the device while possible...
		while (Socket != nullptr && Socket->GetConnectionState() == ESocketConnectionState::SCS_Connected)
		{
			int32 sent = 0;

			uint8 Request] = { 250, 3, 3 };
			if (Socket->Send(Request, 3, sent))
			{
				int32 BufferSize = 16;
				int32 BytesRead = 0;
				uint8 Response[16];

				if (Socket->Recv(Response, BufferSize, BytesRead))
				{
					ProcessSocketMessage(Response);
				}
				// else?
			}
			// else?

			// Sleep to reduce usage of system resources(nearly delta time).
			FPlatformProcess::Sleep(0.03F);
		}
	}

	return 0;
}

void SocketTask::Stop()
{
	// This will make the socket connection state different then SCS_Connected and
	// It will break the while loop in Run method.
	Socket->Close();
}

void SocketTask::ProcessSocketMessage(uint8 InSocketMessage[16])
{
	// Your code here...
}



In this example, my server (which is a device connected to ethernet) only responses to 3 bytes valued {250, 3, 3} and the response messages are words (2 bytes/16 bits). The examples works great, I tested it in 4.8, 4.9 and 4.10 versions.

I’ve used this task in my GameInstance class. In the Init method of the class I’ve created an instance of the task then Run immedieately.

MyGameInstance.cpp



Task = new SocketTask("192.168.1.100", 1000);
Task->Run();


Don’t forget to add “Sockets” and “Networking” module names in Build.cs
Also note that, threading is not a must but specially connecting to it take realy long time about 2 seconds.