ConnectPort X4 "Connection timed out"

I encounter a bug about once every week where the Digi Connectport X4 socket closes due to ‘Connection timed out’ when sending data to a server.

I then have to restart the gateway for it to run correctly again. This usually happens when the server is shut down or the network is down however, everything seemed to be running correctly this time.

Is there a way to set a python timeout on the “post to server” function so that it does not cause the program to crash?

You need to put your code in a try block and handle the exception. You can also open the socket with a timeout. see attachment.

Keith

# TIMEOUT SOCKETS test example
#
# learning how non-blocking TCP sockets work on a connection attempt.  the goal here is to find a method to try to make a connection and then gracefully timeout and tear down the socket at a resonable later time without bringing the whole script down.  
# Items of note:
# 1) It appears that the TCP layer will give up anyways after about 15 seconds.
# 2) make sure to use settimeout() before trying a connect on a nonblocking socket.
# 3) the try except will handle the timeout.

from socket import *
from select import *

remoteLoggerPort = 2101  
remoteLoggerIP = '10.4.112.16'

# setup outbound TCP connection to server
remoteLoggerSocket = socket(AF_INET, SOCK_STREAM)
#remoteLoggerSocket.setblocking(0)
remoteLoggerSocket.settimeout(5)

try:
    remoteLoggerSocket.connect((remoteLoggerIP, remoteLoggerPort))
    remoteLoggerSocket.send("TEST1234")
    remoteLoggerSocket.close()

except Exception, e:
    print "Exception: closing socket"
    remoteLoggerSocket.close()



print "program terminated"