473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Making Client threads constantly reading from the Queue for other client messages

Well i have a problem with properly sharing data or messages between client threads when they have connected to the server. Iam trying to create some kind of messaging server or a chat like Server. where the message from one client is placed on the Queue and sent to other client who are connected or sent to new clients who are just connecting.
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
  1. import Queue
  2. import threading
  3. import time 
  4. import socket 
  5.  
  6. queue = Queue.Queue()
  7.  
  8. class ThreadClient(threading.Thread):
  9.     def __init__(self, queue, client):
  10.         threading.Thread.__init__(self)
  11.         self.queue = queue
  12.         self.client = client 
  13.  
  14.     def run(self):
  15.  
  16.         print "Got connection from %s"% str(self.client.getpeername())
  17.  
  18.         while True:
  19.             #Grab message from the queue
  20.             data = self.client.recv(2342)
  21.             self.queue.put(data)
  22.  
  23.             host = self.queue.get()
  24.  
  25.             self.client.send(str(host))
  26.  
  27.             #singing task 
  28.             self.queue.task_done()
  29.         self.client.close()
  30.  
  31. def main():     
  32.     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  33.     s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
  34.     s.bind(("", 1110))
  35.     print "Waiting for clients"
  36.     while True:
  37.       s.listen(5)
  38.       client , address = s.accept()
  39.       D = ThreadClient(queue, client)
  40.       D.setDaemon(True)
  41.       D.start()
  42.  
  43.     queue.join()
  44. main()
Oct 4 '12 #1
0 1188

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jerry J | last post by:
I'm trying to understand how many threads are involved when a client event handler is called. I am using a simple .net windows application. I click a button on a form that triggers an event. ...
11
by: Mark | last post by:
Hi, For the last 2 years I've been developing vehicle tracking/telemetric software for a company as a self employed individual. The project is quiet big, and is going to be there flagship...
0
by: Benny | last post by:
Hi, My application creates a number of aync threads....i want to make sure that all these threads are of the invariant culture. So, how do i ensure that whenever a new thread is created in my...
3
by: yoav | last post by:
Okay: I have a GUI program that initializes a TcpClient and a Form. From withing the form I create a TextBox and init the client. Then I run a System.Threading.Timer with a method that checks if...
1
by: Mihaly | last post by:
I have a stored procedure in SQL Server 2000, and I want to read the error messages from this stored procedure. Please help me for this question: This is the stored procedure. Please supose than...
4
by: Jason Kumpf | last post by:
OK I've been staring at this code all day and still with everything I have tried I cannot figure out two problems I am having. Once is why the space limit for the directory I create in the code...
13
by: Sandeep Singh | last post by:
I am making socket client application in C# how can i get ip address of client who has connected to server
12
by: SAL | last post by:
Hello, Is it possible to read a CSV from the Client, and bind my Datagrid to the data in the CSV file without uploading the file to the Server first? I have tried and in Debug mode on my...
1
by: Ralph Wiggum | last post by:
I'm planning on pulling data from a webservice and push the data on to a buffer/queue for each response. This should happen in a thread, while another thread parses the data (heavy) and writes them...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.