简体   繁体   中英

Sockets in Library Targetting .NET 4.5.1 and Windows 8.1/WP8.1

None of the Sockets APIs (System.Net.Sockets, System.Net.WebSockets, Windows.Networking.Sockets namespaces) exist in a PCL targetting .NET 4.5.1, Windows 8.1 and Windows Phone 8.1. What are my networking options to access a input and output Stream?

There aren't any socket API in common between the full .Net 4.5.1 Framework (using System.Net) and the Windows Runtime (using Windows.Networking). Since a PCL allows the intersection of the APIs for all of the PCL's targets this means there are no Socket API available directly in that PCL. You'll have to open the socket in the platform specific code.

All of these targets do support .Net Streams, so once you've opened the socket in platform specific code you can pass the stream for the socket into the PCL for handling.

You can use Inversion of Control to let the PCL request the stream from the host when necessary. The PCL can define an interface (eg IPlatformSpecificCode) which the host can implement. When the PCL wants to open the network connection it can call IPlatformSpecificCode.OpenPlatformSpecificSocket(address,port) and the host can open a System.Net.Sockets.Socket or a Windows.Networking.Sockets.StreamSocket and return a .Net stream for the PCL to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM