def client(server_addr): """Client - read more date after shut-down """ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(server_addr) # connect to server process sent_bytes = [] recv_bytes = [] for message in msg.msgs(20, length=2000): # generate 20 msgs n_sent = sock.send(message) # send message to server sent_bytes.append(n_sent) data = sock.recv(2048) # receive response from server if not data: # check if server terminates abnormally print('Server abnormally terminated') break recv_bytes.append(len(data)) else: # Now all the messages sent. Terminate outgoing TCP connection. sock.shutdown(socket.SHUT_WR) # send eof mark (FIN) # Receive more for the remaining messages while True: data = sock.recv(2048) if not data: break recv_bytes.append(len(data)) sock.close() msg.report(sent_bytes, recv_bytes)
def client(server_addr): """Client - converting to file-like object to allow buffered I/O Assumption: request/response messages ending with LF """ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(server_addr) # connect to server process # file-like obj in read/write binary mode # Note: binary mode preserve the data (not convert encodings and line ending) rfile = sock.makefile('rb') # incoming stream wfile = sock.makefile('wb') # outgoing stream sent_bytes = [] recv_bytes = [] for message in msg.msgs(20, length=2000): n_sent = wfile.write(message) wfile.flush() # flush-out user buffer to send immediately sent_bytes.append(n_sent) data = rfile.readline() # incoming line message if not data: # check if server terminates abnormally print('Server abnormally terminated') break recv_bytes.append(len(data)) sock.close() msg.report(sent_bytes, recv_bytes)
def client(server_addr): """Client - read more date after shut-down """ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(server_addr) # connect to server process sock.setblocking(False) # non-blocking socket sel = selectors.DefaultSelector() sel.register(sock, selectors.EVENT_READ | selectors.EVENT_WRITE) it = msg.msgs(100, length=2000) # generator is iterable sent_bytes = [] recv_bytes = [] keep_running = True while keep_running: events = sel.select( timeout=1) # wake up every 1 sec even though no events if not events: # timeout event occurs print('timeout') continue for key, mask in events: conn = key.fileobj # recv if socket becomes readable if mask & selectors.EVENT_READ: data = conn.recv(4096) if not data: print('Server closing') sel.unregister(conn) keep_running = False break recv_bytes.append(len(data)) print('recv:', len(data)) # sendall if socket becomes writable if mask & selectors.EVENT_WRITE: try: message = next(it) except StopIteration: # no more messages # Do not check writable. sel.modify(conn, selectors.EVENT_READ) conn.shutdown(socket.SHUT_WR) break conn.sendall( message ) # send the entire message even though non-blocking mode sent_bytes.append(len(message)) print('send:', len(message)) # end for # end while sock.close() msg.report(sent_bytes, recv_bytes)
def run(self): for message in msg.msgs(20, length=2000, delay=0.5): n_sent = self.sock.send(message) self.sent_bytes.append(n_sent) data = self.in_file.readline() # receive response if not data: print('Server closing') break self.recv_bytes.append(len(data)) self.sock.close() # to send eof to server msg.report(self.sent_bytes, self.recv_bytes)
def client(server_addr): """Incorrect client """ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(server_addr) # connect to server process sent_bytes = [] recv_bytes = [] for message in msg.msgs(100, length=1400): # generate 100 msgs n_sent = sock.send(message) # send message to server sent_bytes.append(n_sent) data = sock.recv(2048) # receive response from server recv_bytes.append(len(data)) sock.close() # send eof mark (FIN) msg.report(sent_bytes, recv_bytes)
def client(server_addr): """Client - sending and receiving threads """ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(server_addr) # connect to server process receiver = threading.Thread(target=recv_loop, args=(sock, )) receiver.start() # start recv_loop thread # main thread continues hereafter for message in msg.msgs(20, length=2000): n_sent = sock.send(message) sent_bytes.append(n_sent) sock.shutdown(socket.SHUT_WR) # send FIN. This will stop receiver thread receiver.join() # wait for receiver exit sock.close() print('Client terminated') msg.report(sent_bytes, recv_bytes)
def client(server_addr): """Client - converting to file-like object to allow buffered I/O Assumption: request/response messages ending with LF """ sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(server_addr) # connect to server process # Note: binary mode preserve the data # does not convert encodings and line ending as in text mode. file = sock.makefile('rwb') # file-like obj in read/write binary mode sent_bytes = [] recv_bytes = [] for message in msg.msgs(20, length=2000): n_sent = file.write(message) file.flush() # flush-out buffer to send immediately sent_bytes.append(n_sent) data = file.readline() # receive response if not data: break recv_bytes.append(len(data)) file.close() sock.close() msg.report(sent_bytes, recv_bytes)
break self.recv_bytes.append(len(data)) self.sock.close() # to send eof to server if __name__ == '__main__': usage = 'Usage: clients.py host:port [n]' n_clients = 3 try: if len(sys.argv) in (2, 3): host, port = sys.argv[1].split(':') port = int(port) if len(sys.argv) == 3: n_clients = int(sys.argv[2]) else: print(usage) except Exception as e: print(usage) raise # create and start n client thread objects threads = [] for i in range(n_clients): cli = Client((host, port)) cli.start() threads.append(cli) # Wait for terminating child threads for t in threads: t.join() # wait for terminating child thread t print(t) msg.report(t.sent_bytes, t.recv_bytes)