Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Cpp-CSharp-UDP-Example

A simple cross-language networking example demonstrating UDP communication between C++ and C#.

Overview

This project shows how two applications written in different programming languages can communicate using the UDP (User Datagram Protocol).

  • Sender: C++ (Linux, POSIX Sockets)
  • Receiver: C# (.NET UdpClient)

The C++ application sends a UDP packet to the C# application, which receives the packet and prints the message to the console.


Project Structure

Cpp-CSharp-UDP-Example/
│
├── cpp_sender_UDP/
│   └── main.cc
│
└── Csharp_receiver_UDP/
    └── Program.cs

Technologies

C++

  • POSIX Sockets
  • socket()
  • connect()
  • sendto()
  • sockaddr_in
  • inet_addr()
  • htons()

C#

  • .NET
  • UdpClient
  • IPEndPoint
  • Receive()
  • Encoding.ASCII

Communication

+---------------------+         UDP          +----------------------+
| C++ Sender          | ------------------>  | C# Receiver          |
| Linux / POSIX       |                      | .NET UdpClient       |
+---------------------+                      +----------------------+

Default configuration:

Setting Value
Protocol UDP
IP Address 127.0.0.1
Port 2525

How It Works

  1. The C++ application creates a UDP socket.
  2. The destination IPv4 address and port are configured.
  3. A UDP packet is sent to the receiver.
  4. The C# application listens on port 2525.
  5. The received bytes are converted to an ASCII string.
  6. The message is displayed on the console.

Example Output

Sender:

Success connect
Message send

Receiver:

C++ & C# connected.

Learning Goals

This project demonstrates:

  • Basic UDP socket programming
  • Cross-language communication
  • IPv4 networking
  • Sending and receiving datagrams
  • Basic POSIX Socket API
  • Basic .NET networking

Notes

  • This project uses UDP, which is connectionless and does not guarantee packet delivery.
  • The communication is performed over the loopback interface (127.0.0.1) for local testing.
  • This example is intended for educational purposes and can serve as a starting point for more advanced networking projects.

Future Improvements

  • TCP version TCP Server TCP Client
  • Bidirectional communication
  • Client/Server chat (Coming Soon)
  • Binary data transfer
  • JSON message exchange
  • IPv6 support
  • Error handling improvements

What is POSIX?

POSIX (Portable Operating System Interface) is a family of IEEE standards that defines a common programming interface for Unix-like operating systems.

The C++ implementation in this project uses the POSIX Socket API, including functions such as:

  • socket()
  • connect()
  • sendto()
  • close()

Because these APIs are standardized, the same networking code can typically be compiled and run on many Unix-like operating systems with little or no modification, including:

  • Linux
  • macOS
  • FreeBSD
  • OpenBSD
  • NetBSD

This project uses POSIX sockets on the C++ side, while the C# implementation uses the .NET UdpClient API. Although the APIs are different, both communicate using the same standard UDP/IP protocol, allowing seamless cross-language communication.


Thanks Mohammad Yousefi

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages