RedRat Hub C# Example Client Code

The few lines of code below open a socket to the hub and uses that to send commands to the irNetBox (with IP address 192.168.1.235):


// Simple C# console application to use RedRatHub.
// Chris Dodge, Jason Rawlings - RedRat Ltd.

var client = new Client();

// Connect to the RedRatHub
client.OpenSocket("localhost", 40000);

// Send some IR signals
client.SendMessage("ip=\"192.168.1.235\" dataset=\"Sky+\" signal=\"9\" output=\"12:10\"");
Console.WriteLine("Sent signal\n");
Thread.Sleep(2000);

client.SendMessage("ip=\"192.168.1.235\" dataset=\"Sky+\" signal=\"9\" output=\"12:10\"");
Console.WriteLine("Sent signal\n");
Thread.Sleep(2000);

client.SendMessage("ip=\"192.168.1.235\" dataset=\"Sky+\" signal=\"9\" output=\"12:10\"");
Console.WriteLine("Sent signal\n");
Thread.Sleep(2000);

// List the datasets known by the hub
Console.WriteLine("List of datasets:");
var list = client.ReadData("hubquery=\"list datasets\"");
Console.WriteLine(list);

client.CloseSocket();
Console.WriteLine("Finished. Hit  to exit...");
Console.ReadLine();
This uses a C# class for sending and receiving RedRatHub messages:

/// 
/// Simple C# class to send commands to the RedRatHubCmd application via a socket.
/// Chris Dodge, Jason Rawlings - RedRat Ltd.
/// 
public class Client
{
    private static Socket socket;

    /// 
    /// Opens the socket to RedRatHubCmd.
    /// 
    public void OpenSocket(string host, int port)
    {
        if (socket != null && socket.Connected) return;

        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        socket.Connect(host, port);
    }

    /// 
    /// Closes the RedRatHubCmd socket.
    /// 
    public void CloseSocket()
    {
        socket.Close();
    }

    /// 
    /// Sends a message to the ReadData() method. Use when returned data from RedRatHub is not needed.
    /// 
    public void SendMessage(string message)
    {
        ReadData(message);
    }

    /// 
    /// Reads data back from RedRatHub. Use when returned data is needed to be output.
    /// 
    public string ReadData(string message)
    {
        if (socket == null || !socket.Connected)
        {
            Console.WriteLine("\tSocket has not been opened. Call 'OpenSocket()' first.");
            return null;
        }

        // Send message
        socket.Send(Encoding.UTF8.GetBytes(message + "\n"));
        var received = "";

        // Check response. This is either a single line, e.g. "OK\n", or a multi-line response with 
        // '{' and '}' start/end delimiters.
        while (true)
        {
            var receivedBytesFull = new byte[256];

            // Receive data
            var receivedLength = socket.Receive(receivedBytesFull);
            var receivedBytes = new byte[receivedLength];
            Array.Copy(receivedBytesFull, receivedBytes, receivedLength);
            received += Encoding.UTF8.GetString(receivedBytes);

            if (CheckEom(received)) return received;
        }
    }

    /// 
    /// Checks for the end of a message
    /// 
    public bool CheckEom(string message)
    {
        // Multi-line message
        if (message.EndsWith("}"))
        {
            return message.StartsWith("{");
        }

        // Single line message
        return message.EndsWith("\n");
    }
}

Download C# Code