Duplex Named Pipe with Python Server and C# Clients

Wally96333 71 Reputation points
2020-12-03T23:23:30.78+00:00

Hi,

I am trying to set up a Python server to process objects from C# (.NET) clients
with a duplex named pipe. I've seen some good examples for setting up pipes,
but it's going the other way: With the server in C# and Python as the client.
Instead of "NamedPipeServerStream", I need to use "NamedPipeClientStream"
in .NET.

I came across the following code on SO:
https://stackoverflow.com/questions/57056598/named-pipes-ipc-python-server-c-sharp-client?noredirect=1&lq=1

The code as downloaded has problems.

sock.bind(SOCK_PATH)

:> TypeError: bind(): AF_INET address must be tuple, not str

My immediate problem is to figure out what sock.bind wants in the way of a tuple.
I thought that giving it a port number might be what it wants, but that is wrong:
sock.bind((SOCK_PATH, 443))
#socket.gaierror: [Errno 11001] getaddrinfo failed

Can anyone please help me figure this out?

If anyone knows of a good working example on how to open a named pipe
in Python, then have clients in C# send objects through the pipe then
receive the modified object I would very much appreciate it if you'd
clue me in.

THANKS!

Here is the code:

#!/usr/bin/python3

import socket
import os
import struct

#SOCK_PATH = "/tmp/CoreFxPipe_mySocket"
#  TRY changing this to the same within C#
# 
SOCK_PATH = "/mySocket"
#  SOCK_PATH = r'\\.\pipe\mySocket'

# with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock:
# Change socket.AF_UNIX to socket.AF_INET for Windows
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
    try:
        os.remove(SOCK_PATH)
    except OSError:
        pass
    #sock.bind(SOCK_PATH)
    #Traceback (most recent call last):
    #sock.bind(SOCK_PATH)
    #TypeError: bind(): AF_INET address must be tuple, not str

    sock.bind((SOCK_PATH, 443))
    #socket.gaierror: [Errno 11001] getaddrinfo failed

    sock.listen()
    conn, addr = sock.accept()
    with conn:
        try:
            while True:
                amount_expected = struct.unpack('I', conn.recv(4))[0]
                print("amount_expected :", amount_expected)

                message = conn.recv(amount_expected)
                print("Received message : ", message.decode())

                # Send data
                message_rev = message[::-1].decode()
                print("Sent message (reversed) : ", message_rev)

                conn.sendall(
                    struct.pack(
                        'I',
                        len(message_rev)
                    )
                    + message_rev.encode('utf-8')
                )

        except (struct.error, KeyboardInterrupt) as e:
            print(e)

        finally:
            print('closing socket')

C# Client

class PipeClient
{
public void PipeClient_Main(string[] args)
{
using (NamedPipeClientStream
pipeClient = new NamedPipeClientStream
(".", "mySocket", PipeDirection.InOut))
{

        // Connect to the pipe or wait until the pipe is available.
        Console.WriteLine("Attempting to connect to pipe...");
        pipeClient.Connect();

        try
        {
            // Read user input and send that to the client process.
            using (BinaryWriter _bw = new BinaryWriter(pipeClient))
            using (BinaryReader _br = new BinaryReader(pipeClient))
            {
                while (true)
                {
                    //sw.AutoFlush = true;
                    Console.Write("Enter text: ");
                    var str = Console.ReadLine();

                    var buf = Encoding.ASCII.GetBytes(str);     // Get ASCII byte array
                    _bw.Write((uint)buf.Length);                // Write string length
                    _bw.Write(buf);                              // Write string
                    Console.WriteLine("Wrote: \"{0}\"", str);
                    Console.WriteLine("Let's hear from the server now..");

                    var len = _br.ReadUInt32();
                    var temp = new string(_br.ReadChars((int)len));

                    Console.WriteLine("Received from client: {0}", temp);
                }
            }
        }
        // Catch the IOException that is raised if the pipe is broken
        // or disconnected.
        catch (IOException e)
        {
            Console.WriteLine("ERROR: {0}", e.Message);
        }

    }
    Console.Write("Press Enter to continue...");
}

}

Community Center | Not monitored
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.