Author GeorgeY
Recipients GeorgeY, martin.panter
Date 2016-10-15.04:41:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1476506511.45.0.00922630928789.issue28447@psf.upfronthosting.co.za>
In-reply-to
Content
"Without code or something demonstrating the bug, I’m pretty sure it is a bug in your program"

Here is the main Thread
-----------------------

mailbox = queue.Queue()


while True:
    #print(addr_groups)


    unknown_clients=[]
    for key in yellow_page.keys():
        if yellow_page[key][0] ==None:
            unknown_clients.append(key)

    print("\n", name_groups)
    if len(unknown_clients) >0:
        print("unknown from:"+str(unknown_clients))
    print(time.strftime(ISOTIMEFORMAT, time.localtime(time.time())) + '\n')

    # Get the list sockets which are ready to be read through select
    read_sockets, write_sockets, error_sockets = select.select(active_socks,[],[],TIMEOUT)

    for sock in read_sockets:
        #New connection
        if sock ==server_sock:
            # New Client coming in
            clisock, addr = server_sock.accept()  
            ip = addr[0]
            if ip in IPALLOWED:
                active_socks.append(clisock)                
                yellow_page[addr] = [None,None,clisock] 
            else:
                clisock.close()
         
        #Some incoming message from a client
        else:
            # Data recieved from client, process it
            try:
                data = sock.recv(BUFSIZ)
                if data:
                    fromwhere = sock.getpeername()
                    mail_s = data.split(SEG_) 
                    del mail_s[0]
                    for mail_ in mail_s:

                        mail = mail_.decode()                        
                       
            except:
                mailbox.put( (("sock_err",sock), 'localhost') )
                continue
=====================

so the sub thread's job is to analyze the exception put into "mailbox"

Here is the run function of sub thread
-----------------------------------
    def run(self):
        
        while True:
            msg, addr = mailbox.get()  
            if msg[0] =="sock_err":
                print("sock_err @ ", msg[1])  #<<<Here comes the print of socket object
                handle_sock_err(msg[1])
                continue ##jump off
            else: ......
==========

Let us see how the handle_sock_err does to the broken socket:

---------------
def handle_sock_err(sock): #sock是出错的网络连接,要注销它并且提示出错
    global active_socks, yellow_page, addr_groups, name_groups 
    addr_del = sock.getpeername()  #<<<ERROR 10038
    
    name_del, job_del = yellow_page[addr_del][ 0:2] 
    yellow_page.pop(addr_del)
    
    tag = 0
    try:

        addr_groups[job_del].remove(addr_del);   tag =1
        name_groups[job_del].remove(name_del);   tag =2
        
        active_socks.remove(sock) 
        tag =3

        print(name_del+" offline!")

    except:
        if tag <3:
            active_socks.remove(sock)
        else:
            pass

=============

I do believe that the broken socket can tell me the address it connected to, so there is even no "try" in getpeername()

Why do I need to find the address of that broken socket found by select in main?
Simple, the server recognizes the user name once the connection has sent correct login information. When the connection is down, the user shall be automatically removed from online user list "yellow_page" and all other dynamic books like "addr_groups", "name_groups"...

This is a very common and reasonable practice of online system. I am not particularly interested in why getpeername() is ineffective in getting the address stopped connection,

but How I get the address that stopped connection.

I do not know why python can only tell me a line has broke, but where it was leading to. And I believe this is a big issue in establishing an effective server, do you agree with me?
History
Date User Action Args
2016-10-15 04:41:51GeorgeYsetrecipients: + GeorgeY, martin.panter
2016-10-15 04:41:51GeorgeYsetmessageid: <1476506511.45.0.00922630928789.issue28447@psf.upfronthosting.co.za>
2016-10-15 04:41:51GeorgeYlinkissue28447 messages
2016-10-15 04:41:50GeorgeYcreate