Official website

Tuesday, July 15, 2014

Communicate over a TCP socket

In the Internet of Things era, a good practice to communicate between protocol-agnostic software and/or hardware is a tailor made implementation on top of TCP. Jubito is taking advantage of Internet infrastructure and supports communication requests.

UPDATE: Currently the AppConfig.xml  configuration file is fully supported by the UI. Under settings you can find menus that corresponds to each functionality.


Trusted clients should be semicolon delimited.

I leave the paragraph below as is, in case you want to edit the file and see things behind the scenes.
Let's move to our example. First of all we need to configure the host and the port. Edit AppConfig.xml file and add your preferences to the following fields...

jaNET/System/Comm/localHost - A hostname of the server to connect to (it can also be its raw IP address)
jaNET/System/Comm/localPort - A number representing the TCP port to be used by the socket (Default is 5744)
jaNET/System/Comm/Trusted - Authorized clients delimited by semicolon

Sample Screenshot...


Save and close the file and go to Control Panel/Switches and turn on the socket.


Or by terminal...

judo socket open


We'll start our first debugging by sending some data via telnet.
Open command line ot a terminal in linux and type...

telnet <host> <port>


After that, drop some functions and/or commands (Instruction Sets)...


We can even make a RESTful style call from a web browser as well...


Now that we've find how to implement a communication channel and pass through any kind of data, such as built-in functions, judo API calls or custom Instruction Set's, let's see how to utilize it in a hardware manner using an http request by following that article...

DVR system by using an IP camera

...or by making a simple .NET application using the TcpClient Class of System.Net.Sockets namespace illustrated by the method bellow...

private string getServerResponse()
{
    Int32 port = 5744;
    String host = "localhost";
    String msg = "%whereami%\r\n"; // Always terminate command with \r\n else will cause a deadlock and socket need to be restarted.

    TcpClient client = new TcpClient(host, port);

    // Translate the passed message into ASCII and store it as a Byte array.
    Byte[] data = System.Text.Encoding.ASCII.GetBytes(msg);

    NetworkStream stream = client.GetStream();

    // Send the message to the connected TcpServer.
    stream.Write(data, 0, data.Length);

    // Buffer to store the response bytes.
    data = new Byte[16 * 1024];

    // Read the first batch of the TcpServer response bytes.
    Int32 bytes = stream.Read(data, 0, data.Length);

    // Close everything.
    stream.Close();
    client.Close();

    return System.Text.Encoding.ASCII.GetString(data, 0, bytes);
}


Sample Console application...


Sample ASP.NET web application...

No comments:

Post a Comment