TCP/ IP server implementation

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

Let’s start by quoting your code, so it looks like code. You should use triple backticks (the same key as the ~ on most keyboards). Put three consecutive backticks at the beginning of each section of code, and three consecutive backticks at the end of each section of code.

The accept() method works only on the main socket, the one master socket that spawns new sockets for every connection. Once you’re handling a specific client, there’s no more accepting to be done; you have a bidirectional socket. That applies to the client too.

Tip: Read your error messages and use them to focus in on the specific part of the code that’s breaking. Python pointed you directly to your accept() calls as the cause of the issue here.

I have successfully created the tcp server. my client is an apk(program) and I have the hex bytes it is supposed to send and receive. I captured it through packet capture. when I am trying to recreate that, I get fwg errors:

the TCP server is successfully created.
code :

socket_echo_server.py

import socket

Create a TCP/IP socket

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

Bind the socket to the port

server_address = (‘192.168.1.15’, 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()

print(‘connection from’, client_address)

Receive the data in small chunks and retransmit it

while True:
# Receive data
data = connection.recv(1024)
mybytes = bytearray(data)
print(‘received’, mybytes)

# Send data
message: bytes = b'\x07\x21\x1c\x34\x78\x3b\xc0'
byte_array = bytearray(message)
print('sending', byte_array)
connection.sendall(byte_array)

# Receive data
data = connection.recv(1024)
print('received {!r}'.format(data))

# Send data
message: bytes = b'0x07 0x21 0x1c 0x34 0x7d 0x3b 0xdf'
print('sending {!r}'.format(message))
connection.sendall(message)

# Receive data
data = connection.recv(1024)
print('received {!r}'.format(data))

# Send data
message: bytes = b'0x07 0x21 0x1c 0x34 0x70 0x3b 0xd8'
print('sending {!r}'.format(message))
connection.sendall(message)

# Receive data
data = connection.recv(1024)
print('received {!r}'.format(data))

# Send data
message: bytes = b'0x07 0x21 0x1c 0x34 0x7c 0x3b 0xdc'
print('sending {!r}'.format(message))
connection.sendall(message)

# Receive data
data = connection.recv(1024)
print('received {!r}'.format(data))

# Send data
message: bytes = b'0x07 0x21 0x1c 0x34 0x0f 0x3b 0xd1'
print('sending {!r}'.format(message))
connection.sendall(message)

# Receive data
data = connection.recv(1024)
print('received {!r}'.format(data))

# Send data
message: bytes = b'0x07 0x21 0x1c 0x34 0x09 0x3b 0xd3'
print('sending {!r}'.format(message))
connection.sendall(message)

# Receive data
data = connection.recv(1024)
print('received {!r}'.format(data))

# Send data
message: bytes = b'0x07 0x21 0x1c 0x34 0x08 0x3b 0xd0'
print('sending {!r}'.format(message))
connection.sendall(message)

# Receive data
data = connection.recv(1024)
print('received {!r}'.format(data))

# Send data
message: bytes = b'0x07 0x2c 0x1c 0x34 0x7e 0x60 0x1b 0x4e 0xfb'
print('sending {!r}'.format(message))
connection.sendall(message)

# Receive data
data = connection.recv(1024)
print('received {!r}'.format(data))

# Send data
message: bytes = b'0x07 0x21 0x1c 0x34 0x7c 0x3b 0xdc  '
print('sending {!r}'.format(message))
connection.sendall(message)
break

# finally:
# Clean up the connection

connection.close()

output of the server is:

the first hex sequence I am supposed to receive is : 07 20 1c 34 4a 20.
Help required in receiving raw hex bytes from client, storing in byte array and printing them.

Source code is text. Output is text. Neither should be shown in photographs. Please use triple backticks ``` at start and end of your code so it can be read easily by everyone else.