RiptideNetworking / Riptide

Lightweight C# networking solution for multiplayer games.
https://riptide.tomweiland.net
MIT License
1.14k stars 144 forks source link

Server not listening? #161

Open AlaskanDruid opened 6 days ago

AlaskanDruid commented 6 days ago

I may be feeling kind of stupid right now, but I cannot find a github discussions tab for this project :/ Anyhow, I made a sample program. Normally, I would put this as a new discussion in the discussion tab to brainstorm with the community to confirm its a bug/issue before reporting it as such :(

As a C# console app (no unity), the below code will not accept any connections at all. This is as barebones as I can make it. Tried connecting with various clients via 127.0.0.1 (port 7778) no go for that port nor any other ports. Just keep getting connection refused. Firewall is completely off (on Windows 11). I am starting to think maybe the newest windows 11 patch disabled udp (again, this happened before in a 2022 patch). However, I wanted to double check ...

(I should mention the firewall is completely turned off)


using Riptide;
using Riptide.Utils;

namespace ServerServer
{
    internal class Program
    {
        static void Main(string[] args)
        {
            RiptideLogger.Initialize(Console.WriteLine, true);

            var server = new Server();

            server.ConnectionFailed += Server_ConnectionFailed;
            server.ClientConnected += Server_ClientConnected;
            server.ClientDisconnected += Server_ClientDisconnected;

            server.Start(7778, 100);

            Console.WriteLine("Press ESC to shutdown.");
            Console.WriteLine("");

            do
            {

                server.Update();

                while (!Console.KeyAvailable)
                {
                    Thread.Sleep(1);
                }
            } while (Console.ReadKey(true).Key != ConsoleKey.Escape);

            server.Stop();

            server.ClientConnected -= Server_ClientConnected;
            server.ClientDisconnected -= Server_ClientDisconnected;
            server.ConnectionFailed -= Server_ConnectionFailed;

        }

        private static void Server_ClientDisconnected(object? sender, ServerDisconnectedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private static void Server_ClientConnected(object? sender, ServerConnectedEventArgs e)
        {
            throw new NotImplementedException();
        }

        private static void Server_ConnectionFailed(object? sender, ServerConnectionFailedEventArgs e)
        {
            throw new NotImplementedException();
        }

    }
}
CrossV4 commented 6 days ago

It happens because server.Update(); is calling just one time. Your nested while loop, blocks and breaks do-while loop. You can try this here

while (!Console.KeyAvailable) { Thread.Sleep(1); } remove this block and your server will update and listen your clients.

Also you can check The demo version for Console

AlaskanDruid commented 6 days ago

Huh, was thinking my brain was fried last night, however, even with the changes above, same issue, server keeps refusing connection. Im starting to think it is another udp blocking win11 patch. ugh. Thanks anyhow, looks like I need to look at backing out OS patches to find out which one is the culpit.

CrossV4 commented 6 days ago

i didnt think it is about from win11. I am using win11 and my all servers runs as expected. As i see you throwing an exception for not written code yet.

private static void Server_ConnectionFailed(object? sender, ServerConnectionFailedEventArgs e) { throw new NotImplementedException(); }

so when a client try to connect your server gets an exception and it can maybe terminate the whole thread. So remove throw new NotImplementedException(); then retry. Also do not forget the add Thread.Sleep(xx); after the server.Update();.

AlaskanDruid commented 6 days ago

Unfortunately, that isn't it. What version/patch level of Windows 11 do you have? I have ..

Version 23H2 (OS Build: 22631.4460)

I'm still hunting down patches.


using Riptide;
using Riptide.Utils;

namespace ServerServer
{
    internal class Program
    {
        static void Main(string[] args)
        {
            RiptideLogger.Initialize(Console.WriteLine, true);

            var server = new Server();

            server.ConnectionFailed += Server_ConnectionFailed;
            server.ClientConnected += Server_ClientConnected;
            server.ClientDisconnected += Server_ClientDisconnected;

            server.Start(7778, 100);

            Console.WriteLine("Press ESC to shutdown.");
            Console.WriteLine("");

            do
            {
                server.Update();
                Thread.Sleep(10);

            } while (Console.ReadKey(true).Key != ConsoleKey.Escape);

            server.Stop();

            server.ClientConnected -= Server_ClientConnected;
            server.ClientDisconnected -= Server_ClientDisconnected;
            server.ConnectionFailed -= Server_ConnectionFailed;

        }

        private static void Server_ClientDisconnected(object? sender, ServerDisconnectedEventArgs e)
        {
            Console.WriteLine("Server_ClientDisconnected");
        }

        private static void Server_ClientConnected(object? sender, ServerConnectedEventArgs e)
        {
            Console.WriteLine("Server_ClientConnected");
        }

        private static void Server_ConnectionFailed(object? sender, ServerConnectionFailedEventArgs e)
        {
            Console.WriteLine("Server_ConnectionFailed");
        }

    }
}

Please note, I had to change the above loop to below as the above loop only runs once.


            do
            {
                while (!Console.KeyAvailable)
                {
                    server.Update();
                    Thread.Sleep(10);
                    //Console.WriteLine("test");

                }
            } while (Console.ReadKey(true).Key != ConsoleKey.Escape);

OH! I am on .Net 9 instead of 8. That might be the difference there (some new bug with .Net 9 and this NuGet package (2.2.0).

CrossV4 commented 6 days ago

hmm, no you still getting wrong idea. As i said in my first reply, your loop block server.Update(); is runs just one time. I tested your while (Console.ReadKey(true).Key != ConsoleKey.Escape); condition, and i see that your condition never return false or true until user give an input value. So thats way your while block cannot run server.Update(); block.

So you should use a dynamic boolean value instead of thread input functions. I.e bool isrunning = true; while(isrunning).

Then check the users input from another thread because a thread is responsible from running this your while loop. So create a thread, and put this while loop into it and in Main thread check and get users input or do whatever you want.

If you cant manage it to do check this demo from riptide api owner

Per6 commented 6 days ago

Yeah, Console.ReadKey() blocks the Thread, so you need to create a seperate Thread where you don't care if it get's blocked.