I'm trying to learn socket programming and currently have the following server
and client
code however the problem is that the server and/or client can't send and recieve messages at the same time, they're taking it in turns to send and recieve messages.
I've looked at the below example but the answer doesn't seem to solve the issue, or I'm following it wrong.
Python Socket - Send/Receive messages at the same time
server
import socket
import threading
s = socket.socket()
host = socket.gethostname()
port = 8080
s.bind((host, port))
s.listen(1)
print("Waiting for connections")
conn, addr = s.accept()
print("Client has connected")
conn.send("Welcome to the server".encode())
def recv_msg():
while True:
recv_msg = conn.recv(1024)
if not recv_msg:
sys.exit(0)
recv_msg = recv_msg.decode()
print(recv_msg)
def send_msg():
send_msg = input(str("Enter message: "))
send_msg = send_msg.encode()
conn.send(send_msg)
print("message sent")
while True:
send_msg()
t = threading.Thread(target=recv_msg)
t.start()
client
import socket
import threading
s = socket.socket()
host = socket.gethostname()
port = 8080
s.connect((host, port))
print("Connected to the server")
message = s.recv(1024)
message = message.decode()
print(message)
def recv_msg():
while True:
recv_msg = s.recv(1024)
if not recv_msg:
sys.exit(0)
recv_msg = recv_msg.decode()
print(recv_msg)
def send_msg():
send_msg = input(str("Enter message: "))
send_msg = send_msg.encode()
s.send(send_msg)
print("Message sent")
while True:
send_msg()
t = threading.Thread(target=recv_msg)
t.start()
I'm ulitmately trying to create a chat app (with kivy) that sort of resembles Whatsapp/Imessage etc, I've not found a tutorial around how to do this (all the ones I've seen are about creating a chatroom) so if anyone's seen one that would be appreciated.