Simple Python chat app with sockets -
i'm trying make simple client/server chat app in python sockets, , turn networked game of rock, paper, scissors.
i found guide online create client/server i'm having trouble modifying loops each script listens other, receives message, shows raw_input becomes message sent other script, on. here's code:
client.py
#!/usr/bin/python import socket s = socket.socket() host = socket.gethostname() port = 12221 s.connect((host, port)) while true: z = raw_input("enter server: ") s.send(z) print s.recv(1024)
server.py
#!/usr/bin/python import socket s = socket.socket() host = socket.gethostname() port = 12221 s.bind((host, port)) s.listen(5) while true: c, addr = s.accept() print 'got connection from', addr print c.recv(1024) q = raw_input("enter client: ") c.send(q)
any help? thank you.
like @davidcullen said in comments, halting on second time through while loop server accept new connection.
you can around doing if-connected check. added print statements debug happening.
server.py
#!/usr/bin/python import socket s = socket.socket() host = socket.gethostname() port = 12221 s.bind((host, port)) s.listen(5) c = none while true: if c none: # halts print '[waiting connection...]' c, addr = s.accept() print 'got connection from', addr else: # halts print '[waiting response...]' print c.recv(1024) q = raw_input("enter client: ") c.send(q)
client.py
#!/usr/bin/python import socket s = socket.socket() host = socket.gethostname() port = 12221 s.connect((host, port)) print 'connected to', host while true: z = raw_input("enter server: ") s.send(z) # halts print '[waiting response...]' print s.recv(1024)
Comments
Post a Comment