7 # action can be reflect or drop
11 def test_data (data, n_rcvd):
13 for i in range(n_read):
14 expected = (n_rcvd + i) & 0xff
15 byte_got = ord (data[i])
16 if (byte_got != expected):
17 print("Difference at byte {}. Expected {} got {}"
18 .format(n_rcvd + i, expected, byte_got))
21 def handle_connection (connection, client_address):
22 print("Received connection from {}".format(repr(client_address)))
26 data = connection.recv(4096)
30 n_rcvd += test_data (data, n_rcvd)
31 if (action != "drop"):
32 connection.sendall(data)
36 def run_server(ip, port):
37 print("Starting server {}:{}".format(repr(ip), repr(port)))
38 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
39 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
40 server_address = (ip, int(port))
41 sock.bind(server_address)
45 connection, client_address = sock.accept()
46 handle_connection (connection, client_address)
50 for i in range (0, pow(2, 16)):
54 def run_client(ip, port):
55 print("Starting client {}:{}".format(repr(ip), repr(port)))
56 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
57 server_address = (ip, port)
58 sock.connect(server_address)
66 timeout = time.time() + 2
67 while n_rcvd < n_sent and time.time() < timeout:
71 for i in range(n_read):
72 if (data[n_rcvd + i] != tmp[i]):
73 print("Difference at byte {}. Sent {} got {}"
74 .format(n_rcvd + i, data[n_rcvd + i], tmp[i]))
77 if (n_rcvd < n_sent or n_rcvd > n_sent):
78 print("Sent {} and got back {}".format(n_sent, n_rcvd))
80 print("Got back what we've sent!!");
85 def run(mode, ip, port):
86 if (mode == "server"):
88 elif (mode == "client"):
91 raise Exception("Unknown mode. Only client and server supported")
93 if __name__ == "__main__":
94 if (len(sys.argv)) < 4:
95 raise Exception("Usage: ./dummy_app <mode> <ip> <port> [<action> <test>]")
96 if (len(sys.argv) == 6):
98 test = int(sys.argv[5])
100 run (sys.argv[1], sys.argv[2], int(sys.argv[3]))