Hi, from looking at Ramas tutorial Rama<3 I’ve attempted to implement a listen server (that will do light work in the b.ground).
UServer::UServer()
{
ReceiveBufferSize = 2 * 1024 * 1024;
}
void UServer::StartListening(UWorld* World)
{
const FString SocketName = FString(TEXT("Socket"));
const FIPv4Endpoint EndPoint = FIPv4Endpoint(FIPv4Address::InternalLoopback, 12345);
Socket = FTcpSocketBuilder(SocketName).AsReusable().Listening(8).BoundToEndpoint(EndPoint);
int32 NewSize = 0;
Socket->SetReceiveBufferSize(ReceiveBufferSize, NewSize);
//GetOuter()->GetWorld()->GetTimerManager().SetTimer(listenHandle,this, &UServer::Listerner, 0.01, true);
if (World)
World->GetTimerManager().SetTimer(listenHandle,this, &UServer::Listerner, 2, true);
}
void UServer::Listerner()
{
if (!Socket) return;
//~~~~~~~~~~~~~
//Remote address
TSharedRef<FInternetAddr> RemoteAddress = ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->CreateInternetAddr();
bool Pending;
// handle incoming connections
if (Socket->HasPendingConnection(Pending) && Pending)
{
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Already have a Connection? destroy previous
if (ConnectionSocket)
{
ConnectionSocket->Close();
ISocketSubsystem::Get(PLATFORM_SOCKETSUBSYSTEM)->DestroySocket(ConnectionSocket);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//New Connection receive!
ConnectionSocket = Socket->Accept(*RemoteAddress, TEXT("RamaTCP Received Socket Connection"));
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "debug msg");
if (ConnectionSocket != NULL)
{
//Global cache of current Remote Address
RemoteAddressForConnection = FIPv4Endpoint(RemoteAddress);
//UE_LOG "Accepted Connection! WOOOHOOOO!!!";
//can thread this too
}
}
}
Before executing the timer I tired
using the Listen method which did nothing.
In my test C# client program I attempt to connect, but no connection is made.
TcpClient c = new TcpClient();
c.Connect(IPAddress.Loopback, 12345);
Console.ReadLine();
Am I missing something, surely Socket->Accept()
is a blocking method, that stops the flow of code, untill a client connects, right?
I made sure to open ports 12345 in my fire wall as well.
Does anyone know why my dummby client cannot connect to the server?