'socket recv() is not returning in Python
I was writing an echo server but recv() does not return while the sender process is alive. If I use one recv() call instead of my recvall() function, it is returning and working. Please let me know what is wrong with recvall() function..
Server:
import socket
def recvall(skt):
msg_total = b""
while True:
msg = skt.recv(1024)
if not msg: break
msg_total += msg
return msg_total
def main():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("localhost",3333))
s.listen()
conn,addr = s.accept()
# data = conn.recv(1024) <= this recv() is not blocking
data = recvall(conn)
conn.sendall(data)
main()
Client:
import socket
def main():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost",3333))
msg = ("a" * 10).encode()
s.sendall(msg)
rsp = s.recv(1024)
main()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
