For some reason i cant seem to be able to create a host or client where the client can always send a message to the host whenever . i can only send one message and after that nothing is received .
my while loops should allow me to do this so i don't know why this is no achievable for me ;'()
#Host server
import socket
server_host= 'localhost'
server_port= 88
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind((server_host,server_port))
server.listen(5)
print('listening on {} port {}'.format(server_host,server_port))
while True:
client,addr = server.accept()
recieve = client.recv(2024)
print(recieve.decode())
Client :
# Python-Socket-Client
import socket
#'localhost'
# 88
target_server = str(input('Target_Host:'))
target_port = int(input('Target_Port:'))
client = socket.socket(socket.AF_INET , socket.SOCK_STREAM )
client.connect((target_server,target_port))
print('\n')
print('****************************')
print(target_server)
print(target_port)
def send_and_recieve_message(message):
#response = client.recv(4065)
if type(message) == str:
client.send(message.encode())
while True:
mess= input("{}:".format('Client'))
send_and_recieve_message(mess)
In your host code server.accept() is a blocking call. It will wait until someone connects. You need to separate it out of your while loop. And then check if the connection is still valid if not wait for another connection.
while True:
client,addr = server.accept()
while client != None:
recieve = client.recv(2024)
print(recieve.decode())
Something similar to this you probably will want to add a better check to see if the connection is still valid.