Opening Sockets to TCP devices using scripting

Yes. It's unfortunate, but you're just setting yourself up for failure if you use socket directly.

Chat-GPT translated java.net.Socket equivalent of your snippet, that looks approximately correct:

# Jython Client using java.net.Socket

import java.net.Socket
import java.io.IOException

TCP_IP = '192.168.1.100'
TCP_PORT = 17001
CONNECTION_TIMEOUT = 5000  # The timeout is specified in milliseconds

try:
    s = Socket(TCP_IP, TCP_PORT)
    s.setSoTimeout(CONNECTION_TIMEOUT)

    out_stream = s.getOutputStream()
    message = 'this is a text message'
    out_stream.write(message.encode('utf-8'))

    s.close()

except IOException as e:
    print("Error:", e)
except Exception as e:
    print("Error:", e)
2 Likes