4

I am writing a program to send the data continuously to the Client from the Server. In here, i am using a timestamp for sample to send it to the multiple Clients who are connected. I have used multi-threading to support multiple Clients. I want the time to be sent every 10 seconds to the client. but in my code, the client stops after receiving the first data. How to make client continuously receive the data. I tried adding while loop in Client Side but it doesn't make it possible. Any suggestions please

Here's the sample Code: Server Side:

import socket
import os
from threading import Thread
import thread
import threading
import time
import datetime

def listener(client, address):
    print "Accepted connection from: ", address
    with clients_lock:
        clients.add(client)
    try:    
        while True:
            data = client.recv(1024)
            if not data:
                break
            else:
                print repr(data)
                with clients_lock:
                    for c in clients:
                        c.sendall(data)
    finally:
        with clients_lock:
            clients.remove(client)
            client.close()

clients = set()
clients_lock = threading.Lock()

host = socket.gethostname()
port = 10016

s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
s.listen(3)
th = []

while True:
    print "Server is listening for connections..."
    client, address = s.accept()
    timestamp = datetime.datetime.now().strftime("%I:%M:%S %p")
    client.send(timestamp) 
    time.sleep(10)
    th.append(Thread(target=listener, args = (client,address)).start())
s.close()

Client Side:

import socket
import os
from threading import Thread


import socket
import time

s = socket.socket()  
host = socket.gethostname()        
port = 10016



s.connect((host, port))
print (s.recv(1024)) 
s.close() 


# close the connection 

My output:

01:15:10

Required Output on clients:

01:15:10
01:15:20
01:15:30
#and should go on

1 Answer 1

5

Server side

while True:
    client, address = s.accept()
    th.append(Thread(target=listener, args = (client,address)).start())
s.close()

In def listener() change while loop to continuously send data for each thread like this

while True:
        data = client.recv(1024)
        if data == '0':
            timestamp = datetime.datetime.now().strftime("%I:%M:%S %p")
            client.send(timestamp)
            time.sleep(2)

at client side add this line in while loop to send some data to satisfy the if condition

s.connect((host, port))
while True:
    s.send('0')
    print(s.recv(1024))
#s.close()
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. It worked. But when i want execute another client on another terminal using the same client code. It doesn't works. Simultaneously another client also wants to receive the same data. how to make it.
updated the solution this will give what you want. Problem was firstly you need to send data continuously for each thread so need to change in listener function then you are not sending any data from client you if condition was not wokring.
this will work. so try the solution and let me know if issue occurs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.