0

I'm using the module socket for python, I did my server and my client but there is a problem: sometimes my client doesn't receive the message from the server, because I guess that the server sent the message before that the client is listening. I thought to do something like that:

HEADER = 64 
FORMAT = 'utf-8'

while(response =! "received"):
    client1.send("hello")
    response = client1.recv(HEADER).decode(FORMAT)

This can't work because the function recv() wait until the package come. I want to use the multiprocessing module, but I don't like how, can somebody help me?

  • why not just set a timeout on the socket? – Sam Mason Nov 16 '20 at 17:18
  • How can I do it? Can you help me with an example or with some link? thanks – bruhjustfailing Nov 18 '20 at 13:52
  • it's difficult to know what to suggest, your code doesn't give much to go on. see https://docs.python.org/3/library/socket.html#socket.socket.settimeout for the docs and https://stackoverflow.com/help/mcve for what makes a good question – Sam Mason Nov 18 '20 at 14:21

1 Answers1

0

Are you familiar with threading module? It is simple to work with. I have tested it for socket recv() method before and worked fine.

Elyas Karimi
  • 292
  • 4
  • 11
  • Is the first time that I use this module, but it works pretty good, the only problem is that sometimes server send message before that client starts listening, so client just remain on listening and can't go on. Is there a solution for the method `recv()` to stop listening after n seconds? – bruhjustfailing Nov 14 '20 at 16:50
  • I used this method for real-time continuous connection and because of that, client was always listening. But if you put this method inside an individual process, you might be able to kill that process after n seconds, or alternatively execute the process every n seconds (periodically). – Elyas Karimi Nov 14 '20 at 17:44
  • I'm going to try, but I didn't find some function that can kill a process, there is process.start() but I think there is no function.stop() or something like that. – bruhjustfailing Nov 15 '20 at 10:23
  • Take a look at this link, it may help. [link](https://stackoverflow.com/questions/32053618/how-to-to-terminate-process-using-pythons-multiprocessing) – Elyas Karimi Nov 15 '20 at 10:30