August 30, 2024

C# on linux -- Socket IO - using Visual Studio Code

I am only just on my first day with VS-code. I have a simple hello world project that I can build. I am buying into Dunning-Kruger and figuring I am an expert now, with 2 hours of experience with VS-code and about a week with C#.

My idea here is actually two projects, but one will easily become the other. The idea is to do some trivial TCP socket client programming. My first goal will be to talk to the infamous port 13 daytime server. The second is to send a message and ring my workshop bell.

Start the daytime server on port 13

The very first thing is to get the daytime server running on port 13. It seems like I have done this countless times, and perhaps I can do it without referring to my notes. This is entirely some linux housekeeping. For whatever reason, linux distros don't start this server by default in this modern age.

I do need my notes. The xinetd server is not installed by default and the package is no longer part of the Fedora package set. They tell me that the package was "retired" as of Fedora 34.

A quick search turned up some simple C source. I made a few trivial changes, compiled it, and am running it. It must be run as root in order to bind to port 13.

Why am I so hell-bent to get a daytime server? Well it was once a classic exercise in network programming and was running on every unix system on the planet -- but that was some time ago.

Back to VS Code

I thought maybe I would rename the first project I set up (ConsoleApp1), but apparently VS Code does not allow you to rename projects. I could rename it outside of VS Code, but I am willing to bet money that will just tangle me up in new and unexciting confusion. So I will just start a fresh new project.

I use Ctrl-Shift-P to get the command palette, then select again a console template, but I change the name to "Socket". Of course it times out while I am typing this, so I have to type do it all again, but I suppose the practice is good for me. It gives me a Program.cs -- I add code to it and then try to run and build it. VS Code is still stuck on running and building my old "hello world" project and it is not clear how to get it to deal with my current project, or how to make my current project (that it is letting me create and edit files in) the project it wants to Run and debug. Dare I call this a stupid bug? I can certainly call it stupid.

This is baffling, not to mention annoying. When I look at my vscode directory, I see the ConsoleApp1 directory alongside the Socket directory. One difference is that there is a ConsoleApp1.sln file, but no such file for "Socket". My guess is that "sln" is short for "solution" and somehow my way of using the command palette to set up my project did not get VS-code to consider it as a "solution".

Nuke it from orbit and start over

I exit VS-code and do this:
cd vscode
rm -rf *
This deletes everything except the .vscode directory. Then I restart code. I use Ctrl-Shift-P again, select new .NET project (but I notice an entry for "open .NET solution ...) I call the project Socket this time rather than ConsoleApp1. I make no changes and just try the Run and debug arrow on the left side.

I tells me that ConsoleApp1 is missing in launch.js -- well this is definitely a Microsoft bug. I don't see launch.js anywhere in my vscode directory. It is hard to believe people actually like this piece of software.

We get even more nasty:

cd /home/tom
rm -rf vscode
mkdir vscode
This does it. I can create a project "Socket" and without making changes I can run and debug the template boilerplate they give me. And I add some code and it compiles and runs that, so I am off to doing what I wanted to do.

The C# programming

This is really what we would like to focus on, rather than battling with VS-code bugs and bad design. I found a nice big of example code online, but as is so often the case, they don't show the required "using" statments required for it all to work. The example I found makes a call to Dns.GetHostEntryAsync(), but I get the error "Await operator can only be used within an Async method". Ah, now this is much more interesting that why VS-code is brain damaged. Actually, it is entirely possible to bypass the async/await business for now and write the code with synchronous calls. (Some people call them asynchronous calls, but I think they are confused).
Feedback? Questions? Drop me a line!

Tom's Computer Info / [email protected]