7 # action can be reflect or drop
10 def handle_connection (connection, client_address):
11 print("Received connection from {}".format(repr(client_address)))
14 data = connection.recv(4096)
17 if (action != "drop"):
18 connection.sendall(data)
22 def run_server(ip, port):
23 print("Starting server {}:{}".format(repr(ip), repr(port)))
24 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
25 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
26 server_address = (ip, int(port))
27 sock.bind(server_address)
31 connection, client_address = sock.accept()
32 handle_connection (connection, client_address)
36 for i in range (0, pow(2, 16)):
40 def run_client(ip, port):
41 print("Starting client {}:{}".format(repr(ip), repr(port)))
42 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
43 server_address = (ip, port)
44 sock.connect(server_address)
52 timeout = time.time() + 2
53 while n_rcvd < n_sent and time.time() < timeout:
57 for i in range(n_read):
58 if (data[n_rcvd + i] != tmp[i]):
59 print("Difference at byte {}. Sent {} got {}"
60 .format(n_rcvd + i, data[n_rcvd + i], tmp[i]))
63 if (n_rcvd < n_sent or n_rcvd > n_sent):
64 print("Sent {} and got back {}".format(n_sent, n_rcvd))
66 print("Got back what we've sent!!");
71 def run(mode, ip, port):
72 if (mode == "server"):
74 elif (mode == "client"):
77 raise Exception("Unknown mode. Only client and server supported")
79 if __name__ == "__main__":
80 if (len(sys.argv)) < 4:
81 raise Exception("Usage: ./dummy_app <mode> <ip> <port> [<action>]")
82 if (len(sys.argv) == 5):
85 run (sys.argv[1], sys.argv[2], int(sys.argv[3]))