Search code examples
c#gopipe

How make long-term pipe connection between go and c# console application


I have pretty straightforward C# console app. It will be a process that add double variables that were passed from go part.

double sum = 0;
bool flag = true;

while(flag) {  // exit on convert error
    var str = Console.ReadLine(); // ask string 

    double addend;
    flag = double.TryParse(str, out addend); // convert to double

    sum += flag ? addend : 0;  //add on success
    Console.WriteLine(sum);
}

return 0;

And my go part is simple too. It create process with previous console app, send strings to it and read back results.

package main

import (
    "fmt"
    "io"
    "log"
    "os/exec"
)

func main() {

    cmd := exec.Command("%path to app%\\ConsoleApp.exe") // process that will be started
    
stdout, err := cmd.StdoutPipe() // use std out pipe
    if err != nil {
        log.Fatal(err)
    }

    stdin, err := cmd.StdinPipe() // use std in pipe
    if err != nil {
        log.Fatal(err)
    }

    if err := cmd.Start(); err != nil { // start
        log.Fatal(err)
    }

    for true {
        var w1 string
        _, err := fmt.Scanln(&w1) // read line

        if err != nil {
            log.Fatal()
        }

        go func() {
            io.WriteString(stdin, w1) // pass it to console app
        }()

        read, err := io.ReadAll(stdout) // read result

        fmt.Println(read)

        if err != nil {
            log.Fatal()
        }
    }
}

I start debug for process of console app (it appears in task manager, RMC - Debug) but there is symbol errors, so I can't even understand did my app get the string or didn't.

That's only half of my trouble. I think once or twice my console app got string (I mean in previous times), but only there first one, cause pipe closes after transmition/copying from it. So how can I control this? I want to do several transactions, write and read, and close pipes only when I need it.

EDIT: I've changed ConsoleApp a bit. Now it looks like this:

double sum = 0;
bool flag = true;
while(flag) {
    var str = Console.ReadLine();
    double addend;
    flag = double.TryParse(str, out addend);

    // File creation will indicate that message was recieved. 
    using(StreamWriter writer = new StreamWriter("t.txt")) { 
        writer.WriteLine($"{flag}");
    }

    sum += flag ? addend : 0;
    Console.WriteLine(sum);
}

return 0;

Solution

  • So, @maxm was realy close (or even has got the solution, but it didn't work with me, fsr).

    The go part:

    package main
    
    import (
        "bufio"
        "fmt"
        "log"
    
        "os/exec"
    )
    
    func main() {
        cmd := exec.Command("%path to the exe%")
    
        stdout, err := cmd.StdoutPipe() 
        if err != nil {
            log.Fatal(err)
        }
    
        stderr, err := cmd.StderrPipe()
        if err != nil {
            log.Fatal(err)
        }
    
        scanner := bufio.NewScanner(stdout)
    
        stdin, err := cmd.StdinPipe()
        if err != nil {
            log.Fatal(err)
        }
    
        if err := cmd.Start(); err != nil {
            log.Fatal(err)
        }
    
        for {
            var w1 string
            _, err := fmt.Scanln(&w1)
            w2 := []byte(w1) // important! You need convert string to byte array
    
            if err != nil {
                log.Fatal()
            }
    
            if _, err := stdin.Write(w2); err != nil { // pass byte array
                log.Fatal(stderr)
                panic(err)
            } 
    
            if scanner.Scan() {
                read := scanner.Text()
                fmt.Println(read)
            } else {
                if err := scanner.Err(); err != nil {
                    panic(err)
                }
            }
        }
    }
    
    

    C# part is pretty easy. First of all you need restore string from byte array:

    public static string ByteArrayToString(this byte[] byteArray, int size) {
        // get decoder to decode input encoding
        var decoder = Console.InputEncoding.GetDecoder(); 
    
        // count char that will be restored
        int charCount = decoder.GetCharCount(byteArray, 0, size);
    
        // initialize char array
        char[] charArray = new char[charCount];
        // restore to char array
        decoder.GetChars(byteArray, 0, size, charArray, 0);
    
        // get and return string from our char array
        return new string(charArray);
    }
    

    Main part is next:

    double sum = 0;
    bool flag = true;
    while(flag) {
        string str;
    
        using Stream stdin = Console.OpenStandardInput(); // for reading 
    
        byte[] buffer = new byte[2048];
        int bytes = 0; // bytes count
    
        while((bytes = stdin.Read(buffer, 0, buffer.Length)) > 0) { // read
            str = buffer.ByteArrayToString(bytes); // restore string
            flag = double.TryParse(str, out double addend);
            sum += flag ? addend : 0;
            Console.WriteLine(sum); //write
    
            if(!flag)
                break;
        }
    }
    
    return 0;