# Named Pipes between Julia and CSharp

**URL:** <https://discourse.julialang.org/t/named-pipes-between-julia-and-csharp/45845>\
**Category:** Performance\
**Tags:** performance\
**Created:** [August 31, 2020, 3:30pm UTC](https://discourse.julialang.org/t/named-pipes-between-julia-and-csharp/45845 "2020-08-31T15:30:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [August 31, 2020, 3:30pm UTC](https://discourse.julialang.org/t/named-pipes-between-julia-and-csharp/45845/1 "2020-08-31T15:30:33Z")

</div>

Hi dear community 🙂

I’m trying to communicate between Julia and CSharp with named pipes on Windows.

I’m using following code in Csharp server:

```julia
using (var pipe = new NamedPipeServerStream("cs", PipeDirection.InOut))
{
    pipe.WaitForConnection();
    var sw = new StreamWriter(pipe);
    var sr = new StreamReader(pipe);

    while (true)
    {
        var s = sr.ReadLine();
        sw.WriteLine(s);
        sw.Flush();
    }
}

```

and the following in Julia client:

```julia
using Sockets
c = connect(raw"\\.\pipe\cs")
function write_read(c, str)
    write(c, str * "\n")
    readline(c)
    nothing
end
data = rand('a':'z', 10_000_000) |> String
@time write_read(c, data) # about 27s

```

Julia-Julia, CSharp-CSharp and Julia(Server)-Sharp(Client) are much faster (0.06s).

How can I speed it up. In other words, what is the problem?

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [September 1, 2020, 7:26am UTC](https://discourse.julialang.org/t/named-pipes-between-julia-and-csharp/45845/2 "2020-09-01T07:26:16Z")

</div>

Any idea what it could be?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 1, 2020, 8:14am UTC](https://discourse.julialang.org/t/named-pipes-between-julia-and-csharp/45845/3 "2020-09-01T08:14:07Z")

</div>

I’m not sure `write` sends a final newline, which your code seems to expect.

---

<div class="post-metadata">

**Author:** ![Impressium](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/impressium/32/19575_2.png) [@Impressium](https://discourse.julialang.org/u/Impressium)\
**Post date:** [September 1, 2020, 8:30am UTC](https://discourse.julialang.org/t/named-pipes-between-julia-and-csharp/45845/4 "2020-09-01T08:30:52Z")

</div>

Sorry, that was a typo. Did you manage to replicate it on your machine?

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [September 1, 2020, 3:20pm UTC](https://discourse.julialang.org/t/named-pipes-between-julia-and-csharp/45845/5 "2020-09-01T15:20:43Z")

</div>

I don’t have a CSharp setup so I can’t really test anything. What I would suggest is printing the current time on the CSharp server when the line has been read, when the line has been written, and after the channel has been flushed. On the Julia side print the current time after the write and after the readline.

Hopefully that would suggest where the issue might be. Also on the Julia side, can you do a `flush(c)` after the write?

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [May 9, 2021, 2:51pm UTC](https://discourse.julialang.org/t/named-pipes-between-julia-and-csharp/45845/6 "2021-05-09T14:51:59Z")

</div>

I am currently working on a quite similar project, but my server is implemented on julia’s side, and this communication felt pretty fast.

However, that is why I am communicating on this thread instead of a new one, I was wondering how you handle the two way communication between your processes.

The current state of my program is the following :

1. C# fires up a julia process which takes a small file starting my named pipe server using the Sockets library and a

2. I have a function based on the MessagePack-CSharp package which handles the serialization on the C# side

3. I send data (as `MessagePackObject`s) or instructions (as strings) over the pipe.

Now I was wondering what the best way to send information back to C# was :

- Communicate over the same pipe ? That means knowing when to wait and when to send information, and this works only with named pipes (but I am unsure if julia supports this)
- Create another pipe only for the return communication?

As you seem to have tested in details the communication over pipes using julia, I eager to have your opinion on this. Cheers !
