Skip to content

s4lt3d/Named-pipes-example-for-cs-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 

Repository files navigation

Cross-Application Data Exchange using Named Pipes

A demonstration of inter-process communication (IPC) between C# and Python using Windows named pipes.


Overview

This guide shows how to establish bidirectional communication between a C# application and a Python script using named pipes, a Windows mechanism for IPC using a server-client model.


Prerequisites

  • .NET SDK (for C# example)
  • Python 3.x
  • pywin32 library for Python:
    pip install pywin32

How to Use

  1. Run the C# application to start the named pipe server
  2. Run the Python script to connect and communicate
  3. Both applications will exchange messages:
    • C# displays: "Hello Back"
    • Python displays: "Hello Python"

Features

  • Server-client architecture — C# acts as server, Python as client
  • Bidirectional messaging — Send and receive data
  • Windows-native — Uses Windows named pipes for reliable IPC
  • Simple interface — Straightforward message passing
  • Error handling — Graceful connection management

Architecture

Named Pipes Overview

Named pipes are a Windows IPC mechanism:

  • Server listens on a named pipe
  • Client connects to the named pipe
  • Data flows bidirectionally through the pipe
  • No network required; works on same machine or network

Communication Flow

C# Application (Server)
    ↓
Named Pipe: "\\.\pipe\MyAppPipe"
    ↓
Python Script (Client)
    ↓
Bidirectional Message Exchange

Code Structure

C# Server

// Create named pipe server
NamedPipeServerStream server =
    new NamedPipeServerStream("MyAppPipe");

// Wait for client connection
server.WaitForConnection();

// Send/receive data
StreamWriter writer = new StreamWriter(server);
StreamReader reader = new StreamReader(server);

Python Client

# Connect to named pipe
with open(r'\\.\pipe\MyAppPipe', 'r+b', buffering=0) as pipe:
    # Send message
    pipe.write(b'Hello from Python')

    # Receive message
    message = pipe.read(1024)

Implementation Details

Message Format

Messages are sent as UTF-8 encoded strings:

  • C# sends: StreamWriter.WriteLine()
  • Python sends: pipe.write(message.encode())
  • Null terminator: Optional but recommended for delimiting

Connection Lifecycle

  1. Server listens — Named pipe created and waiting
  2. Client connects — Establishes connection to pipe
  3. Handshake — Optional acknowledgment messages
  4. Exchange — Bidirectional communication
  5. Disconnect — Close streams and release resources

Troubleshooting

"Pipe does not exist" Error

  • Ensure C# server is running before starting Python client
  • Check pipe name matches exactly (case-sensitive)
  • Verify firewall isn't blocking

Connection Timeout

  • Server may be blocking on read; implement threading
  • Use non-blocking I/O for better responsiveness

Access Denied

  • Named pipes require appropriate Windows permissions
  • Run as administrator if needed

About

Cross-Application Data Exchange using Named Pipes for C# and Python

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors