How to check socket binding is done or not?

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
User avatar
cemox
Posts: 16
Joined: Mon Oct 08, 2018 5:31 pm
Location: Turkey

How to check socket binding is done or not?

Post by cemox » Sun Mar 15, 2020 9:57 am

Hi,
I have a function that creates a socket for UDP connection and it works fine.

Code: Select all

def connect():
    conn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    # server_address = (ip, port)
    print ('starting up on %s port %s' % server_address)
    conn.bind(server_address)
    conn.settimeout(0)
    # conn.settimeout(None)
    return(conn)
But when the program start, right after connect() function, another function which receives from the socked is called. I noticed that if there is not enough time period between the two calls, connect() function returns None and the program crashes. I put some delays between the two calls, but I want to have more robust code. As far as I know, there is no method like isConnected() in usocket library. How can I hold the program execution until the socket binding is ready?

When I check what Connect() function returns I see something like this:

Code: Select all

<socket state=0 timeout=0 incoming=3fff99d0 off=0>
When there is not a successful connection it returns "incoming=None". Is there way of manipulating this returned statement? (I do not know what type of data is this).

Thanks.

User avatar
tve
Posts: 91
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: How to check socket binding is done or not?

Post by tve » Sun Mar 15, 2020 3:26 pm

Which platform?
I don't understand the failure timeline you're describing, you seem to be saying:
1. you call connect()
2. then you can another function which receives from the socket
3. then connect() return None and the program crashes
Do you call connect() twice, or how can connect return None in step 3 when it already completed in step 1?

Post Reply