Try   HackMD

網路程式設計 HW10

第一題 一送一收,觀察檢查表的運作

畫面截圖

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Server

import select import socket HOST = '127.0.0.1' PORT = 8000 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(1) conn = None print("Server is running...") while True: try: conn, addr = s.accept() except: time.sleep(1) else: conn.setblocking(0) print("Connected") while True: try: print("[1]clear the socket fd set.") get = [] print("[2]add client socket to fd set") get.append(conn) print("[3]call select() and waiting") read, out, ex = select.select(get, [], [], None) act = len(read) + len(out) + len(ex) print("[4]wake up from select():" + str(act)) for sk in read: if sk is conn: data = conn.recv(1024) if not data: print("Connection closed.") s.close() break else: print("recv:", data.decode(), "(in 10 sec)") except: pass

Client

import socket import time HOST = '127.0.0.1' PORT = 8000 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) word = "How are you?" while True: try: s.connect((HOST, PORT)) except: print("err") time.sleep(1) else: print('Connected to', HOST) s.setblocking(0) while True: time.sleep(10) s.sendall(word.encode()) print("Send:", word, "(in 10 sec)")

第二題 增加為兩個client,server會echo

畫面截圖

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Server

import select import socket import time HOST = '127.0.0.1' PORT = 8000 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(2) conn1 = None conn2 = None print("Server is running...") while True: try: conn1, addr1 = s.accept() conn2, addr2 = s.accept() except: time.sleep(1) else: conn1.setblocking(0) conn2.setblocking(0) print("1 Connected") print("2 Connected") while True: time.sleep(10) try: print("[1]clear the socket fd set.") get = [] print("[2]add client socket to fd set") get.append(conn1) get.append(conn2) print("[3]call select() and waiting") read, out, ex = select.select(get, [], [], None) act = len(read) + len(out) + len(ex) print("[4]wake up from select():" + str(act)) for sk in read: if sk is conn1: data = conn1.recv(1024) if not data: print("Connection closed.") s.close() break else: print("recv from client1:", data.decode(), "(in 10 sec)") conn1 s.sendall(data) if sk is conn2: data = conn2.recv(1024) if not data: print("Connection closed.") s.close() break else: print("recv rom client2:", data.decode(), "(in 10 sec)") conn2.sendall(data) except: pass

Client

import socket import time HOST = '127.0.0.1' PORT = 8000 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) word = "How are you?" while True: try: s.connect((HOST, PORT)) except: print("err") time.sleep(1) else: print('Connected to', HOST) s.setblocking(0) while True: time.sleep(10) try: s.sendall(word.encode()) print("Send:", word, "(in 10 sec)") data = s.recv(1024) print("recv:", data.decode()) except: pass

第三題 服務多個client的echo server

畫面截圖

Server

import select import socket import time client = [] HOST = '127.0.0.1' PORT = 8000 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.setblocking(False) s.listen(5) print("Server is running...") get = [] while True: print("[1]clear the socket fd set.") get = [] print("[2]add sevet to fd set") get.append(s) print("[2]add client socket to fd set") for i in client: get.append(i) print("[3]call select() and waiting") read, out, ex = select.select(get, [], [], None) act = len(read) + len(out) + len(ex) print("[4]wake up from select():" + str(act)) for sk in read: if sk is s: conn, addr = s.accept() print("New connection: ", str(addr)) client.append(conn) for i in client: if sk is i: data = i.recv(1024) if not data: print("client closed connection") client.pop(client.index(i)) i.close() break else: i.sendall(data) print("recv from and echo to client[", client.index(i), "]: ",data.decode())

Client

import socket import time HOST = '127.0.0.1' PORT = 8000 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) word = "How are you?" while True: try: s.connect((HOST, PORT)) except: print("err") time.sleep(1) else: print('Connected to', HOST) s.setblocking(0) while True: time.sleep(10) try: s.sendall(word.encode()) print("Send:", word, "(in 10 sec)") data = s.recv(1024) print("recv:", data.decode()) except: pass

第四題 使用writefds來傳送

畫面截圖

Server

import select import socket import time HOST = '127.0.0.1' PORT = 8000 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(5) server = [s] print("Server is running...") client = [] msg = '' conn, addr = s.accept() client.append(conn) while True: print("[1]clear the socket fd set.") readfds = [] writefds = [] print("[2]add server to fd set") readfds.append(s) print("[2]add client socket to fd set") for i in client: if len(msg) == 0: readfds.append(i) else: writefds.append(i) print("[3]call select() and waiting") act = select.select(readfds, writefds, [], None) print("[4]wake up from select()") for i in client: if i in readfds: data = i.recv(1024) msg = data.decode() print("recv from client", client.index(i), ': ', data.decode()) if i in writefds: i.sendall(msg.encode()) msg = ''

Client

import socket import time HOST = '127.0.0.1' PORT = 8000 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) word = "How are you?" while True: try: s.connect((HOST, PORT)) except: print("err") time.sleep(1) else: print('Connected to', HOST) s.setblocking(0) while True: time.sleep(10) try: s.sendall(word.encode()) print("Send:", word, "(in 10 sec)") data = s.recv(1024) print("recv:", data.decode()) except: pass

第五題 使用timeout

畫面截圖

Server

import select import socket import time HOST = '127.0.0.1' PORT = 8000 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((HOST, PORT)) s.listen(5) server = [s] print("Server is running...") client = [] msg = '' conn, addr = s.accept() client.append(conn) while True: print("[1]clear the socket fd set.") readfds = [] writefds = [] print("[2]add server to fd set") readfds.append(s) print("[2]add client socket to fd set") for i in client: if len(msg) == 0: readfds.append(i) else: writefds.append(i) print("[3]call select() and waiting") act = select.select(readfds, writefds, [], 5) #print(type(act)) print("[4]wake up from select()") if act == ([], [], []): print("select() is waiting for 5 sec") else: for i in client: if i in readfds: data = i.recv(1024) msg = data.decode() print("recv from client", client.index(i), ': ', data.decode()) if i in writefds: i.sendall(msg.encode()) msg = ''

Client

import socket import time HOST = '127.0.0.1' PORT = 8000 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) word = "How are you?" while True: try: s.connect((HOST, PORT)) except: print("err") time.sleep(1) else: print('Connected to', HOST) s.setblocking(0) while True: time.sleep(10) try: s.sendall(word.encode()) print("Send:", word, "(in 10 sec)") data = s.recv(1024) print("recv:", data.decode()) except: pass

心得

自從有附上python的程式碼之後,就能比較輕鬆地做完了