socket problem

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Emil
Posts: 2
Joined: Thu Nov 09, 2017 4:45 pm

socket problem

Post by Emil » Thu Nov 09, 2017 5:29 pm

I'm trying to make a very basic server based on the example here: https://docs.micropython.org/en/latest/ ... k_tcp.html

The code below works as expected the first time I run it. But if I run the same program again I get "OSError: 112" on the line with "s.bind(addr)". Changing the port or doing a hard reset solves this.

I think the problem is that I'm trying to bind a port that is already in use. (?)

Any suggestions for how to solve this? Is there a way to unbind all ports?


import socket
import network

#Code for connecting to wifi left out.

html = "<!DOCTYPE html><html><head><title>Test</title></head><body><h1>Hello World!</h1></body></html>"
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]

s = socket.socket()
s.bind(addr)
s.listen(1)

print('listening on', addr)

while True:
cl, addr = s.accept()
print('client connected from', addr)
cl_file = cl.makefile('rwb', 0)
while True:
line = cl_file.readline()
if not line or line == b'\r\n':
break
response = html
cl.send(response)
cl.close()

Drako
Posts: 12
Joined: Thu Oct 19, 2017 9:28 am

Re: socket problem

Post by Drako » Thu Nov 09, 2017 5:51 pm

I am guessing here but maybe try a socket.close() on the socket instance when you are done?
In your case s.close()
Or try garbage collecting manually by using gc.collect(), or both...

User avatar
pythoncoder
Posts: 5145
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: socket problem

Post by pythoncoder » Thu Nov 09, 2017 6:13 pm

If the program is interrupted with ctrl-C sockets will be left open. This can be fixed with judicious use of try ... finally

Most socket problems seem to result from sockets being left open.

On the ESP8266 RAM is limited so I usually issue a soft reset before re-running any program.
Peter Hinch

Emil
Posts: 2
Joined: Thu Nov 09, 2017 4:45 pm

Re: socket problem

Post by Emil » Fri Nov 10, 2017 10:27 pm

Thank you for your replies.

I have added gc.collect() at the start of the program. The problem with this is that if I stop the program and then rerun it, the socket does not appear to be garbage collected. But if I wait a few minutes before running the program again it does work.

s.close() is fine if I'm done with the scoket and can close it, but I have not figured out how to use it if the program is stoped (ctrl-c).

Should doing a soft reset close open ports? It doesn't appear to do so.

Btw, I'm using an ESP32.

User avatar
pythoncoder
Posts: 5145
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: socket problem

Post by pythoncoder » Sat Nov 11, 2017 9:01 am

Emil wrote:
Fri Nov 10, 2017 10:27 pm
...
s.close() is fine if I'm done with the scoket and can close it, but I have not figured out how to use it if the program is stoped (ctrl-c)...
Try this. It will "lock up" in a loop until you press ctrl-c, when it will print "Close socket".

Code: Select all

try:
    while 1:
        pass
except KeyboardInterrupt:
    pass  # Optionally suppress keyboard interrupt traceback
finally:
    print('Close socket')
Note that the except clause is not necessary - it merely illustrates trapping the ctrl-C interrupt. The finally clause is what does the work and will ensure the socket is closed in the event of any exception including syntax errors, divide by zero or whatever.
Peter Hinch

Post Reply