Thursday 15 January 2015

sockets - How to detect disconnection in python, without sending data -


OK, I have a socket and I'm handling a line at a time and logging it. The code below works very well for this in the COAT function I use to send the data to the log. I use this for loop, so I can process a line at a time.

  socket.connect ((host, port)) readbuffer = "" while true: readbuffer = readbuffer + socket.recv (4096). Decode ("UTF-8") COAT ("Receive:" + ReidBuffer) # Buffer Tipe = Strip: (Reidbuffer, "\ n") readbuffer = temp.pop () for the line in temporarily: # one at a time Handle the line.  

I ran into a problem where the server disconnected me, suddenly I had a huge file full of the word "RECEIVING". I know this because when a dragon socket is disconnected, it starts getting blank data.

I have tried to insert:

  if "" == readbuffer: print ("it is disconnected!") Break  

All who immediately broke the loop, and say that it has also been disconnected on a successful connection.

I also know that I can find out the disconnection by sending data, but I can not do this, because whatever I send is broadcast to all other customers on the server, and This is for debugging those customers, so I will interfere with it.

Thank you in advance what I do.

You have to check the result of recv ()

  while true: c = socket.recv (4096) if c == '': break # no more readbuffer = readbuffer + c.decode ("UTF-8") ...  

No comments:

Post a Comment