I tried sending three different messages from the server through socket and then print them in the client side. but when I do that all of the messages blending into one message. Can someone help me fix this?
Wanted output:
b'Hello1'
b'Hello2'
b'Hello3'
My current output:
b'Hello1Hello2Hello3'
b''
b''
Server code:
import socket
def main():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.bind(("127.0.0.1", 9898))
sock.listen(1)
client_sock, addr = sock.accept()
hello1 = client_sock.sendall("hello1".encode())
hello2 = client_sock.sendall("hello2".encode())
hello3 = client_sock.sendall("hello3".encode())
if __name__ == "__main__":
main()
Client code:
import socket
def main():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect(("127.0.0.1", 9898))
hello1 = sock.recv(2048)
hello2 = sock.recv(2048)
hello3 = sock.recv(2048)
print(hello1)
print(hello2)
print(hello3)
if __name__ == "__main__":
main()