I am creating a tcp server-client program, with client sending data to server and server printing that data. similarly server sending command to Client and client printing that data and responding to it. it has to run in a loop handling many handshakes. it is working fine for one et of send and receive data, however hen it is not working.
Server code:
#socket_echo_server.py
import socket
import sys
Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Bind the socket to the port
server_address = (‘192.168.4.1’, 5555)
print(‘starting up on {} port {}’.format(*server_address))
sock.bind(server_address)
Listen for incoming connections
sock.listen(1)
while True:
# Wait for a connection
print(‘waiting for a connection’)
connection, client_address = sock.accept()
try:
print(‘connection from’, client_address)
# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(1024)
print('received {!r}'.format(data))
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection, client_address = sock.accept()
# Send data
message: bytes = b'0x07 0x21 0x1c 0x34 0x78 0x3b 0xc0.'
print('sending {!r}'.format(message))
sock.sendall(message)
finally:
# Clean up the connection
connection.close()
Server error:
/Users/kanchanpandey/PycharmProjects/TCP/venv/bin/python /Users/kanchanpandey/PycharmProjects/TCP/tcp_server.py
starting up on 192.168.4.1 port 5555
waiting for a connection
connection from (‘192.168.4.1’, 58182)
received b’0x07 0x20 0x1c 0x34 0x4a 0x20’
Traceback (most recent call last):
File “/Users/kanchanpandey/PycharmProjects/TCP/tcp_server.py”, line 27, in
connection, client_address = sock.accept()
^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/socket.py”, line 293, in accept
fd, addr = self._accept()
^^^^^^^^^^^^^^
OSError: [Errno 22] Invalid argument
Process finished with exit code 1
Client code:
#socket_echo_client.py
import socket
import sys
Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Connect the socket to the port where the server is listening
server_address = (‘192.168.4.1’, 5555)
print(‘connecting to {} port {}’.format(*server_address))
sock.connect(server_address)
while True:
# Send data
message: bytes = b"0x07 0x20 0x1c 0x34 0x4a 0x20"
print(‘sending {!r}’.format(message))
sock.sendall(message)
connection, client_address = sock.accept()
# Receive the data in small chunks and retransmit it
data = connection.recv(1024)
print(‘received 1st data{!r}’.format(data))
#finally:
print(‘closing socket’)
sock.close()
Error : client
/Users/kanchanpandey/PycharmProjects/TCP/venv/bin/python /Users/kanchanpandey/PycharmProjects/TCP/tcp_client.py
connecting to 192.168.4.1 port 5555
sending b’0x07 0x20 0x1c 0x34 0x4a 0x20’
Traceback (most recent call last):
File “/Users/kanchanpandey/PycharmProjects/TCP/tcp_client.py”, line 19, in
connection, client_address = sock.accept()
^^^^^^^^^^^^^
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/socket.py”, line 293, in accept
fd, addr = self._accept()
^^^^^^^^^^^^^^
OSError: [Errno 22] Invalid argument
Process finished with exit code 1