What Includes are needed to set up Networking and Sockets?

After following tutorials to set up a connection to a tcp socket server, I have added the following code to my Player Controller class:

MyPlayerController.cpp:

void AMyPlayerController::P2Con()
{
	FSocket* Socket = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateSocket(NAME_Stream, TEXT("default"), false);
	 
	int32 port = 19834;
	FIPv4Address ip;
	ip = FIPv4Address(127, 0, 0, 1);


	TSharedRef addr = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
	addr->SetIp(ip.GetValue());
	addr->SetPort(port);
}

Project2.Build.cs:

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

but things like ISocketSubsystem and FIPv4Address were either undefined or incomplete types. I figured this means I needed to include their header files, so I used documentation to obtain their header file locations and added the includes within MyPlayerController.h as follows:

MyPlayerController.h includes (not including the auto-generated)

#include "Runtime/Networking/Public/Interfaces/IPv4/IPv4Address.h"
#include "Runtime/Sockets/Public/SocketSubsystem.h"
#include "Runtime/Core/Public/Templates/SharedPointer.h"

but including these files causes even more errors. I have tried just including Networking.h, and Sockets.h within their respective directories and it still has issues. I guess my question would be, what includes are needed to compile the above code, or is there an online resource which discusses including from modules such as Networking or Sockets? I’m very new to UE4 so I apologize if this question is trivial, but I’ve been stuck on this for hours so any help would be greatly appreciated. Thanks for reading!

FIPv4Address is defined in IPv4Address.h, which is included from Networking.h, but you may find it easier to use FInternetAddr exclusively. You can conveniently set the IP address with the SetIp function that takes a string as a parameter and avoid the dependency on FIPv4Address entirely. You may need to add an #include for IPAddress.h, which defines FInternetAddr.

If you’re still getting errors, please post a specific error so we can better diagnose it.

Also, you should be able to use just the filename in your #include paths. The full path (starting at the Runtime directory) should be unnecessary if the corresponding module is declared as a dependency, which in your case it appears to be.

You need the following includes:

#include "Networking.h"
#include "Sockets.h"
#include "SocketSubsystem.h"

The Socket API is a little difficult to use right now, but since you’re already using it for most of your code anyway, you probably don’t need the Networking module at all. The FinternetAddr class as a SetIp() overload that takes a string. Then you don’t need the FIPv4Address.

If you’re still getting compile errors, please post the errors.

After attempting to include header files

#include "Networking.h"
#include "Sockets.h"
#include "IPAddress.h"

Cannot open source file "Sockets.h"
Cannot open source file "Networking.h"
Cannot open source file "IPAddress.h"
PLATFORM_SOCKETSUBSYSTEM is undefined
ISocketSubsystem: Incomplete type is not allowed
FInternetAddr: Incomplete type is not allowed
Argument list for class TSharedRef is missing

Brief overview of the errors I’m getting, it appears that the dependancies are not being loaded, is there more to adding a dependancy than simply inserting a string inside the Project2.Build.cs file?

No, that should be all that is needed. Do you actually have the full source code (or at least the header files) for the Engine? Sockets.h should be located in /Engine/Source/Runtime/Sockets/Public/

Trying to just include without the directory does not work, including with the directory seems to load the headers, however references inside those files do not load their corresponding headers. i.e. Networking.h: cannot open source file “UdpSocketSender.h”, “Sockets.h”, “TcpListener.h” etc…

#include "Runtime/Networking/Public/Networking.h"
#include "Runtime/Sockets/Public/Sockets.h"
#include "Runtime/Sockets/Public/SocketSubsystem.h"

causes these errors: 37 IntelliSense: invalid specifier outside a class declaration c:\Program Files - Pastebin.com

so to sum it up, I have access to the members of the referenced classes now, but within those headers it cannot find any other referenced headers i.e. “TcpListener.h” inside Runtime/Networking/Public/Networking.h

This sounds like a bug. Using 4.2 and a new “Basic Code” project, I was able to #include "SocketSubsystem.h" without errors after adding “Sockets” to the list of PublicDependencyModuleNames. Can you reproduce this problem with a new project? Are you using 4.2?

I am using 4.2. Also on a new Basic Code project I am still getting a lot of errors, with the above code and the suggested includes (see below). I was hoping it was only an issue with Intellisense but it seems that is not the case. I could go into the MyProject3->Settings->NMake and add each and every library path to the Include Search Path list, but I’m not sure this will do anything except make intellisense less angry.

Errors on compile for new Basic Code project with socket code:

I will also state that without the posted code added to it, it does compile. Also it will compile with an include to “SocketSubsystem.h” but will display an intellisense error stating that it could not open the source file SocketSubsystem.h.

P.S. I did add Sockets and Networking to the dependancies in MyProject3.Build.cs before trying to compile*

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

It’s unnecessary to add the library paths in the NMake settings. I understand it’s annoying, but let’s get you compiling before worrying about Intellisense.

If you use ISocketSubsystem::Get you will need to #include "SocketSubsystem.h", that’s where the function is declared.

Looking at your other errors and the code in your original post, you are actually missing the template parameter on TSharedRef. CreateInternetAddr() returns a TSharedRef< FInternetAddr >. You can also specify auto to let the compiler deduce the type.

Ahh it compiles now Ryan, thanks! Only problem now is the intellisense errors.

This actually worked, @gmpreussner. Thanks.

@gmpreussner, I have the same addition to my build.cs file as the question, but Visual Studio tells me that it can’t find “Networking.h”. Any ideas why that might be?

PrivateDependencyModuleNames.AddRange(
new string[] {
“Networking”,
“Sockets”,
}
);

Or PublicDependencyModuleNames, depending on whether or not your module is exporting those types.