# Opening Sockets to TCP devices using scripting

**URL:** <https://forum.inductiveautomation.com/t/opening-sockets-to-tcp-devices-using-scripting/76860>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [July 25, 2023, 1:03pm UTC](https://forum.inductiveautomation.com/t/opening-sockets-to-tcp-devices-using-scripting/76860 "2023-07-25T13:03:41Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![bmeyers](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmeyers/32/37453_2.png) [@bmeyers](https://forum.inductiveautomation.com/u/bmeyers)\
**Post date:** [July 26, 2023, 3:22pm UTC](https://forum.inductiveautomation.com/t/opening-sockets-to-tcp-devices-using-scripting/76860/12 "2023-07-26T15:22:41Z")

</div>

That is funny. I was having ChatGPT do the same thing. I don't know enough about Java though to say if this is particularly good code.

```auto
# Import the Java Socket class from the java.net package
from java.net import Socket, InetAddress

TCP_IP = '192.168.1.100'
TCP_PORT = 17001
CONNECTION_TIMEOUT = 5000 # Milliseconds (5 seconds)

try:
    # Create a new Java Socket instance
    s = Socket()
    s.connect(InetAddress.getByName(TCP_IP), TCP_PORT, CONNECTION_TIMEOUT)

    message = 'this is a text message'
    message_bytes = message.encode('utf-8') # Convert the string to bytes with UTF-8 encoding
    s.getOutputStream().write(message_bytes) # Send the bytes through the socket's output stream
    s.getOutputStream().flush() # Ensure the data is sent immediately

except Exception as e:
    print(f"Error: {e}")

finally:
    s.close() # Close the socket after use
```

What kind of failure(s) are we talking about?

---

_[View the full topic](https://forum.inductiveautomation.com/t/opening-sockets-to-tcp-devices-using-scripting/76860)._
