Python Tcp Disconnect Detection

I have a simpletcp example:

import socket
import time


TCP_IP = '127.0.0.1'
TCP_PORT = 81
BUFFER_SIZE = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))

while True:
    s.send(bytes('hello', 'UTF-8'))
    time.sleep(1)

s.close()

How can I detect if I have lost my connection to the server and how can I reconnect?

Do I need to wait for a response to the server?

UPDATE:

import socket
import time

TCP_IP = '127.0.0.1'
TCP_PORT = 81
BUFFER_SIZE = 1024

def reconnect():
    toBreak = False
    while True:
        s.close()
        try:
            s.connect((TCP_IP, TCP_PORT))
            toBreak = True
        except:
            print ("except")        
        if toBreak:
            break
        time.sleep(1)


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))

while True:
    try:    
        s.send(bytes('hello', 'UTF-8'))
        print ("sent hello")
    except socket.error as e:
        reconnect()

    time.sleep(1)

s.close()

If I break the connection, it throws an error (it doesn't really matter), and goes to reconnect the loop. But after reconnecting, the connection returns this error:

OSError: [WinError 10038] An operation was attempted for something that is not a socket

If I restart the script that calls the same s.connect ((TCP_IP, TCP_PORT)), it works fine.

+4
source share
3 answers

socket.error: [Errno 104] Connection reset by peer (aka ECONNRESET) send() recv() . , , :

while True:
    try:
        s.send(bytes('hello', 'UTF-8'))
    except socket.error, e:
        if e.errno == errno.ECONNRESET:
            # Handle disconnection -- close & reopen socket etc.
        else:
            # Other error, re-raise
            raise
    time.sleep(1)
+6

.

def connect():
    while True:
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((host, port))
            return s.makefile('w')
        except socket.error as e:
            log("socket error {} reconnecting".format(e))
            time.sleep(5)

dest = connect()
while True:
    line = p.stdout.readline()
    try:
        dest.write(line)
        dest.flush()
    except socket.error as e:
        log("socket error {} reconnecting".format(e))
        dest = connect()
+1

Can you try this (I think you are not trying to use socket.SO_REUSEADDR):

def open_connection():
    data0=''

    try:
        # Create a TCP/IP socket
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

        # Connect the socket to the port where the server is listening
        server_address = ('192.168.0.100', 8000)
        sock.settimeout(10)     # TimeOut 5 secunde

        while True:

            try:
                sock.connect(server_address)
                message = 'new connection'
                sock.sendall(message)

                # Look for the response
                amount_received = 0
                data0=sock.recv(1024)
                amount_received = len(data0)
                return

            finally:
                wNET = 0
                pass

    except:
        sock.close()
        time.sleep(60)
        del data0
0
source

All Articles