简体   繁体   中英

client disconnects when he sends message python

I'm making a extra function for my chat program which allows you to register (it saves your wished name with your peername). the idea is that when someone sends a message, the host (script) first checks if you have sent the register command (-set --name) or not. If you have it asks you for your wished username and saves that in a .txt (names.txt) file with your peername (ex. 57883:jack) and then sends a message to the other clients that 57883 has changed his name to jack.

if you haven't sent the command the host (script) checks to see if you've registered by looking in the names.txt file for your peername, and when it has found it, it gets the name behind the ':' and sends that together with the message to the other clients (like this: Hello lads, how're ye doin'?)

else:
   try:
       data = sock.recv(RECV_BUFFER)
       if data:
           for o,a in data:
               if o in ("-set", "--name"):
                   name = raw_input('name: ')
                   peer2 = sock.getpeername()
                   with open('names.txt', 'a') as f:
                       f.write(name + ':' + peer2)
                       f.write('\n')
                       f.close()
                   broadcast_data(sock,'set name to: ' + name)
               else:
                   peer = sock.getpeername()
                   with open('names.txt', 'r') as d:
                       lines = d.readlines()
                       for i, peer in enumerate(f):
                           numb, name = peer.rstrip(":")
                           print i, numb, name
                   broadcast_data(sock,'<' + name + '> ' + data)
   except:
       broadcast_data(sock, "Client (%s, %s) is offline" % addr)
       print "Client (%s, %s) is offline" % addr
       sock.close()
       CONNECTION_LIST.remove(sock)
       continue

This is what i have so far, but it doesnt work.. the client connects finely with the host, but when the client sends a message he disconnects. why does he fail to try?

else:
   try:
       data = sock.recv(RECV_BUFFER)
       if data:
           broadcast_data(sock,'<' + str(sock.getpeername()) + '> ' + data)
   except:
       broadcast_data(sock, "Client (%s, %s) is offline" % addr)
       print "Client (%s, %s) is offline" % addr
       sock.close()
       CONNECTION_LIST.remove(sock)
       continue

this was the code before i tried to add the extra function. cheers!

I might be wrong, but doesn't the line:

name = raw_input('name: ')

work on the host computer, printing "name:" and expecting the user to type a line of input on the console? Nothing else is going to happen while the user types and presses enter.

Also, the line:

               for i, peer in enumerate(f):

Is f in scope here? It appears you are expecting it to return an integer and a socket. The only other visible use of f is in the file open/close for -set and --name. Do you mean enumerate(lines)?

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