Python UDP sendto() Ignoring Exception Block and Crashing -
i have been trying basic chat application working months in python 2.7 (using geany ide), , got basic application working using udp. can connect , broadcast communications, if client connects , closes later, server crashes next time tries broadcast message of clients. issue crashes without returning error message or traceback can't tell what's happening. here code (it work on windows due getch()
code in client).
server:
from socket import * s = socket(af_inet,sock_dgram) s.bind(("25.150.175.48",65437)) maxsize = 4096 listeners = [] while true: data, addr = s.recvfrom(maxsize) if addr not in listeners: listeners.append(addr) #print addr[1] print "new client added %s on port %i" % (addr[0],addr[1]) l in listeners: try: s.sendto(data,l) except: listeners.remove(l)
client:
from socket import * import select import msvcrt s = socket(af_inet,sock_dgram) s.bind(("25.150.175.48",65438))#change port 65439 second client maxsize = 4096 waitseconds = 0.001 server = ("25.150.175.48",65437) s.sendto(raw_input(">"),server) while true: ready = select.select([s], [], [], waitseconds) if ready[0]: data = s.recv(maxsize) print data else: if msvcrt.kbhit(): char = msvcrt.getch() if char == "\r": s.sendto(raw_input(">"),server)
my theory if can have server recognize when person closes client, can remove client address list of listeners , work fine. unfortunately, it's not working correctly, , don't know how have server send out test packet of sort can use verify connection. examples i've read on internet seem verify port exists. if has idea on how handle this, appreciate help.
edit:
after tooling around bit managed receive traceback running server program in idle. traceback follows:
traceback (most recent call last): file "g:\python\miscellaneous\examples\udp chat server & client\udp server.py", line 7, in <module> data, addr = s.recvfrom(maxsize) error: [errno 10054] existing connection forcibly closed remote host
i need handling this, try/except statement seems fail catch when run (which i'm concerned about; python bug?). after returning traceback program quits. suggestions?
you won't traceback try\except block. unless know you're doing shouldn't ignore errors that.
try: s.sendto(data,l) except exception e: print(e) listeners.remove(l)
as crash it's hard say. udp not connection oriented, client being running or not shouldn't crash server, or stop sending packages.
the way can check if client online, using udp exclusively, sending package , removing list if doesn't respond in period of time.
Comments
Post a Comment