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?
asked Mar 28 '14 at 04:22 AM in C++ Programming
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.
answered Mar 28 '14 at 03:18 PM
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.
answered Mar 28 '14 at 03:23 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here