Third Party Socket Server Connection
We’ve been quietly working on our new game here at OSnap! Games and 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.
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
- Type
- Description
- 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. Epic provides the tools to get this done quickly.
FIPv4Address ip(127, 0, 0, 1); TSharedRef<FInternetAddr> addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr(); addr->SetIp(ip.Value); addr->SetPort(7776);
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" } );
Reposted from http://www.osnapgames.com/2014/05/24/connecting-to-a-third-party-socket-server-in-unreal-engine-4/