Problem when Socket is closed

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Twistx77
Posts: 6
Joined: Sat Nov 05, 2016 4:59 pm

Problem when Socket is closed

Post by Twistx77 » Thu Dec 15, 2016 3:01 pm

Hi,

I'm trying to make the following code work after being connected to a wlan:

Code: Select all

def openSocket():
    s = socket.socket()
    s.bind( ("", 8266) )
    s.listen(1)
    cliente, a = s.accept() 


    while True:    
        try:
            data = cliente.recv(1) 
        except:
            print ("Error")
            break           
        
        print (data[0])

        if (data[0] == 49):
            led.value(0)

        if (data[0] == 50)
            led.value(1)
while True:
openSocket();

It works as expected, the problem comes when once a client has been connected and disconnects I get:

Traceback (most recent call last):
File "main.py", line 57, in <module>
File "main.py", line 48, in openSocket.
IndexError: Bytes index out of range

when the ESP tries to receive data from the cliente in line data = cliente.recv(1). I understand that the error is caused because there is no socket now that has been closed but I don't know how to handle it to be able to detect that the client has disconnected and then avoid using the recv method.

Can anyone give me a hint in how I could solve this issue?

Thank you.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Problem when Socket is closed

Post by dhylands » Thu Dec 15, 2016 4:47 pm

part of the issue will be that you're getting a zero length data object back and not checking the length before trying to access elements within data.

Twistx77
Posts: 6
Joined: Sat Nov 05, 2016 4:59 pm

Re: Problem when Socket is closed

Post by Twistx77 » Thu Dec 15, 2016 5:20 pm

Sure. But how can I check the length and how can I discern between getting a 0 length because I havent received anything and the socket being disconnected.

Si there any way to check if the socket is still open?

Thanks.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Problem when Socket is closed

Post by dhylands » Thu Dec 15, 2016 7:02 pm

Twistx77 wrote:Sure. But how can I check the length

Code: Select all

if len(data) > 0:
and how can I discern between getting a 0 length because I havent received anything and the socket being disconnected.
My understanding is you'll get a zero length buffer when the other end does an orderly shutdown, and you'll get an exception raised for any errors. If you have a non-blocking socket and no data is available, then the recv return an EAGAIN error (which will manifest itself as an exception in python).
Si there any way to check if the socket is still open?
I'm not sure.

tigren
Posts: 1
Joined: Mon Dec 19, 2016 2:19 am

Re: Problem when Socket is closed

Post by tigren » Mon Dec 19, 2016 2:22 am

[quote="dhylands"]

[quote="Twistx77"]
[quote]Si there any way to check if the socket is still open?[/quote]

I'm not sure.[/quote]

According to the official python docs on sockets (https://docs.python.org/3/howto/sockets.html):

When a recv returns 0 bytes, it means the other side has closed (or is in the process of closing) the connection. You will not receive any more data on this connection. Ever.

Post Reply