Welcome to the Pycom forum Forum
    • \[\[global:header.categories\]\]
    • \[\[global:header.recent\]\]
    • \[\[global:header.tags\]\]
    • \[\[global:header.popular\]\]
    • Login

    Explore Pybytes | Official Documentation | Report a Firmware Bug/Issue | GitHub

    Socket won't unbind from address when socket is terminated

    Scheduled Pinned Locked Moved WiPy 2&3
    3 Posts 2 Posters 48 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K Offline
      Karin Layton
      last edited by

      After my successive failures using bluetooth to get my wipy's to communicate, I switched to using wifi and sockets. It does work, however using basically example code from the documentation I cannot soft reset and rerun the module without getting the error (from server wipy):
      OSError: [Errno 112] EADDRINUSE

      code used:
      client:

      def socket_thread(p):
          sconn =True
          while sconn:
              # Wait for any action to happen infinitely
              l = p.poll()
      
              # Start processing the actions happened
              for t in l:
                  # First element of the returned tuple is the socket itself
                  sock = t[0]
                  # Second element of the returned tuple is the events happened
                  event = t[1]
      
                  # If any error or connection drop happened close the socket
                  if(event & uselect.POLLERR or event & uselect.POLLHUP):
                      sock.close()
                      sconn = False
                      continue
      
                  # If the socket is writable then send some data
                  # The socket becomes writable if the connect() was successfull
                  if(event & uselect.POLLOUT):
                      # If any error occurs during sending here, do "nothing", poll() will return with error event, close the socket there
                      try:
                          sock.send("Data to send")
                          # We only want to send one message on this socket, in the future wait only for new incoming messages
                          p.modify(sock, uselect.POLLIN | uselect.POLLHUP | uselect.POLLERR)
                      except:
                          pass
      
      
                  # If any new data is received then get it
                  if(event & uselect.POLLIN):
                      # If any error occurs during receiving here, do "nothing", poll() will return with error event, close the socket there
                      try:
                          r = sock.recv(12)
                          # If recv() returns with 0 the other end closed the connection
                          if len(r) == 0:
                              sock.close()
                              sconn = False
                              continue
                          else:
                              # Do something with the received data...
                              print("Data received: " + str(r))
                      except:
                          pass
      def makesockets():
          telem = socket.socket()
          telem.setblocking(False)
      
          # Create a new poll object
          p = uselect.poll()
          # Register the sockets into the poll object, wait for all kind of events
          p.register(telem, uselect.POLLIN | uselect.POLLOUT | uselect.POLLHUP | uselect.POLLERR)
          try:
              telem.connect(socket.getaddrinfo("192.168.4.1", 1235)[0][-1])
              print('socket created')
          except OSError as e:
                  if e.args[0] != uerrno.EINPROGRESS:
                      raise
          _thread.start_new_thread(socket_thread, (p,))
      

      server:

      def client_thread(clientsocket):
          # Receive maxium of 12 bytes from the client
          r = clientsocket.recv(12)
      
          # If recv() returns with 0 the other end closed the connection
          if len(r) == 0:
              clientsocket.close()
              return
          else:
              # Do something wth the received data...
              print("Received: {}".format(str(r)))
      
          # Sends back some data
          clientsocket.send('nice')
      
          # Close the socket and terminate the thread
          clientsocket.close()
      
      # Set up server socket
      def createsocket():
          serversocket = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
          serversocket.setsockopt(usocket.SOL_SOCKET, usocket.SO_REUSEADDR, 1)
          serversocket.bind(("192.168.4.1", 1235))
          serversocket.settimeout(100)
          serversocket.listen(1)
          (clientsocket, address) = serversocket.accept()
          _thread.start_new_thread(client_thread, (clientsocket,))
      
      
      1 Reply Last reply Reply Quote 0
      • G Offline
        Gijs
        last edited by

        You can re-initialize the socket after closing it by calling telem= socket.socket() again. This should prevent the error you are getting

        1 Reply Last reply Reply Quote 0
        • K Offline
          Karin Layton
          last edited by

          I tried adding that but it was unsuccessful- however, the repl is staying open (not giving me a >>>) on my server after receiving a message, so it isn't finishing out whatever it's stuck open doing I think. I don't see any reason why it shouldn't finish out though? Doesn't the clientsocket.close() kill the thread?

          1 Reply Last reply Reply Quote 0

          Hello! It looks like you're interested in this conversation, but you don't have an account yet.

          Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

          With your input, this post could be even better 💗

          Register Login
          • 1 / 1
          • First post
            Last post

          Pycom on Twitter

          Pycom