简体   繁体   中英

Why does my server print endless \n when a client disconnects?

I am in the process of creating a server but I have hit wall and can't seem to climb over it...

The current code I have works very well until a user disconnects from the server. An endless amount of \\n are printed and I can not seem to figure out why.

I do know that the problem lies in the recv function. I believe it has something to do with my threading but I just can not seem to figure out what it is.

Here is the code, I would be very grateful if someone could help me out.

def recv(self, obj, addr, s):
    while True:
        try:print obj.recv(1024)
        except:pass

def connect(self, port):
    s = socket.socket()
    s.bind(('', port))
    s.listen(1)
    self.clearscreen()

    print "Waiting for connections..."
    while True:
        obj, addr = s.accept()
        verify = obj.recv(1024)
        if verify == "ea25364e2dab91b40ae4f73163854b5d":
            print "\n"+str(addr) + " has connected.\n "
            self.conns[addr] = obj
            try:
                Thread(target=self.handle, args=(obj, addr, s)).start()
                Thread(target=self.recv, args=(obj, addr, s)).start()
            except:pass
        else:pass

因为在调用recv()时从不检查EOS,所以永远不会检测到断开连接。

Okay, it looks like I have figured it out.

  def recv(self, obj, addr, s):
      while True:
          try:a = obj.recv(1024)
          except:pass
          if not p: return 1
          else: print a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM