C# NetworkTables client not connecting to robot code

I am working on a C# client which communicates with the robot using networktables (with help from this nuget package.) Everything appears to not throw any errors, but there are no roborio logs indicating a new NT connection (like with shuffleboard) and NetworkTable.IsConnected is always false. I’ve tried numerous different configurations to no avail. If it helps, the robot I’m trying to connect to is simulated using halsim, and my client is actually a program built in Unity.

Here’s the general code i’m using:


NetworkTable.SetClientMode();
//4500 is my team number in both driver station & my project
NetworkTable.SetTeam(4500); 
NetworkTable.SetIPAddress("127.0.0.1"); //have tried with & without this line
NetworkTable.SetDSClientEnabled(true); //have tried with & without this line
NetworkTable.Initialize();
var table = NetworkTable.GetTable("LiveWindow");
System.WriteLine(table.IsConnected); //always outputs //false

Does it work in a standard c# program? I know I had tried unity in the past, and it didn’t work. It uses functionality that just doesn’t exist and work in unity.

I just tried using it in a .NET Core 3.0 program, it didn’t work unfortunately; I got the same results.

However, I’ve actually gotten this working before, in a unity program. All it uses are the methods NetworkTable.SetClientMode and NetworkTable.SetTeam. I can see new entries being written to from shuffleboard. This was in Unity 2018, with .NET framework 4.x enabled. I’m trying to repeat this result in Unity 2019, mostly because I enjoy keeping the latest software. I have no idea what is going on right now.

Hmm, I wonder if some of the runtime stuff Unity uses, and some of the changes in .NET Core 3 caused this. The FRC.NetworkTables version you are using was only ever really tested with full .NET Framework 4.x. It had a .NET Standard 1.3 build, but if I remember correctly, it was buggy in Core. I stopped writing the managed one completely because of maintenance issues, and switched completely back to wrappers around ntcore for simplicity sake, so I haven’t even looked at that code base or touched it for over 2 years. I wouldn’t be shocked if an update broke it, it was a wonky setup to begin with and I didn’t really know much about networking at the time to get it right.

I can try to dig up the source code for that again, but its unlikely I would be able to fix it unless its an extremely simple bug.

I wonder if its something with your simulation setup. I just tested with a default simulation setup for the robot, and the following code for a client in .NET Core 3.0, and it worked.

            NetworkTable.SetClientMode();
            NetworkTable.SetTeam(9999);
            NetworkTable.SetIPAddress("127.0.0.1");
            NetworkTable.SetDSClientEnabled(true);
            NetworkTable.Initialize();

            while (true)
            {
                Console.WriteLine(NetworkTable.Connections().Count);
                Thread.Sleep(500);
            }

So it definitely can connect to 2019 and 2020 robot projects. I wonder if your firewall is doing something weird on your local system disallowing localhost connections.

Huh, that’s weird. I just created a new WPILib project with simulation enabled, started the simulation, then ran my client program. Nothing changed. I did it again, but with private and public network firewalls turned off for good measure, and again, nothing changed.

I also tried copying the exact DLLs from the working unity project to the new unity project, with no change in result.

I do have an error that says “‘chcp.com’ is not recognized as an internal or external command,
operable program or batch file.” I don’t know if this has anything to do with my problem though.

(sidenote: I stopped using a custom driversocket dll so the drive station stopped working. Is that not a part of the default configuration yet?)

Also, I’ve used ASP.NET and followed a tutorial to set up a sample website hosted on localhost. I’ve connected to it and it works so it seems weird that it would deny certain connections.

I have not tested anything with C#, but the first time I tried this with Python, I missed the fact that the client can take a while to connect to the server. You might want to try putting the connect in a loop with a sleep time and see if it connects after a while.

Oh, yeah I didn’t even notice that. It usually takes about a second to connect after calling initialize. My example prints out 0 a few times before finally connecting and then printing 1.

Oh come on, I wasn’t waiting for long enough. Both the unity program and my netcore 3.0 program connect after a few seconds (although I do need to specify to connect to localhost, otherwise it logs errors saying that it times out while connecting to certain IP addresses.)

Thanks @prensing and @Thad_House for all the help!