I'm starting to work on a chess engine and I want to follow the uci interface where comunication is done using plane text. In the specification it says
all communication is done via standard input and output with text commands
but I dont know what this means in real terms.
I'm thinking of writing in C# as a .net standard library. I understand that as uci is cross platform we cannot talk in language like Console.WriteLine but what does that line in the api mean for me.
Do I have to run a loop listening for Console.ReadLine() or something similar for standard input? I just don't get it. Or should I be writing a console application that takes the input as a command line argument, and writes string to the console?
The full specification can be downloaded from this link:
http://download.shredderchess.com/div/uci.zip
Any process have three default streams:
Standard input (stdin): This is the only input stream for all terminal or console aps. When you cal Console.ReadLine or Console.Read the result is taken from this stream.
Standard Output (stdout): When you call output-related commands in the console singleton class, all data goes here. For example the WriteLine or theWrite methods. The color and beeps commands send data to there too.
Standard error (stderr): This is a dedicated stream to print error-related content to the console. This is dedicated because some applications and scripting solutions want to hide these messages.
You can communicate with the containing process (such a console prompt) with these streams. The command line arguments can be passed only to the program's main method.
Related
What I'm Trying to Achieve
I'm attempting to build a console game that has multiple console windows that would be displaying inventory, status effects, current map, and health. Another console would be the main one that gathers input to effect the other consoles. The reason I want to do it this way is so that the other consoles can be updating their "graphics" (or text) without disturbing the input flow.
What I've Tried So Far
So far, I've attempted to use System.IO's File, FileStream, StreamWriter, and StreamReader to communicate between the consoles via text files. The problem I've ran into is that, when the main console (the input console) is attempting to write inputs to a file--which is communicating with another console (the "graphics" console)--it throws an error because the "graphical" console is trying to read the input of the file (or vice versa).
I figured that making the FileStream's FileAccess be Readable would do the trick, but I ran into the same issue.
I think I could get this to work if I could communicate between the consoles to tell each other that one is done writing to or reading the file... kind of like a back and forth... "I'm writing to the file... okay, I'm done" "I'm reading the file... okay, I'm done" and the cycle continues...
So, in summary, I suppose, my question is how can I communicate between two consoles using files?
Possible Solutions I could try learning SQL, but I don't know if I'd end up running into the same issue... so, if I must learn SQL for this project, I suppose, that'd be my last option.
Thank you!
IPC (inter process communication) is the keyword you're looking for.
There are multiple ways to do IPC, e.g. shared memory, named pipes or similar. .NET has an IpcChannel which uses TCP or a named pipe if the destination is on the local PC.
I'm trying to send data from a python script to my c# application using a standard input stream. I also need to eventually send data back from my c# application to the another python script. I first tried to do this with a UDP Connection which works fine for a couple lines, but I have a fair amount of data to send (a few thousand lines) and a requirement for data integrity which UDP cannot provide. I could also write to a file, but that seems very inefficient.
One last restriction is that while my two applications are related I cannot setup a direct connection between them using something like IronPython as they are both spawned separately by a 3rd party application.
This is what I am currently trying, but it is not working. Similar to this question: Passing data between Python and C# without writing a file
p = subprocess.Popen(C_SHARP_EXECUTABLE_FILE_PATH, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
p.communicate(blob)
On my C# side I'm not entirely sure how to read this data, but I've tried using things like a loop around this:
Console.ReadLine()
or getting the standard input and reading from it directly using:
Console.OpenStandardInput();
The current issue is that as soon as I call p.communicate my Python script gets locked and doesn't proceed. If it's waiting for the line to be read, what do I need to do to make it stop waiting? I tried only providing the stdin parameter, but that didn't help.
I've an application same as command line application. I type command, it send command to server and display result on screen.
My application is an executable application on windows and has two feature that help me to easy work: record script and play script.
Now, I want to send command to running application and raise play script button to run sent command on it.
Is there any way to solve this problem in c# language?
Uh-oh. I hope I understood you correctly. I assume you have a third-party application that interacts with some server and is operated by two distinct commands, play and record. You want to write a new application that would invoke these commands somehow, and the problem is this invocation.
You can do it in C#, but the way you do it depends on the way you operate the program. I will show you some cases:
The program is a command line tool. Input is sent via command line parameters when the program is run. Output is gathered via redirections of the standard output of the program. Look at System.Diagnostics.Process.Start and its parameters, namely ProcessStartInfo which contains parameters and input/output redirection options.
The program is a console tool that is not operated via command line parameters. Use the same as above, but feed input via input redirection.
The program is a GUI program with no ability to get input in text form. This is most likely the case. Here you will have to either find a specific way to operate the program such as with an advanced technique called hooking. Or you can send commands as if a user is issuing them. Look at System.Windows.Forms.SendKeys method. It might help. How to get a result here is a big question. You can either parse the screen or monitor some state of a program or something else. No ready solution for everyone here.
What I am trying to do is see if I can use the MinGW C++ compiler and debugger to compile files using a C# app. I want to be able to read the output and write to the input (input mainly for the debugger) within the C# app whenever I need to.
Reading from output will be much like Visual Studio does in its 'Output' window durring a project build.
I have used System.Diagnostics.Process before but haven't been able to figure out how to interact with the processes.
So, how do I take control of std input/output?
Redirecting standard input
Reading standard output
The following question shows how to capture standard output:
How do I run a Console Application, capture the output and display it in a Literal?
Writing to standard input is similar -- set RedirectStandardInput to true, and use a streamwriter to write to it:
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardinput.aspx
How can I read from standard input and write to standard output. System.Diagnostics.Process.StandardInput's MSDN reference didn't help as it separately starts the process and then redirects the Standard Input/Output but what If the process is already running and called my Application to feed it some data. Here's an example to make things a bit clear:
I am simply using Unix pipes i.e. cat command in cygwin (A Linux like Environment for windows) that basically just reads standard input and print to standard output. following is the command:
% cat input/sample.txt | src/csharp/maptest
But that doesn't seems to work.
If some one know ruby here as i don't here's what i want to do the same in C#:
#!/usr/bin/env ruby
STDIN.each_line
do |line|
some code here
end
And here's some python equivalent code that i want to accomplish in c# or vb.net:
#!/usr/bin/env python
import re
import sys
for line in sys.stdin:
val = line.strip()
Any solutions?
Thanks in advance.
You're looking for the static methods in the Console class :
System.Console.In encapsulate the standard input stream
System.Console.Out encapsulate the standard output stream
Pointers to documentation for System.IO.Pipes are a red herring here.
If you just want the ability to have a process pipe its standard output to another process's standard input, then this works the same as you would expect provided the target process is written to read input from standard input. For example I can do
dir /b /s c:\*.* | findstr exe
to find all executable files on my C: drive.
All you need to do therefore is to build your maptest application so that it reads from standard input, in other words it must accept input via the Console.Read* methods as mentioned in other answers. See this previous question for an earlier discussion
C# Console receive input with pipe
You can't use Console.ReadLine() and Console.WriteLine()?
Never mind the above. Have you tried System.IO.Pipes? Here's the MSDN documentation.