Link to home
Start Free TrialLog in
Avatar of Konstantin Krolikov
Konstantin KrolikovFlag 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)
s.settimeout(10)
connected = False
    while connected != True:
        try:
            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..
Avatar of rodaddy
rodaddy
Flag of United States of America image

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

recvSockHost = '224.110.0.1'
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:
           DoStuff()

Open in new window

Avatar of Konstantin Krolikov

ASKER

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?
hhh :)
ok, but this is the right way.
in this way it will keep ask for connection until it enstablish?
ASKER CERTIFIED SOLUTION
Avatar of rodaddy
rodaddy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks