# Send/receive message via TCP socket

**URL:** <https://forum.inductiveautomation.com/t/send-receive-message-via-tcp-socket/96240>\
**Category:** Ignition\
**Tags:** scripting\
**Created:** [September 20, 2024, 3:21pm UTC](https://forum.inductiveautomation.com/t/send-receive-message-via-tcp-socket/96240 "2024-09-20T15:21:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![antonio\_graham](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@antonio\_graham](https://forum.inductiveautomation.com/u/antonio_graham)\
**Post date:** [September 20, 2024, 3:21pm UTC](https://forum.inductiveautomation.com/t/send-receive-message-via-tcp-socket/96240/1 "2024-09-20T15:21:16Z")

</div>

Hello I'm attempting to send ASCII messages to a thermotron8800 controller via the script console calling the following gateway script. I keep getting a 'failed to connect connection refused error when testing the socket connection. I'm thinking I'll need to use a different socket for communication or is there a better way to send/receive status messages from Ignition to the controller?

def test\_connection(ip, port):

```
import socket

try:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((ip, port))
    status = "Connection successful"
except socket.error as e:
    status = "Failed to connect: {0}".format(e)
finally:
    sock.close()
    
return status

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 20, 2024, 3:26pm UTC](https://forum.inductiveautomation.com/t/send-receive-message-via-tcp-socket/96240/2 "2024-09-20T15:26:01Z")

</div>

A 'connection refused' error indicates the error is entirely on the side of whatever you're trying to connect to. Either the controller itself or something else on the network in between is rejecting the attempt to connect from Ignition.

I would also generally recommend using Java's socket infrastructure over the Jython standard library.

---

<div class="post-metadata">

**Author:** ![drewdin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/drewdin/32/3363_2.png) [@drewdin](https://forum.inductiveautomation.com/u/drewdin)\
**Post date:** [June 5, 2025, 9:04pm UTC](https://forum.inductiveautomation.com/t/send-receive-message-via-tcp-socket/96240/3 "2025-06-05T21:04:19Z")

</div>

> [@antonio\_graham](#):
>
> ```python
> import socket
> 
> try:
> sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> sock.connect((ip, port))
> status = "Connection successful"
> except socket.error as e:
> status = "Failed to connect: {0}".format(e)
> finally:
> sock.close()
>     
> return status
> 
> ```

I know this is an old post but any reason you didnt use the tcp/udp driver and decided to use python? Just curious.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [June 5, 2025, 9:06pm UTC](https://forum.inductiveautomation.com/t/send-receive-message-via-tcp-socket/96240/4 "2025-06-05T21:06:24Z")

</div>

The TCP/UDP drivers are...basically, pretty bad. Among other significant issues, they're incapable of sending/receiving arbitrary raw binary payloads, which is fundamental to the vast majority of low level protocols.

---

<div class="post-metadata">

**Author:** ![drewdin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/drewdin/32/3363_2.png) [@drewdin](https://forum.inductiveautomation.com/u/drewdin)\
**Post date:** [June 5, 2025, 9:20pm UTC](https://forum.inductiveautomation.com/t/send-receive-message-via-tcp-socket/96240/5 "2025-06-05T21:20:20Z")

</div>

OK cool, I'll go the python route then! Thanks for the info

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [June 5, 2025, 9:47pm UTC](https://forum.inductiveautomation.com/t/send-receive-message-via-tcp-socket/96240/6 "2025-06-05T21:47:09Z")

</div>

Consider using `java.net`'s socket class instead of python's, harder to footgun

---

<div class="post-metadata">

**Author:** ![drewdin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/drewdin/32/3363_2.png) [@drewdin](https://forum.inductiveautomation.com/u/drewdin)\
**Post date:** [June 6, 2025, 12:34am UTC](https://forum.inductiveautomation.com/t/send-receive-message-via-tcp-socket/96240/7 "2025-06-06T00:34:56Z")

</div>

Thanks, any links to some docs?

---

<div class="post-metadata">

**Author:** ![ryan.white](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ryan.white/32/73193_2.png) [@ryan.white](https://forum.inductiveautomation.com/u/ryan.white)\
**Post date:** [June 6, 2025, 1:00am UTC](https://forum.inductiveautomation.com/t/send-receive-message-via-tcp-socket/96240/8 "2025-06-06T01:00:59Z")

</div>

[javadocs](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/Socket.html)

Some other posts that may provide insight:

> [@Communicating with external applications over TCP/IP socket /websockets using gateway scripts](https://forum.inductiveautomation.com/t/communicating-with-external-applications-over-tcp-ip-socket-websockets-using-gateway-scripts/20455/10):
>
> Just call all the java stuff from jython. Use jython’s NetBeans shorthand where you can. Something like this (ultra-simplified): from java.net import InetSocketAddress from java.nio import ByteBuffer from java.nio.channels import SocketChannel from java.nio.charset import StandardCharsets def opener(target): parts = target.rsplit(':', 1) if len(parts)\>1: print "Socket Address %s:%s" % (parts[0], parts[1]) sa = InetSocketAddress(parts[0], int(parts[1])) else: print "Socket Address %s…

> [@TCP/IP Client Service](https://forum.inductiveautomation.com/t/tcp-ip-client-service/55118/4):
>
> Something like this: 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 th…
