Page 1 of 1

How to check socket binding is done or not?

Posted: Sun Mar 15, 2020 9:57 am
by cemox
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.

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

Posted: Sun Mar 15, 2020 3:26 pm
by tve
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?

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

Posted: Tue Apr 28, 2020 11:15 am
by cemox
Sorry, I forgot to tell. It is ESP8266 (Lolin) board. Thanks for your reply. I solved it by calling the connect() function a couple of times repeatedly, it works somehow.