# TCP/IP Client Service

**URL:** <https://forum.inductiveautomation.com/t/tcp-ip-client-service/55118>\
**Category:** Ignition\
**Created:** [January 18, 2022, 4:14pm UTC](https://forum.inductiveautomation.com/t/tcp-ip-client-service/55118 "2022-01-18T16:14:47Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [January 19, 2022, 5:58pm UTC](https://forum.inductiveautomation.com/t/tcp-ip-client-service/55118/4 "2022-01-19T17:58:13Z")

</div>

Something like this:

```auto
from java.lang import Thread
from java.util.concurrent import ConcurrentHashMap

g = system.util.persistent("long-lived-thread")
# The .setdefault method will not replace an existing key, and
# is thread-safe (concurrent).
chm = g.setdefault('chm', ConcurrentHashMap())

tagpath = '[default]path/to/boolean/tag'

# Rely on closures/globals to make above available in the thread
def liveLongAndProsper():
	me = Thread.currentThread()
	# A ConcurrentHashMap's .put() method has the handy behavior of
	# atomically replacing and **returning** the previous value for
	# the given key.
	priorThread = chm.put('running', me)

	# If something was already running, interrupt it and join it.
	if priorThread:
		priorThread.interrupt()
		priorThread.join()

	# Not quite an infinite loop
	while not Thread.interrupted():
		v = system.tag.read(tagpath).value
		system.tag.writeSynchronous(tagpath, not v)
		# Sleep is only appropriate in a dedicated thread like this one
		Thread.sleep(5000)

system.util.invokeAsynchronous(liveLongAndProsper)

```

So this turns on when anything references its script module. And lives “forever”, or a successor kills it. You can turn it off with this:

```auto
priorThread = someScriptModule.chm.get('running')
if priorThread:
	priorThread.interrupt()

```

It won’t start on an inactive gateway, but won’t die. You’ll want the loop to check the gateway redundancy status if that’s important.

---

_[View the full topic](https://forum.inductiveautomation.com/t/tcp-ip-client-service/55118)._
