Skip to content

jchristn/SimpleUdp

Repository files navigation

alt tag

SimpleUdp

Simple wrapper for UDP client and server in C#

NuGet Version NuGet

SimpleUdp provides simple methods for creating your own UDP-based sockets application, enabling easy integration of sending data, receiving data, and building state machines.

New in v3.1.1

  • Add Touchstone-based shared, console, xUnit, and NUnit test projects under src/
  • Add multi-process startup-order coverage and package-consumer verification
  • Keep the receive loop alive on Windows after sending to a UDP port that is not listening yet
  • Prevent invalid destination IP sends from leaking the internal send semaphore

New in v3.1.0

  • Add EnableBroadcast for opt-in UDP broadcast sends without changing the existing send and receive APIs

New in v3.0.0

  • Require .NET 8.0 or newer
  • Use a single bound socket for both inbound and outbound UDP traffic

New in v2.0.x

  • Retarget to .NET 8.0
  • Removal of Start, Stop APIs, and, the started event
  • Better multi-platform compatibility (Windows, Mac OSX, Ubuntu)

Help or Feedback

Need help or have feedback? Please file an issue here!

Tests

Touchstone-based tests live under src/:

  • Test.Shared contains the reusable test case descriptors and helpers
  • Test.Automated is the console runner
  • Test.Xunit runs the shared cases through the Touchstone xUnit adapter
  • Test.Nunit runs the shared cases through the Touchstone NUnit adapter

Run the console suite:

dotnet run --project src/Test.Automated --framework net10.0

Run adapter suites:

dotnet test src/Test.Xunit/Test.Xunit.csproj --framework net10.0
dotnet test src/Test.Nunit/Test.Nunit.csproj --framework net10.0

Special Thanks

Thanks to community members that have helped improve this library! @jholzer @charleypeng @seatrix

Need TCP Instead?

I have you covered.

Don't know what to use? Just ask! File an issue, I'll be happy to help.

Simple Example

Start a node.

using SimpleUdp;

UdpEndpoint udp = new UdpEndpoint("127.0.0.1", 8000);
udp.EndpointDetected += EndpointDetected;

// only if you want to receive messages...
udp.DatagramReceived += DatagramReceived;

// send a message...
udp.Send("127.0.0.1", 8001, "Hello to my friend listening on port 8001!");

// enable broadcast if you need to send to a broadcast address...
udp.EnableBroadcast = true;
udp.Send("255.255.255.255", 8001, "Hello to everyone listening on port 8001!");

static void EndpointDetected(object sender, EndpointMetadata md)
{
  Console.WriteLine("Endpoint detected: " + md.Ip + ":" + md.Port);
}

static void DatagramReceived(object sender, Datagram dg)
{
  Console.WriteLine("[" + dg.Ip + ":" + dg.Port + "]: " + Encoding.UTF8.GetString(dg.Data));
}

The Node Project

Start node 1.

Node\bin\Debug\net8.0> node 127.0.0.1 8000

Start node 2.

Node\bin\Debug\net8.0> node 127.0.0.1 8001

Send message from node 1 to node 2. To do this, enter a command as follows:

[ip:port] [msg]

i.e.

127.0.0.1:8001 hello to my friend running on port 8001!
[127.0.0.1:8000 Command/? for help]: 127.0.0.1:8001 hello to my friend on port 8001!

Send message from node 2 to node 1.

[127.0.0.1:8001 Command/? for help]: Endpoint detected: 127.0.0.1:8000
[127.0.0.1:8000]: hello to my friend on port 8001!
127.0.0.1:8000 hello back to you my friend!

Version History

Please refer to CHANGELOG.md.

About

SimpleUdp is a super simple way of building UDP clients and servers in C#.

Topics

Resources

License

Code of conduct

Contributing

Stars

46 stars

Watchers

1 watching

Forks

Releases

No releases published

Sponsor this project

  •  

Packages

 
 
 

Contributors

Languages