so im working on a project and i got stuck (probably something to do with the thread because im new to it), so i narrowed the code to this short Python, Tcp, server-client connection.
server-side:
import socket
import time
host = '127.0.0.1'
port = 9199
# Create a TCP/IP socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the address and port
server_socket.bind((host, port))
# Listen for incoming connections
server_socket.listen()
# Accept incoming connections
client_socket, address = server_socket.accept()
i=0
while i<5:
# Send a message to the client
message = "Hello from the server!"+str(i)
client_socket.sendall(message.encode())
i=i+1
time.sleep(5)
print('sent: ', message)
client-side:
import socket
import threading
import time
host = '127.0.0.1'
port = 9199
def get_messages():
message = client_socket.recv(1024).decode()
print(message)
def sleep():
time.sleep(50)
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host,port))
print('Connected to server')
messages_thread = threading.Thread(target=get_messages)
time_thread = threading.Thread(target=sleep)
messages_thread.start()
time_thread.start()
just a simple server that sends client a message evrey 5 secs, while the clients uses 2 threads- one that just waits 50 secs and one that recieves messages.
when i run the code i only recieve the first message. why is that?