TCP Socket Listener, Receiving Binary Data into UE4 From a Python Script!

My bad, I’m still quite new at using UE4.

Basically I have created a new blank c++ project, added in your code as a new PC. It complies fine.

I’ve followed your instructions to add the PC to the world settings via a custom game node but when I run the project nothing happens.

I should add that my python code is running in the background, I’ve tested my code with another python programme so I know my orginial python code is working :slight_smile:

Then you should try to find out why it is not working, starting from “is everything getting called as i expect it to” by printing to the log or on-screen-debug messages. Is you python code registering another socket trying to connect? If not, your c++ code is most likely not getting called. If your code connects, but sending/receiving messages does not work, the problem would be a completely other direction. We can’t help you without knowing more precisely what is working and what not :wink:

hi , there
i m very new to c++ programming
i was trying to implement 's code inside a new project so that i can play my game from an andriod device
but getting error for
#include <String>

Info ERROR: The first include statement in source file ‘…\EDU_Projects\Car_CPP\Source\Car_CPP\Car_CPPPawn.cpp’ is trying to include the file ‘String’ as the precompiled header, but that file could not be located in any of the module’s include search paths.

i tried this but didn’t help :frowning:
& i couldn’t understand this one

can u suggest something
thanks in advance

Dear ,

As always you are a great aid to me learning unreal. I followed your tutorial and I managed to connect and send data. For a small while, that is. After about 30-45 seconds the FSocket pointer changes value to 0xdddddddddddd for some reason. I have no idea why. It does not matter what I do to it, it is always the same amount of time. No matter how much I send to or read from the socket, the time is the same. And it is almost always 0xdddddddddddddd. A few times it was 0xdcdcdcdcdcdcdcdcdc, but i have been unable to reproduce it.

Do you have any idea what this could be caused by?

I made the class that controls the server, ANetwork, to inherit from the AGameNetworkManager class. My Custom PlayerController class used by a custom gameMode class controlls the server via function calls to the ANetwork class.

If you want I can send you the source code, but it is pretty much a copy paste of your code.

Hi there!

This sounds like something is getting garbage collected, but I am not sure what! Do you have any UObject* involved in your project that are not UPROPERTY() that might be getting GC’ed ?

Wiki on UE4 Garbage Collection

Other than Garbage Collection I am not aware of anything else that would have such a consistent time out.

I have several clients that use my TCP code, for hours at a time, without issue so there must be something going on in your project that is causing this strange timeout.

It sounds like your ANetwork class is disappearing for some reason, and it contains the FSocket ptr, can you try running all your code from the player controller entirely?

Move all ANetwork code into player controller as a test?

That would help narrow down the issue, as there’s no way the PC would get GC’ed somehow or disappear :slight_smile:

Elimate the use of an instanced actor ANetwork (and use only PC) and let me know how it goes!

This is just to test the matter, not saying it is a final solution :slight_smile:

Good luck!


Hi ,

thank you for sharing your code.
But I have a few problems. After adding a new c++ class via unreal engine i added your code to the .h and .cpp file but i keep getting the following errors:

1>C:\Users\Max\Documents\Unreal Projects\BlueSocketTest\Source\BlueSocketTest\BlueRamaSocket.h(6): fatal error C1083: Cannot open include file: ‘Networking.h’: No such file or directory
1>c:\users\max\documents\unreal projects\bluesockettest\source\bluesockettest\BlueRamaSocket.h(6): fatal error C1083: Cannot open include file: ‘Networking.h’: No such file or directory
1> -------- End Detailed Actions Stats -----------------------------------------------------------
1>ERROR : UBT error : Failed to produce item: C:\Users\Max\Documents\Unreal Projects\BlueSocketTest\Binaries\Win64\UE4Editor-BlueSocketTest-3819.dll

Could you tell me why i have this problem with the include of the networking.h file? please let me know if you need any further information.

I think you forgot to include networking module in your Build.cs
Check the wiki article from the first post

Thank you for that point, but I added netwokting and sockets to my build.cs file as shown in the wiki article.

Are you using Intellisense? Mine keep accusing missing includes when they aren’t inside the project file (like the Networking.h that is inside Engine folder). It stopped to happen when I changed to Visual Assist…

Guys, I’m trying to reproduce the code, but I’m having trouble building it… I’m getting the following errors:


It’s a clean project (I’ve created just to test the code). Unreal 4.9 and I’m using a Player Controller class. Any ideas?

edit: Ok, found the issue:
SetTimer usage changed since 4.7 4.7 C++ Transition Guide - C++ - Unreal Engine Forums
ParseIntoArray parameters changed since 4.8:

Did anyone actually tried this with an IP address other than localhost? Anything different from causes the application to crash here… When debugging, the UEditor triggers the first breakpoint after crashing here:



That’s odd… I’ve also tried to input the IP directly via FIPv4Address parameters to exclude any issue with the IP address conversion, but the issue persists…


I’m having a Engine Crash issue with the function CreateTCPConnectionListener.

FSocket* ATestActor::CreateTCPConnectionListener(const FString& YourChosenSocketName, const FString& TheIP, const int32 ThePort, const int32 ReceiveBufferSize)
	uint8 IP4Nums[4];
	if (!FormatIP4ToNumber(TheIP, IP4Nums))
		return false;


	//Create Socket
	FIPv4Endpoint Endpoint(FIPv4Address(IP4Nums[0], IP4Nums[1], IP4Nums[2], IP4Nums[3]), ThePort);
	FSocket* ListenSocket = FTcpSocketBuilder(*YourChosenSocketName).AsReusable().BoundToEndpoint(Endpoint).Listening(8);

	//Set Buffer Size
	int32 NewSize = 0;
	/*---------------> WHERE IT POINTS WHEN CRASHES*/  ListenSocket->SetReceiveBufferSize(ReceiveBufferSize, NewSize);

	return ListenSocket;

I’m using a .jar file to send information to the local host( I’m using 4.9… Any help on what’s happening would be excellent :(.

Got the output from the crash… looks like it failed to create the socket

[2015.09.15-04.10.42:416][150]LogWorld: Game class is 'derpGameMode'
[2015.09.15-04.10.42:425][150]LogWorld: Bringing World /Game/ThirdPersonCPP/Maps/UEDPIE_0_ThirdPersonExampleMap.ThirdPersonExampleMap up for play (max tick rate 0) at 2015.09.14-23.10.42
[2015.09.15-04.10.42:426][150]LogActor:Warning: GameSession /Game/ThirdPersonCPP/Maps/UEDPIE_0_ThirdPersonExampleMap.ThirdPersonExampleMap:PersistentLevel.GameSession_0 has natively added scene component(s), but none of them were set as the actor's RootComponent - picking one arbitrarily
[2015.09.15-04.10.42:426][150]LogActor:Warning: GameNetworkManager /Game/ThirdPersonCPP/Maps/UEDPIE_0_ThirdPersonExampleMap.ThirdPersonExampleMap:PersistentLevel.GameNetworkManager_0 has natively added scene component(s), but none of them were set as the actor's RootComponent - picking one arbitrarily
[2015.09.15-04.10.42:427][150]LogWorld: Bringing up level for play took: 0.009418
[2015.09.15-04.10.42:429][150]FTcpSocketBuilder: Failed to create the socket RamaSocketListener as configured

Blaah Dx… Any help would be great :(.

that crash is usually a “secondary crash” when it’s already at critical error and trying to shut everything down. :o
Unless you actually are cleaning up the 2d phys environment


Ok soo I managed to make it not crash.

Now for the logic part:

//'s TCP Connection Listener
void ANetworkActor::TCPConnectionListener()

	if (!ListenerSocket)
    //Remote address
    TSharedRef<FInternetAddr> RemoteAddress = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
    bool Pending;

    // handle incoming connections
    if (ListenerSocket->HasPendingConnection(Pending) && Pending)
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "DERP");    //DOES NOT SHOOT MESSAGE ON SCREEN

		//Already have a Connection? destroy previous
		if (ConnectionSocket)

		//New Connection receive!
		ConnectionSocket = ListenerSocket->Accept(*RemoteAddress, TEXT("RamaTCP Received Socket Connection"));

		if (ConnectionSocket != NULL)
			//Global cache of current Remote Address
			RemoteAddressForConnection = FIPv4Endpoint(RemoteAddress);

			//UE_LOG "Accepted Connection! WOOOHOOOO!!!";

			//can thread this too
			GetWorldTimerManager().SetTimer(Timer02, this, &ANetworkActor::TCPSocketListener, 0.01, true);

Now it seems to be that there is no pending connections to the IP Port. I’m trying to figure this out in particular, but it just wont budge :(. I also removed the check for Pending in the if statement

if (ListenerSocket->HasPendingConnection(Pending))

and it outputs my Derp. however, when i get to this:

		if (ConnectionSocket != NULL)
                        GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, "DERP");

			//Global cache of current Remote Address
			RemoteAddressForConnection = FIPv4Endpoint(RemoteAddress);

			//UE_LOG "Accepted Connection! WOOOHOOOO!!!";

			//can thread this too
			GetWorldTimerManager().SetTimer(Timer02, this, &ANetworkActor::TCPSocketListener, 0.01, true);

The derp message wont output as well.

One of two things is happening that I do not know:

1.) Do I need to find a way to open the IP Port? because it seems to me there’s a wall of some sort blocking it’s way to getting it?

2.) Trying to figure out why the ConnectionSocket is NULL after we initialize it?

Any help would be amazing :(.
1-800-123-HELP O_o

UPDATE: One last thing discovered:

So I’m back to the issue with the Engine Crashing and pointing to the ListenSocket

I’ve discovered that in order to make this work, I had to do the following:

1.) Start the engine
2.) Run the command line to open the .jar file to send info to the IP Port
3.) We have communication


It’s causing the crash at that spot. Is there a buffer size issue maybe? What do I do about this ? :frowning:

*bump Dx. Anything to lead me in the right direction :frowning:

Is there anyone who got this working on 4.9?
Im following the tutorial but I’m not able to import Networking.h in the firsplace.
When I’m using the full location to import the headerfile the compiler errors get too much to count.
Please if someone can help me out…

Thanks in advance.


I got it working in 4.9 But i’m running into another problem. It only works for one time after opening the project. I think the problem lies within not closing the socket. Then again I’m not really a programmer so my idea could be way off. Maybe someone could point me in the right direction how to close the socket before exiting.

Hi I’m still fighting with the code… but got most of it working, you have to:
->Close the socket
Then With IsocketSystem destroy it

you can do this on the BeginDestroy event

Good luck