def server_test(debug_model: bool): server = RDTSocket(debug=debug_model) server.bind((SERVER_ADDR, SERVER_PORT)) try: while True: conn, client_addr = server.accept() Echo(conn, client_addr).start() except KeyboardInterrupt as k: print(k)
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 from socket import socket, AF_INET, SOCK_DGRAM, SOCK_STREAM import time if __name__ == '__main__': server = RDTSocket() # server = socket(AF_INET, SOCK_STREAM) # check what python socket does server.bind(('127.0.0.1', 9999)) # server.listen(0) # check what python socket does while True: print(type(server)) conn, client_addr = server.accept() start = time.perf_counter() while True: data = conn.recv(4096) print(data) if data: conn.send(data) else: break ''' make sure the following is reachable ''' conn.close() print(f'connection finished in {time.perf_counter()-start}s')
import time from difflib import Differ from rdt import RDTSocket if __name__ == '__main__': client = RDTSocket() # client = socket(AF_INET, SOCK_STREAM) # check what python socket does client.connect(('127.0.0.1', 9999)) echo = b'' count = 1 slice_size = 2048 blocking_send = False with open('alice.txt', 'r') as f: data = f.read() encoded = data.encode() assert len(data) == len(encoded) ''' 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)
from Cons import * from rdt import RDTSocket import time from difflib import Differ client = RDTSocket(debug=DEBUG, rate=RATE) 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:
from rdt import RDTSocket import time from difflib import Differ from Cons import RATE, FILE, CNT client = RDTSocket(rate=RATE) client.connect(('127.0.0.1', 2777)) data_count = 0 echo = b'' count = CNT with open(FILE, 'r') as f: data = f.read() encoded = data.encode() 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
from rdt import RDTSocket if __name__ == '__main__': client_address = ('127.0.0.1', 44444) client = RDTSocket(rate=None) client.bind(client_address)
from rdt import RDTSocket import time from Cons import RATE if __name__ == '__main__': server = RDTSocket(rate=RATE) server.bind(('127.0.0.1', 2777)) while True: conn, client_addr = server.accept() start = time.perf_counter() while True: data = conn.recv(2048) if data: conn.send(data) else: break ''' make sure the following is reachable ''' conn.close() print(f'connection finished in {time.perf_counter()-start}s')
from rdt import RDTSocket if __name__ == '__main__': server_address = ('127.0.0.1', 33333) server = RDTSocket(rate=None) server.bind(server_address) while True: conn, client = server.accept()
from rdt import RDTSocket if __name__ == '__main__': server_address = ('127.0.0.1', 33333) client = RDTSocket(rate=None) client.connect(server_address)
from rdt import RDTSocket if __name__ == '__main__': server_address = ('127.0.0.1', '33333') server = RDTSocket(rate=None) server.bind(server_address)
from rdt import RDTSocket from threading import * import time client_addr = ("127.0.0.1", 9091) client = RDTSocket() if __name__ == '__main__': client.bind(client_addr) client.connect(("127.0.0.1", 9090)) # while True: # data = input(">") # client.send(data.encode()) # # if data == 'exit': # break while True: data = client.recv(1024) print("-----------------------") print("Client Receive: ", data) print("-----------------------") if data == b'exit': break # start = time.time() # data = open("src/补充说明.pdf", 'rb').read() # client.send(data) # print(time.time() - start)
from rdt import RDTSocket if __name__ == "__main__": try: server = RDTSocket() server.bind(("127.0.0.1", 1234)) # 这个叫 server 的 socket 只用来监听连接请求,有一个 client 想连接,server 就给这个 clinet 分配一个专属的 conn,在 conn 上收发数据 while True: conn, addr = server.accept() while True: data = conn.recv(1024000) if data: print("received payload: " + data.decode()) if data == b"exit": break elif data == b"quit": print("Server Closed") exit() else: break conn.close() print("Server Connection Lost") except KeyboardInterrupt: exit()
from rdt import RDTSocket from socket import socket, AF_INET, SOCK_STREAM import time from difflib import Differ if __name__=='__main__': # client = RDTSocket() client = RDTSocket() # check what python socket does client.connect(('127.0.0.1', 9999)) echo = b'' count = 5 slice_size = 2048 blocking_send = True with open('alice.txt', 'r') as f: data = f.read() encoded = data.encode() assert len(data)==len(encoded) ''' 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
from Cons import RATE, DEBUG from rdt import RDTSocket import time if __name__ == '__main__': server = RDTSocket(debug=DEBUG, rate=RATE) server.bind(('127.0.0.1', 9999)) while True: conn, client_addr = server.accept() start = time.perf_counter() while True: data = conn.recv(2048) if data: conn.send(data) else: break ''' make sure the following is reachable ''' conn.close() print(f'connection finished in {time.perf_counter()-start}s')
import network from rdt import RDTSocket import _thread if __name__ == '__main__': # sock_snd = RDTSocket() # sock_snd.bind(('127.0.0.1', 5555)) # # sock_rcv = RDTSocket() # sock_rcv.bind(('127.0.0.1',5550)) # sock_snd.sendto(b'connecting',('127.0.0.1',5550)) # for i in range(10): # sock_snd.send(b'hello') # print(sock_rcv.recvfrom(2048)) rdt = RDTSocket() try: _thread.start_new_thread(rdt.send, (b'hello', )) _thread.start_new_thread(rdt.recv, (2048, )) except: print("Error: unable to start thread") pass
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()
from rdt import RDTSocket server_addr = ("127.0.0.1", 9090) server = RDTSocket() if __name__ == '__main__': server.bind(server_addr) conn, addr = server.accept() # while True: # data = conn.recv(1024) # print("-----------------------") # print("Server Receive: ", data) # print("-----------------------") # # if data == b'exit': # break while True: data = input(">") conn.send(data.encode()) if data == 'exit': break # with open("dst/补充说明1.pdf", mode='wb') as file: # data = conn.recv(1024000000) # print("-----------------------") # print("Server Receive!") # print("-----------------------")