Page 1 of 1

socket problem

Posted: Thu Nov 09, 2017 5:29 pm
by Emil
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()

Re: socket problem

Posted: Thu Nov 09, 2017 5:51 pm
by Drako
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...

Re: socket problem

Posted: Thu Nov 09, 2017 6:13 pm
by pythoncoder
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.

Re: socket problem

Posted: Fri Nov 10, 2017 10:27 pm
by Emil
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.

Re: socket problem

Posted: Sat Nov 11, 2017 9:01 am
by pythoncoder
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.