Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #server
- import random
- import socket
- randnum = random.randint(0,30)
- clientList = []
- HOST = '127.0.0.1'
- PORT = 458
- ADDR = (HOST, PORT)
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.bind(ADDR)
- s.listen(3)
- ls = []
- condition = Condition()
- class ProducerThread(Thread):
- def __init__(self, client, username):
- self.client = client
- self.username = username
- Thread.__init__(self)
- def run(self):
- global queue
- while True:
- try:
- mess = self.client.recv(1024)
- condition.acquire()
- m = mess.decode()
- if (m == randnum):
- self.client.send("bingo")
- self.client.close()
- print ("closed")
- condition.notify()
- condition.release()
- except socket.error as e:
- print("Couldnt connect with the socket-server: %s\n terminating program" % e)
- print("client left")
- # remove him from the client list, and close his socket
- break
- while True:
- print("waiting for connection")
- conn, addr = s.accept()
- print(addr)
- conn.send(("your address is " + str(addr)).encode())
- username = conn.recv(100000)
- username.decode()
- print username
- clientList.append((conn, username))
- t = ProducerThread(conn, username)
- t.start()
- #client
- import socket
- from threading import Thread, Condition
- HOST = '127.0.0.1'
- PORT = 458
- ADDR = (HOST, PORT)
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.connect(ADDR)
- condition = Condition()
- msg = s.recv(100000)
- msg.decode()
- print(msg)
- # send username
- print("Enter Username")
- username = raw_input()
- s.send(username.encode())
- class Recive(Thread):
- def __init__(self):
- Thread.__init__(self)
- def run(self):
- # global isConnected
- while True:
- try:
- mess = s.recv(1024)
- mess.decode()
- if(mess == "bingo"):
- {
- s.close()
- }
- print (mess)
- except socket.error as e:
- print("Couldnt connect with the socket-server: %s\n terminating program" % e)
- break
- class SendM(Thread):
- def __init__(self):
- Thread.__init__(self)
- def run(self):
- while True:
- m = raw_input()
- try:
- s.send(m.encode())
- except socket.error as e:
- s.close()
- print("***Couldnt connect with the socket-server: %s\n terminating program" % e)
- break
- t = SendM()
- t.start()
- t2 = Recive()
- t2.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement