Tkinter createfilehandler with socket not working
Question: I found a working example for Tkinter createfilehandler with socket in Chapter
18 from "Python and Tkinter Programming" (of John Grayson) client_server.py

I have tested it on Linux Mint 14.04, with python 2.7.6, and the program seems
to work without errors, but ...

The problem is that: server sends date and hour, but client does not display
this. I do not understand why. Can you help me to understand the problem?

Here is the code:

    
    
    from Tkinter import *
    import sys, socket, time
    
    class Server:
        def __init__(self):
            host = 'localhost' #socket.gethostbyname(socket.gethostname())
            addr = host, 18000
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.bind(addr)
            while 1:
                time.sleep(5)
                print time.asctime(time.localtime(time.time()))
                s.sendto(time.asctime(time.localtime(time.time())), addr)
    
    class GUIClient:
        def __init__(self, master=None):
            self.master = master
            self.master.title('Time Service Client')
            self.frame = Frame(master, relief=RAISED, borderwidth=2)
            self.text = Text(self.frame, height=26, width=50)
            self.scroll = Scrollbar(self.frame, command=self.text.yview)
            self.text.configure(yscrollcommand=self.scroll.set)
            self.text.pack(side=LEFT)
            self.scroll.pack(side=RIGHT, fill=Y)
            self.frame.pack(padx=4, pady=4)
            Button(master, text='Close', command=self.master.quit).pack(side=TOP)
    
            self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            self.socket.bind(('localhost', 18000))
    
            self.master.createfilehandler(self.socket, READABLE, self.ihandler)
    
            self.master.after(5000, self.doMark)
    
        def ihandler(self, sock, mask):
            data, addr = sock.recvfrom(256)
            self.text.insert(END, '%s\n' % data)
    
        def doMark(self):
            self.text.insert(END, 'waiting...\n')
            self.master.after(5000, self.doMark)
    
    if len(sys.argv) < 2:
        print 'select -s (server) or -c (client)'
        sys.exit(2)
    if sys.argv[1] == '-s':
        server=Server()
    elif sys.argv[1] == '-c':
        root = Tk()
        root.option_readfile('optionDB')
        example = GUIClient(root)
        root.mainloop() 
    

I have modified code to work without problems.


Answer: The host variable in `Server.__init__` is quite likely different than IP
address `127.0.0.1` used by `GUIClient`

    
    
    class Server:
        def __init__(self):
            host = socket.gethostbyname(socket.gethostname())
            ...
            # addr probably != 127.0.0.1
            s.sendto(time.asctime(time.localtime(time.time())), addr)
    
    class GUIClient:
        def __init__(self, master=None):
             ...
            self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            # change port if necessary
            self.socket.bind(('127.0.0.1', 18000))
    

