Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Portable Class Library and UDP support

I am writing a C# library for the Philips Hue Lights. I am trying to write the base API wrappers in the .NET portable set that way I can re-use this library for various platforms such as Windows 8/RT/WP. The API itself is all over HTTP using REST, so HttpWebRequest will serve most of my needs.

The network bridge that controls the lights themselves can be discovered using SSDP over UDP. However, I am unable to find a way to use UDP sockets in the portable class library (PCL).

There is no System.Net.Sockets available. There is nothing in the System.Net namespace that would allow it either. I have seen a DatagramSocket listed in Windows.Networking.Sockets but am unable to see that namespace in Intellisense.

Does anyone have any idea how I could get UDP functionality for SSDP under the .NET PCL?

I really do not want to have to separate the discovery functionality from the core library.

Right now I am targeting .NET 4.5 + SL 5 + WP 8 + .NET for Windows Store. I was under the impression that Sockets were available still.

like image 678
Erik Avatar asked Jan 11 '13 17:01

Erik


People also ask

What is the difference between .NET Standard and PCL portable class libraries )?

NET Standard is platform-agnostic, it can run anywhere, on Windows, Mac, Linux and so on. PCLs can also run cross-platform, but they have a more limited reach. PCLs can only target a limited set of platforms.

What is portable class library in C#?

The Portable Class Library project enables you to write and build managed assemblies that work on more than one . NET Framework platform. You can create classes that contain code you wish to share across many projects, such as shared business logic, and then reference those classes from different types of projects.

What is .NET PCL?

Portable Class Libraries (PCL)

What is library in c# net?

A class library defines types and methods that are called by an application. If the library targets . NET Standard 2.0, it can be called by any . NET implementation (including . NET Framework) that supports .


1 Answers

There isn't a common intersect for socket support between WinRT and WPF apps, and so it isn't available in PCL projects targeting them.

I have a PCL library targeting WPF and WinRT that interacts with a UDP discovery network, and the cleanest implementation I came up with involved creating an IUDPSocket interface in the PCL library that defines members for sending / receiving data and connecting to multicast groups. The WPF app implements my IUDPSocket using a System.Net.Sockets.Socket, and the RT app implements this using a Windows.Networking.Sockets.DatagramSocket.

The constructor of my discovery network client class (defined in the PCL project) takes a delegate which is used to create an instance of the IUDPSocket. I do this instead of passing in an initialized IUDPSocket instance so the calling code doesn't have to know about which port(s) or address(es) are involved.

like image 83
Derek Smithson Avatar answered Sep 18 '22 16:09

Derek Smithson