Avatar of Konstantin Krolikov
Konstantin Krolikov
Flag for Israel asked on

socket connect loop - pytohn

hello experts,
i created a simple chat server using socket in python.

my problem is that i want the client to try connect to the server until the connection is made..(when i set the timeout of the socket)

however, when the connection fail(timeout), i did another request but it done immediately.
here is the code:
from socket import *
s = socket(AF_INET, SOCK_STREAM)
connected = False
    while connected != True:
            s.connect(('localhost', 12345))
            connected = True
        except error:
            print "could not connected to server now.. Tries to connect again.."

#rest of the code...

Open in new window

is there any way to reset the timeout or i have to close that socket and open new one..

by the way, only when i set the timeout of the socket it doesn't work, if i don't set the timeout of the socket, it works..

thanks ahead..
PythonProgramming Languages-Other

Avatar of undefined
Last Comment
Konstantin Krolikov

8/22/2022 - Mon

don't do it yourself, use select http://docs.python.org/library/select.html

recvSockHost = ''
recvSockPort = 10001

recvSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
recvSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
recvSockId = recvSock.fileno()

inputReady, _, _ = select.select([recvSock], [], [], .01)
    for activeSocket in inputReady:
        if activeSocket == recvSock:

Open in new window

Konstantin Krolikov

but how can i do it myself?
i want to know this..

Understandable, but when the wheel has been invented several times (with much better results)  why do you want to make a square wheel?
Your help has saved me hundreds of hours of internet surfing.
Konstantin Krolikov

hhh :)
ok, but this is the right way.
in this way it will keep ask for connection until it enstablish?

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Konstantin Krolikov