Pages Menu
TwitterFacebook
Categories Menu

Posted by on May 24, 2014

Connecting to a Third Party Socket Server in Unreal Engine 4

Connecting to a Third Party Socket Server in Unreal Engine 4

 
We’ve been quietly working on our new game here at OSnap! Games and we’ve selected to build it using Unreal Engine 4.

One of the features our new game will include is a lobby system where all players can chat among one another. The UE4 dedicated server is great for a game server but it’s overkill for something as simple as a chat server so we set out to get UE4 connecting to a third party socket server. Luckily the developers at Epic have provided the tools to do just this!

 

Creating the Socket

The first step in getting UE4 connected to a third party server is initializing a socket.

 

FSocket* Socket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream, TEXT("default"), false);

 

The parameters are as follows

  1. Type
  2. Description
  3. ForceUDP

In our case we’re connecting using TCP so we’ve set ForceUDP to false.

 

Preparing the Address

The next part is getting the address you wish to connect to ready. Again, Epic provides the tools to get this done quickly.


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);

 

Now that your address is ready to go you simply have to tell UE4 to start the connection.

 

bool connected = Socket->Connect(*addr);

If connected is true you’ve successfully connected to your socket server!

 

Sending a Message

Alright, so you’re connected but how do you send messages to it? A lot of that is up to you. Depending on your server there are different ways to serialize and send a message. The important part though is how does UE4 handle it.

First things first, we must prepare the message to be sent.


FString serialized = TEXT("loadPlayer|1");
TCHAR *serializedChar = serialized.GetCharArray().GetData();
int32 size = FCString::Strlen(serializedChar);
int32 sent = 0;

 

What’s going on in the above? We’re sending a message to a socket server with the type “loadPlayer” and a single parameter of 1. What is happening in UE4 here is we’re taking an FString, and turning it into a TCHAR*.

Finally, with the message formatted we can send it to our server!

 

bool successful = Socket->Send((uint8*)TCHAR_TO_UTF8(serializedChar), size, sent);

 

Sending data in UE4 requires it to be a uint8* and again, Epic provides the tools to get from TCHAR* to UTF8 which can be sent as a uint8*.

As far as reading data back from your socket server the key is in the functions HasPendingData and Recv of the FSocket class.

 

Dependency in Build.cs

One final thing to take note of. Inside your project’s Build.cs file you’ll need to add the “Sockets” package as a dependency in PublicDependencyModuleNames.

 


PublicDependencyModuleNames.AddRange(
   new string[]
   {
      "Core",
      "CoreUObject",
      "Engine",
      "Sockets"
   }
);