Git Product home page Git Product logo

socketlite.pcl's Introduction

Important Note!

This is a legacy code-base. The code base moved to a new repo: SocketLite

Proceed with caution...

#SocketLite.PCL (Legacy Code Base) NuGet NuGet

Supports Xamarin Forms on Windows 10/UWP, iOS, Andriod and .NET Core

This project is a fork that build upon the fantastic work done with Socket for PCL.

Why this fork? Two reasons:

  1. The original Socket for PCL delivers great broad cross-platform support. SocketLite PCL only covers .NET 4.5+, UWP, iOS and Android.

  2. SocketLite has been refactored to use Reactive Extensions (Rx) internally as well as externally.

The purpose of this PCLis to make it easy to write cross platform socket code. For example Simple Http Listener is written using SocketLite.PCL

This library is based on "Bait and Switch" pattern. It is strongly recommend to read this short and great blog post to get an good understanding of this pattern before contributing to the SocketLite PCL code-base: The Bait and Switch PCL Trick

Get SocketLite.PCL in NuGet: Install-Package SocketLite.PCL

Classes

The plugin currently provides the following socket abstractions:

Class Description .NET Windows 10 / UWP
TcpSocketListener Bind to a port and accept TCP socket connections. TcpListener StreamSocketListener
TcpSocketClient Connect to a TCP endpoint with bi-directional communication. TcpClient StreamSocket
UdpSocketReceiver Bind to a port and receive UDP messages. UdpClient DatagramSocket
UdpSocketClient Send messages to arbitrary endpoints over UDP. UdpClient DatagramSocket
UdpSocketMulticastClient Send and receive UDP messages within a multicast group. UdpClient DatagramSocket

Examples Usage

A TCP Listener
var tcpListener = new SocketLite.Services.TcpSocketListener();
await tcpListener.StartListeningAsync(80, allowMultipleBindToSamePort: true);

var tcpSubscriber = tcpListener.ObservableTcpSocket.Subscribe(
	x =>
	{
	    System.Console.WriteLine($"Remote Address: {x.RemoteAddress}");
        System.Console.WriteLine($"Remote Port: {x.RemotePort}");
        System.Console.WriteLine("---***---");
	ex =>
    {
	    // Exceptions received here;
    }););
	
tcpSubscriber.Dispose();
A TCP Client
var tcpClient = new TcpSocketClient();
await tcpClient.ConnectAsync("192.168.1.100", 1234);

var helloWorld = "Hello World!";

var bytes = Encoding.UTF8.GetBytes(helloWorld);
await tcpClient.WriteStream.WriteAsync(bytes, 0, bytes.Length);
tcpClient.Disconnect();
A UDP Receiver
var udpReceived = new UdpSocketReceiver();
await udpReceived.StartListeningAsync(1234, allowMultipleBindToSamePort: true);

var udpMessageSubscriber = udpReceived.ObservableMessages.Subscribe(
    msg =>
    {
        System.Console.WriteLine($"Remote adrres: {msg.RemoteAddress}");
        System.Console.WriteLine($"Remote port: {msg.RemotePort}");

        var str = System.Text.Encoding.UTF8.GetString(msg.ByteData);
        System.Console.WriteLine($"Messsage: {str}");
    },
    ex =>
    {
        // Exceptions received here;
    });

// When done dispose
//udpMessageSubscriber.Dispose();
A UDP Client
var udpClient = new UdpSocketClient();

var helloWorld = "Voyager 1";

var bytes = Encoding.UTF8.GetBytes(helloWorld);

// Fire datagram into the great void
await udpClient.SendToAsync(bytes, bytes.Length, address:"192.168.1.5", port:1234);
A Multicast UDP Client
var udpMulticast = new SocketLite.Services.UdpSocketMulticastClient();
await udpMulticast.JoinMulticastGroupAsync("239.255.255.250", 1900, allowMultipleBindToSamePort:true); //Listen for UPnP activity on local network.

// Listen part
var tcpSubscriber = udpMulticast.ObservableMessages.Subscribe(
    x =>
    {
        System.Console.WriteLine($"Remote Address: {x.RemoteAddress}");
        System.Console.WriteLine($"Remote Port: {x.RemotePort}");
        System.Console.WriteLine($"Data/string: {Encoding.UTF8.GetString(x.ByteData)}");
        System.Console.WriteLine("***");
    });

// When Done Dispose
//tcpSubscriber.Dispose();

// Send part
//var msg = "Hello everyone";
//var bytes = Encoding.UTF8.GetBytes(msg);

//await udpMulticast.SendMulticastAsync(bytes);

//udpMulticast.Disconnect();
Using a Specific Network Interface

If no interface is specified the receivers/listeners bind to all available interfaces. If needed a specific interface can be specified.

var communicationInterface = new CommunicationInterface();
var allInterfaces = communicationInterface.GetAllInterfaces();

var firstUsableInterface = allInterfaces.FirstOrDefault(x => x.IsUsable);

var udpReceived = new UdpSocketReceiver();
await udpReceived.StartListeningAsync(
    port:1234, 
    communicationInterface:firstUsableInterface, 
    allowMultipleBindToSamePort: true);

socketlite.pcl's People

Contributors

1iveowl avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.