2023年3月6日月曜日

Sockets not releasing for Pico_W

 https://github.com/micropython/micropython/issues/10812

の方と同様の現象発生で、昨日悩まされてました。

You need to close the waiting (bound on port 80) socket, otherwise you won't be able to make a new one on the same port.

しょうがないので、とりあえず、bind(addr)を正常終了までリトライするコード

に変えてみました。

以下、コード例

....

# Open socket

addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]

s = socket.socket()

##s.bind(addr)

bs="addr"

ec=0

while bs!=None:

  try:

    bs=s.bind(addr)

  except OSError:

    ec=ec+1

    print("EADDRINUSE?? " + str(ec))

  utime.sleep(1)

  print('s.bind(addr) rt ', bs)

s.listen(1)

print('listening on', addr)

....

とりあえず、

最悪でも、2分以内に、bind出来た。。