0

I'm writing a serial port to TCP bridge in C#. It's a console application with 4 threads.

  1. main thread for reading commands from console (e.g. 'exit' to close the application)
  2. if a connection is established, it handles a client
  3. a TCP worker thread, which copies all received TCP data to COM3
  4. a UART worker thread, which copies all received UART data to TCP

This applications works fine if it's launched locally on my PC either in Visual Studio debug modus, in CMD.exe or even in PowerShell.

Now I want to grant one of my colleges the right to launch UARTServer.exe in a remote PowerShell session. PS is setup in the correct way and he can use PS like a ssh session (Enter-PSSession -ComputerName xxxx -Credentials yyyy).

But when he starts UARTServer.exe in his remote session, Console.ReadLine() returns with null.

Here is the C# snipped for the main thread, which reads from stdin to look for commands like 'exit'.

        Console.WriteLine("Starting UARTServer...");

        uartServerThread = new Thread(new ThreadStart(ServerWorker));
        uartServerThread.Start();

        Console.WriteLine("Enter 'exit' to stop this server.");
        Console.Write("UARTServer> ");

        while(true)
        {
            var input = Console.ReadLine();
            if (input == "exit")
            {
                break;
            }
            else if (input == null)
            {
                Console.WriteLine("Null");
                break;
            }
            else
            {
                Console.WriteLine("Unknown command");
                Console.Write("UARTServer> ");
            }
        }

        Console.WriteLine("Shutting down UARTServer...");
        uartServerThread.Abort(true);
        uartServerThread.Join();

        Console.WriteLine("Exiting UARTServer");

This is the corresponding console output:

[localhost]: PS D:\Shares\SATA> .\UARTServer.exe
Starting UARTServer...
Enter 'exit' to stop this server.
UARTServer> Null
Shutting down UARTServer...
Exiting UARTServer
[localhost]: PS D:\Shares\SATA>

My questions:

  • Why does it happen? Is Stdin not connected in a remote session?
  • How can I fix this?
Paebbels
  • 15,573
  • 13
  • 70
  • 139

1 Answers1

0

Many streams behave quite differently when they are on top of a network connection, such that .Read will return nothing if there is nothing available in the buffer. I suspect that when remoting on the command line then your Console is actually on top of a network stream. Note in this example they are checking for a null result from ReadLine and looping until they have something to actually process:

https://stackoverflow.com/a/1450496/84206

while((line = Console.ReadLine()) != null) {
    // do something with line
}

I would suggest this approach so that you can loop until a line is received from the client. There are more refined approaches that avoid looping indefinitely and consuming CPU just for a loop that is waiting for data. But this demonstrates how you have to adjust for stream reads that sometimes return nothing.

Community
  • 1
  • 1
AaronLS
  • 37,329
  • 20
  • 143
  • 202