How to accept two connections using sockets in Python -
i working on chat program. right can except 1 client. how make can accept 2 clients? still bit of noob when comes sockets can explain thoroughly?
server code:
import socket def mainfunc(): host = "" port = 50000 iplist = [] nicknamelist = [] num = true s = socket.socket() s.bind((host, port)) s.listen(1) c, addr = s.accept() print("connection from: " + str(addr) + "\n") iplist.insert(0, str(addr)) while true: data = c.recv(1024) if not data: break if num == true: nicknamelist.insert(0, str(data)) num = false else: print("from " + nicknamelist[0] + ": " + str(data) + "\n") message = raw_input("message want send: ") print("\n") c.send(message) c.close()
i have tried changing s.listen(1) s.listen(2). did not seem allow second person connect. can explain why?
one call accept
accepts 1 connection. accept 2 connections, call accept
twice.
Comments
Post a Comment