Dear,
Hope you are doing well,
I wrote a simple (Single Threaded and Multi Threaded) TCP echo server, and captured the packets in both scenarios, the problem here is, in the two pcaps, the order of the TCP packet is the same, not concurrent in the multi threaded one.
Here's the PCAPs to check and please feel free to reach out.
Note: I've executed the clients in a bash script to ensure concurrency:
!#/bin/bash
python3 client.py &
python3 client.py &
wait
BR,
Ahmad Jehad
Attachment:
singletcp.pcap
Description: Binary data
Attachment:
multitcp.pcap
Description: Binary data
'''
Here we started by importing the threading library, and created the socket
and started the listening process in the main() function,
'''
import socket
import threading
SERVER_SOCKET_HOST = '127.0.0.1'
SERVER_SOCKET_PORT = 5555
def main():
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverSocket.bind((SERVER_SOCKET_HOST,SERVER_SOCKET_PORT))
serverSocket.listen()
print(f"Listening on: {SERVER_SOCKET_PORT}")
while True:
conn, addr = serverSocket.accept()
print(f'Connection Established: {addr[0]}:{addr[1]}')
THREAD_HANDLER = threading.Thread(target=handle_client, args=(conn,))
THREAD_HANDLER.start()
def handle_client(conn):
with conn as serverSocket:
request = serverSocket.recv(1024)
print(f'Message Received: {request.decode("utf-8")}')
if __name__ == '__main__':
main()
import socket
SERVER_SOCKET_HOST = "127.0.0.1"
SERVER_SOCKET_PORT = 5555 # You can use any available non-service port
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serverSocket:
serverSocket.bind((SERVER_SOCKET_HOST, SERVER_SOCKET_PORT))
serverSocket.listen()
print(f"Listening on {SERVER_SOCKET_PORT}")
conn, addr = serverSocket.accept()
# For conventional naming purposes we named them conn, and addr.
with conn:
print(f"Connected with {addr}")
while True:
data = conn.recv(1024)
# 1024 means 1024 bytes of data
if not data:
break
# If no data left then break the program
conn.send(data)
import socket
CLIENT_SOCKET_HOST = "127.0.0.1"
SERVER_SOCKET_PORT = 5555
# To connect with the server we need to create a socket on his address and port.
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as clientSocket:
clientSocket.connect((CLIENT_SOCKET_HOST, SERVER_SOCKET_PORT))
clientSocket.send(b"Hello from the zombie")
# We sent the the string as bytes of data, to deserilize data over the network.
data = clientSocket.recv(1024)
clientSocket.send(input(""))
print(f"Received the following: {data}")