def client_test(file_path, debug): client = RDTSocket(debug=debug) if client.connect((SERVER_ADDR, SERVER_PORT)): data_count = 0 count = 20 f = open(file_path, 'rb') data_send = f.read() start = time.perf_counter() for i in range(count): # send data for count times data_count += len(data_send) client.send(data_send) data_send = data_send * count mid = time.perf_counter() print('---------------------------------------------') print( f'client send OK, data size: {unit_convert(len(data_send))}, send time cost: {mid - start} s' ) print('---------------------------------------------') echo = bytearray() while True: reply = client.recv(2048) echo.extend(reply) assert echo == data_send[:len(echo)], 'Inconsistent echo content' if len(echo) == len(data_send): break end = time.perf_counter() print('=============================================') print( f'client recv OK, data size: {unit_convert(len(echo))} bytes, recv time cost: {end - mid} s' ) print(f'Total time cost: {end - start}') print('=============================================') client.close() print('client main thread stop.')
from rdt import RDTSocket if __name__ == "__main__": try: client = RDTSocket() client.connect(("127.0.0.1", 1234)) while True: print("input data:") data = input() if data == "alice": data = open("alice.txt", "r").read() client.send(data.encode()) if data == "exit" or data == "quit": break client.close() print("Client Closed") except KeyboardInterrupt: exit()
''' check if your rdt pass either of the two mode A may be significantly slower when slice size is small ''' if blocking_send: print('transmit in mode A, send & recv in slices') slices = [ encoded[i * slice_size:i * slice_size + slice_size] for i in range(len(encoded) // slice_size + 1) ] assert sum([len(slice) for slice in slices]) == len(encoded) start = time.perf_counter() for i in range(count): # send 'alice.txt' for count times for slice in slices: client.send(slice) reply = client.recv(slice_size) echo += reply else: print('transmit in mode B') start = time.perf_counter() for i in range(count): client.send(encoded) while len(echo) < len(encoded) * (i + 1): reply = client.recv(slice_size) echo += reply client.close() ''' make sure the following is reachable '''
client.bind(('127.0.0.1', 9998)) client.connect(('127.0.0.1', 9999)) data_count = 0 echo = b'' count = CNT with open(FILE, 'rb') as f: data = f.read() encoded = data assert len(data) == len(encoded) start = time.perf_counter() for i in range(count): # send 'alice.txt' for count times data_count += len(data) client.send(encoded) ''' blocking send works but takes more time ''' while True: reply = client.recv(2048) echo += reply print(reply) if len(echo) == len(encoded) * count: break client.close() ''' make sure the following is reachable '''