My dilemna is probably simple, but I am in an endless (mental) loop trying strategies that have not worked. I am sure there is a ‘best/better’ way.
Writing my first console app (a basic TCP server using ServerSocket). I want to input keystrokes from the Terminal for commands (quit, listen, stop listening, write out stats to Terminal etc). I whipped up a simple parsing routine called ‘processInput ( s as string)’ which pulls from StdIn and breaks the strings down into discrete actions that my server responds to. Here is the basics of my App.Run event code:
// instantiate server, set up root directory, networkInterface, port, start listening, etc) App.running = true Do App.processInput (Input) 'parsing method fed by StdIn App.DoEvents(100) Loop until not App.running return 0
When I run this, no Socket events are fired. The server is dead silent. My surprise was that I copy/pasted the exact server code/classes from a Desktop app where it was working perfectly. After a number of hours and trial/error, I found that when I hit the Enter key in the Terminal, all the socket events fired.
Great…calling StdIn obviously blocks the loop awaiting the Enter key before returning the input stream. Simple solution. I just commented out the processInput method. Now the server is alive and all socket events fire as they did in the Desktop version. But I can’t communicate with the Console App via the Terminal for even basic things like sending a Quit message.
Next, I tried to spin off a TCPSocket to listen to StdIn (as per the Language Ref) thinking this would work asynchronously…
//prior to Do...Loop App.keyboardListener as TCPSocket keyboardListener = StdIn AddHandler keyboardListener.DataAvailable, AddressOf processInput keyboardListener.connect keyboardListener.listen
I updated the input param for my parsing routine (to TCPSocket). It compiles and runs, but does nothing in response to keystrokes in the Terminal.
So, I am looking for a way to poll StdIn without blocking the event loop in Console app. I expect this is quite simple…but… Best way???
Many thanks in advance