Socket programming in python counter not working
Question: I am making a client and a sever relation ship using python. The client has a
button in which I click the button it will connect to the server and count the
clicked button by the client.

But in my situation, the server only count once and the client button is not
working anymore. does anybody has an idea about my case? any help will be
appreciated, thanks in advance.

this is my code

client.py

    
    
    import socket 
    from Tkinter import*
    
    root = Tk()
    
    root.title("ADKOO")
    root.geometry("150x80")
    
    s = socket.socket()         # Create a socket object
    host = socket.gethostname() # Get local machine name
    port = 12340
    s.connect((host, port))
    
    def counterPlus():
      s.send('sent by '+host)
    
    
    app = Frame(root)
    app.grid()
    
    button1 = Button(app, text="+", width=15, command=counterPlus)
    
    button1.grid()
    root.mainloop()
    

server.py

    
    
    import socket
    
    s = socket.socket()
    host = socket.gethostname()
    port = 12340
    s.bind((host, port))
    
    s.listen(5)
    pressed = 0
    while True:
     c, addr = s.accept()
     pressed = pressed + 1
     print 'Got connection from', addr
     print c.recv(1024), 'pressed count', pressed
     #c.close()
    


Answer: `server.py` accept a client, then receive only once, and accept another
client....

You need loop to continuous receive data from client.

Try following code:

    
    
    import socket
    
    s = socket.socket()
    host = socket.gethostname()
    port = 12340
    s.bind((host, port))
    
    s.listen(5)
    while True:
        c, addr = s.accept()
        pressed = 0
        while True:
            print 'Got connection from', addr
            data = c.recv(1024)
            if not data:
                break
            pressed = pressed + 1
            print data, 'pressed count', pressed
    

**NOTE**

Your code and this code does handle one client at a time.

To handle multiple client, you need multiprocess / multithreading / select /
...

