TCP Listener by Socket Scripting

Hi all,

Is there anyone who is familiar with using script to create TCP-IP listener (socket) in Ignition? I found out some scripting related to sending message in Ignition through TCP-IP (socket) which look like this:

import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) HOST = '127.0.0.1' PORT = 184 s.connect((HOST, PORT)) s.send('blabla') s.close()

But I wonder how do can I create listener for TCP-IP by scripting?

Thanks!

This post discusses how to do exactly what you need. Make sure you read right to the end to get the final working solution!

Hi AlThePal,

I read the post and found what is needed. Thanks. Just want to clarify few things.

  1. In my log I get:

Received 68657921 from None
Received 00 from None
Received 00 from None
Received 00 from None
Received 00 from None
Received 00 from None
Received 00 from None
Received 00 from None
Received 00 from None
Received 041241 from None
Received 00 from None

I notice that, unlike yours, the “from” in my log is “None”

This post explains the difference between bind() (which will be needed for server) and connect(). Since in my Ignition application is a client, I do not use bind().

So, from my understanding (who is not so familiar with Python, much less Jython :frowning: ), it seems that the “from” comes from bind(). Without which, we cannot know the sender (aka “from”). Do I understand correctly?

  1. The “poll” time for the invokeAsynchronous seems to be once every second. Is there a way to change the timing? Alternatively, is there a way to make the call event-based instead of time-based?

Many thanks! :smiley:

My apologize, please forget my second question:

I just realized that this is caused by the server application I used which has a setting to send its “heartbeat” (in the form of \0) periodically as reflected in my log:

[quote]Received 68657921 from None
Received 00 from None
Received 00 from None
Received 00 from None
Received 00 from None
Received 00 from None
Received 00 from None
Received 00 from None
Received 00 from None
Received 041241 from None
Received 00 from None[/quote]

This has nothing to do with time-based poll. The async listener itself is already an event-based. :blush:

Hi Ian,

I think the terms ‘Client’ and ‘Server’ are misleading in this case, where one side sends out data without being asked and the other just waits to receive them. I think it is more helpful to describe them as ‘Talkers’ and ‘Listeners’, with the Talker needing to use connect and the Listener needing to use bind.

As far as I can tell, you are sending data from your device and want to get it into Ignition. This means your device is the Talker and Ignition is the Listener, so Ignition should be using bind. Remember that unless you are utilising the reliable connection of TCP, you could use UDP to broadcast the data - this means your Talker won’t use connect at all.

Ok. I get it, I believe.

Many thanks! :smiley: