0

I have a client and a server communicating as follows:

# server

running = 1 

    while running:
        data = self.client.recv(self.size) 
        self.client.send(str(self.vel)) 

# client

def runit(self):

    self.s.send('test') 
    data = float(self.s.recv(self.size))
    self.master_.after(5,self.runit)

So both are in infinity loops, although this does transfer data, it is inefficient for my application. I am making a game and I want the server to send data to the client at specific instances, and I also want the client to receive that data at that instance. Something similar to a callback would work. Unfortunately, I wasn't able to find anything suitable for my needs.

snowleopard
  • 717
  • 8
  • 19
  • What do mean by saying it's not efficient? Do you mean the loop makes it inefficient? when you establish a server and there is a listen function in it , the while does not loop always it waits for a connection and when some client is connected then the while loops once.it's the same way for the client about receiving data.and about the sending data it depends on the time you want the server or client to send data , is it after receiving some specific data? – Freelancer Dec 12 '14 at 05:00

1 Answers1

0

First, I don't see anything "inefficient", a while loop is very common in such case, and as the comment says, the loop will just blocks in recv until a client connects.

So your question is how to send data from server to client? If a connection is established, assuming you're using TCP, then just call send() method on that socket. Maybe you want to set socket SO_KEEPALIVE, see How to change tcp keepalive timer using python script?.

Community
  • 1
  • 1
laike9m
  • 18,344
  • 20
  • 107
  • 140