0

I want to create a Socket Client and want to receive the answer if a message is sent in one function but also want to listen to all incoming data all the time.

The problem is, if I do both, the code stops executing as recv() seems to be a blocking function.

This is a quick overview:

Server.py

import socket
HOST = "127.0.0.1"
PORT = 6543
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
 print("Connected by ", addr)
 while True:
 data = conn.recv(1024)
 conn.sendall(data)

Client.py

import socket
import time
import multiprocessing
HOST = "127.0.0.1"
PORT = 6543
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
def keepAlive():
 while True:
 s.send(b"Keep Alive to be handled in keepAlive")
 print(s.recv(1024), "received from keepAlive")
 time.sleep(1)
def send():
 while True:
 s.send(b"Random message to be handled in listen")
 time.sleep(1)
def listen():
 while True:
 print(s.recv(1024), "received from listen")
if __name__ == "__main__":
 keepAliveProc = multiprocessing.Process(target=keepAlive)
 listenProc = multiprocessing.Process(target=listen)
 sendProc = multiprocessing.Process(target=send)
 keepAliveProc.start()
 listenProc.start()
 sendProc.start()

But this will block and the Code doesn't continue. Can anyone help with this?

asked Nov 10, 2020 at 9:54
6
  • How do you expect listen and keepAlive to know which data to receive? As per your design, both would be waiting for data at the same time. Commented Nov 10, 2020 at 13:15
  • yes thats actually the problem. I would like to to be keepAlive faster and get the answer right after sending... How would you solve this in general? Something like send and wait for response? Commented Nov 10, 2020 at 13:17
  • It might be easier to have one function do the recv but be aware of and digest/discard the keep-alive message. Commented Nov 10, 2020 at 13:23
  • the point is, that i have to handle input to and response from the server in a combined way. if i digest all responses in one function, i might not know, what response belongs to which input Commented Nov 10, 2020 at 13:42
  • 1
    There is no such thing as input and responses belonging together in TCP. As you send a keep-alive, you cannot know whether the other side is sending a regular response first before the keep-alive response. Commented Nov 10, 2020 at 16:19

2 Answers 2

0

I don't think you need a res = socket.recv(1024) in your server block. You are not expecting a response from the client (listen()) as the client is not sending anything.

answered Nov 10, 2020 at 11:09
Sign up to request clarification or add additional context in comments.

1 Comment

It might happen, that the server will send something from time to time even without any input. What i showed above is not a client, server combination but two function within the client
-2

I don't have experience in python programming, but i did develop socket communication in java recently. If recv blocks program, then it blocks until it received some data. I think you need to change from multiproccessing to threads since multiproccessing is CPU bound and your communication is network based.

Maybe this can help: Python socket recv in thread

answered Nov 10, 2020 at 11:06

5 Comments

unfortunately, threading shows the same behaviour :(
Do you get any message at all, or it block first one?
well i want the keepAlive message so be received in the keepAlive function, right after it was sent, and everything else in the listen() function. It's sending the message once, receives it in the wrong function (listen()) and then stops.
If i understand correctly, you are receiving some messages from server. Messages you want to receive in listen() method are messages that you know how they will look, right? And the one you don't know you will receive in keepAlive. If this is a case, just receive data once, and than depending on message call functions for that
I added a working example in my code so maybe its more clear what i need. I know the response from keepAlive and want to handle other incoming messages differently. I just don't want to handle them all in one place but the one's I know (for example keepAlive) right in place

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.