Connecting to Socket Server

I’ve done what reading I can in the documentation so far, and I believe I’m on the right track but perhaps someone can correct me.

I’m attempting to connect to a custom socket server rather than a dedicated server run by players.

From my understanding doing this would involve FSocket, ISocketSubsystem, and FInternetAddr.

So far I’ve attempted to compile the following.

FSocket *socket = ISocketSubsystem::Get()->CreateSocket(“default”, “default”, false);

Now I understand why this isn’t working, because ISocketSubsystem is an interface. What class implements this interface? How should I be using these classes? Has anyone done anything like this yet, or attempted to?

ISocketSubsystem::Get() returns a singleton for the running platform’s socket subsystem. It is this singleton that implements the ISocketSubsystem interface, which is why your example code works. You will likely stumble upon this pattern in other places as well.

In the Networking module there are two helper classes, FTcpSocketBuilder and FTcpListener, for TCP clients and servers respectively. Please note that this module is currently not documented, and the API may change in the future, so use it with care. At the very least, it will provide you with some examples on how to create sockets in UE4. Look at FTcpSocketBuilder::Build() to see what it is doing. The module also contains helper classes for UDP sockets.

In particular, note that, after creating a socket you will also have to bind to a port and possibly perform other initialization steps in order to establish a connection to your server.

You’re better off creating it that way (assuming TCP):

FSocket *socket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSSTEM)->CreateSocket(NAME_Stream, TEXT(“My server socket”), false);

There’s a number of socket subsystem in the engine and more than one can be present at a time, so providing PLATFORM_SOCKETSUBSYSTEM is safer.

Nevermind, builder approach suggest by Max is cleaner, however you can still use that code if you need to know the guts.

When I use ISocketSubsystem::Get() I get a linker error.

Looking at the FTcpSocketBuilder class I have a few questions you can hopefully answer.

  • Do I need to define the BoundToAddress, and BoundToEndPoint?
  • Does the port it’s bound to on the client need to match the port the server is using?
  • Is there anything that absolutely needs to be defined in that class before building the socket?

Solved my linker error, missing “Sockets” in my dependency module.

Could you tell me what you mean by dependency module?

i’m getting linker errors with the ISocketSubSystem…

In PROJECTNAME.Build.cs you need to add “Sockets” to PublicDependencyModuleNames

valla helal lan