I tried to create a Queue object and then received data from Client and put that received data to the Queue and after send that Queue to the connected clients. But this plan failed the server only shares the message in the queue with the client sending it.
What i want is to make sure that Clients are received and then they are given the Queue object with data and also the client thread should constantly be reading and writing to the Queue. All client threads should shared the same Queue of new messages.
Here is the code that i tried.
Expand|Select|Wrap|Line Numbers
- import Queue
- import threading
- import time
- import socket
- queue = Queue.Queue()
- class ThreadClient(threading.Thread):
- def __init__(self, queue, client):
- threading.Thread.__init__(self)
- self.queue = queue
- self.client = client
- def run(self):
- print "Got connection from %s"% str(self.client.getpeername())
- while True:
- #Grab message from the queue
- data = self.client.recv(2342)
- self.queue.put(data)
- host = self.queue.get()
- self.client.send(str(host))
- #singing task
- self.queue.task_done()
- self.client.close()
- def main():
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
- s.bind(("", 1110))
- print "Waiting for clients"
- while True:
- s.listen(5)
- client , address = s.accept()
- D = ThreadClient(queue, client)
- D.setDaemon(True)
- D.start()
- queue.join()
- main()