If you want to send bytes to a socket, you must have a socket open (server) and be connected to it (client) from the start (your most likely first loop iteration) until the end of whatever you are trying to send (end of loop/array/bytes). Afterwards you are free to decommission the resources be it closing the connection or anything else. In other cases you'll break the tunnel and thus communication halts.
In server part, move this out of the loop, somewhere else:
communication_socket.close()
# e.g.
while True:
...
# detect you have received everything, then break the loop
communication_socket.close()
in client part:
cli.connect((HOST,PORT))
while True:
...
cli.close()
therefore your socket will not be closed if you expect to receive multiple messages with it because the closing part on server will be after the loop, i.e. after you are done with the receiving (whenever that is) and on the client part, close after the loop, once you are done sending.
Full:
# server.py
import socket
HOST='127.0.0.1'
PORT=6595
#Code for the socket
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind((HOST,PORT))
server.listen(5)
communication_socket,address=server.accept()
while True:
message=communication_socket.recv(1024).decode('utf-8')
if not message:
break
print(f"Message from client is: {message}")
communication_socket.close()
# client.py
import socket
HOST='127.0.0.1'
PORT=6595
#Code for the socket
cli=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
i=1
cli.connect((HOST,PORT))
while i<10:
i=str(i)
cli.send(i.encode('utf-8'))
#print(cli.recv(1024).decode('utf-8'))
i=int(i)
i=i+1
print(i)
cli.close()
Message from client is: 1
Message from client is: 2
Message from client is: 3
Message from client is: 4
Message from client is: 5
Message from client is: 6
Message from client is: 7
Message from client is: 8
Message from client is: 9
2
3
4
5
6
7
8
9
10
By accept()
you are accepting a new connection, therefore you'd get only the first byte in your example. Once you move it out, you can send all of the data, however only through one of the connections. For a new connection you'd need another accept()
and depending on your use-case it can be a/synchronous, sequentially or parallel.
By the number in recv()
you are limiting the data. In 1024
your data might be mashed into a single line, or not, depending on your computer's speed. With recv(1)
you should get only single byte, thus separating each on a separate line/print.
Don't forget to check https://docs.python.org/3/library/socket.html#example or have a look at this simple ping-pong. It might give you an idea:
# pingserver.py
import socket
from datetime import datetime
from time import sleep
from random import randrange, choice
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(("127.0.0.1", 8000))
sock.listen(1)
conn, addr = sock.accept()
with conn:
data = "."
print(f"I'll start with sending {data!r} to a new connection")
while True:
conn.send(f"{datetime.now()}:S\t{data}".encode())
sleep(randrange(1, 3))
data = conn.recv(1024)
data = "\t".join(data.decode().split("\t")[1:])
if data:
print(f"{datetime.now()}:S\t{data}")
# pingclient.py
import socket
from datetime import datetime
from time import sleep
from random import randrange
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect(("127.0.0.1", 8000))
while True:
sleep(randrange(1, 2))
data = sock.recv(1024)
data = "\t".join(data.decode().split("\t")[1:])
if data:
print(f"{datetime.now()}:C\t{data}")
data = chr(ord(data) + 1)
sock.send(f"{datetime.now()}:C\t{data}".encode())
python pingserver.py
python pingclient.py # different terminal
# joint output sortable by time
Alternatively, even though it might be mostly more expensive: