└── lab1 ├── sample ├── main.c └── lab1_test_script.py /lab1/sample: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZE3kr/ECE_356_SkeletonCode/main/lab1/sample -------------------------------------------------------------------------------- /lab1/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int server(uint16_t port); 10 | int client(const char * addr, uint16_t port); 11 | 12 | #define MAX_MSG_LENGTH (1300) 13 | #define MAX_BACK_LOG (5) 14 | 15 | int main(int argc, char ** argv) 16 | { 17 | if (argc < 3 || (argv[1][0] == 'c' && argc < 4)) { 18 | printf("usage: myprog c
or myprog s \n"); 19 | return 0; 20 | } 21 | 22 | uint32_t number = atoi(argv[2]); 23 | if (number < 1024 || number > 65535) { 24 | fprintf(stderr, "port number should be larger than 1023 and less than 65536\n"); 25 | return 0; 26 | } 27 | 28 | uint16_t port = atoi(argv[2]); 29 | 30 | if (argv[1][0] == 'c') { 31 | return client(argv[3], port); 32 | } else if (argv[1][0] == 's') { 33 | return server(port); 34 | } else { 35 | fprintf(stderr, "unkonwn commend type %s\n", argv[1]); 36 | return 0; 37 | } 38 | return 0; 39 | } 40 | 41 | int client(const char * addr, uint16_t port) 42 | { 43 | int sock; 44 | struct sockaddr_in server_addr; 45 | char msg[MAX_MSG_LENGTH], reply[MAX_MSG_LENGTH]; 46 | 47 | if ((sock = socket(AF_INET, SOCK_STREAM/* use tcp */, 0)) < 0) { 48 | perror("Create socket error:"); 49 | return 1; 50 | } 51 | 52 | printf("Socket created\n"); 53 | server_addr.sin_addr.s_addr = inet_addr(addr); 54 | server_addr.sin_family = AF_INET; 55 | server_addr.sin_port = htons(port); 56 | 57 | if (connect(sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) { 58 | perror("Connect error:"); 59 | return 1; 60 | } 61 | 62 | printf("Connected to server %s:%d\n", addr, port); 63 | printf("Enter message: \n"); 64 | while (fgets(msg, MAX_MSG_LENGTH, stdin)) { 65 | if (send(sock, msg, strnlen(msg, MAX_MSG_LENGTH), 0) < 0) { 66 | perror("Send error:"); 67 | return 1; 68 | } 69 | int recv_len = 0; 70 | if ((recv_len = recv(sock, reply, MAX_MSG_LENGTH, 0)) < 0) { 71 | perror("Recv error:"); 72 | return 1; 73 | } 74 | if (recv_len == 0){ 75 | printf("Server disconnected, client quit.\n"); 76 | break; 77 | } 78 | reply[recv_len] = '\0'; 79 | printf("Server reply:\n%s", reply); 80 | printf("Enter message: \n"); 81 | } 82 | if (send(sock, "", 0, 0) < 0) { 83 | perror("Send error:"); 84 | return 1; 85 | } 86 | return 0; 87 | } 88 | 89 | int server(uint16_t port) 90 | { 91 | /* 92 | Add your code here 93 | */ 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /lab1/lab1_test_script.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | import sys 3 | import time 4 | import pexpect 5 | import random 6 | import string 7 | import threading 8 | 9 | 10 | class Server(threading.Thread): 11 | def __init__(self, prog_path, port_number): 12 | super(Server, self).__init__() 13 | self.prog_path = prog_path 14 | self.controler = None 15 | self.port_number = port_number 16 | 17 | def run(self): 18 | self.controler = pexpect.spawn("%s s %d" % (self.prog_path, self.port_number)) 19 | 20 | while True: 21 | try: 22 | self.controler.expect("\n\r\t\n\n", timeout=2) 23 | except pexpect.TIMEOUT: 24 | continue 25 | except pexpect.EOF: 26 | break 27 | 28 | def close(self): 29 | self.controler.sendcontrol('c') 30 | 31 | 32 | def check_binary_valid(prog_path): 33 | try: 34 | client = pexpect.spawn("%s" % prog_path) 35 | except pexpect.ExceptionPexpect: 36 | return 0 37 | try: 38 | client.expect("usage: myprog c
or myprog s ", timeout=2) 39 | client.close() 40 | return 1 41 | except pexpect.TIMEOUT, pexpect.EOF: 42 | client.close() 43 | return 0 44 | 45 | 46 | if __name__ == '__main__': 47 | if len(sys.argv) < 2: 48 | print 'Usage: python lab1_test_script.py myprog_path' 49 | else: 50 | prog_path = sys.argv[1] 51 | if not check_binary_valid(prog_path): 52 | print 'Please input a valid path of your binary program ' 53 | else: 54 | messages = [ 55 | 'test case 1', 56 | 'test case 2: COMPSCI 356 i\f\\\'\"\as an undergraduate course in computer science teaching the fundamentals of computer networks. We will cover the technologies supporting the Internet, from Ethernet and WiFi through the routing protocols that govern the flow of traffic, and the web technologies that generate most of it.Topics: The topics we will study include but are not limited to: how to achieve reliable communications over unreliable channels, how to find a good path through a network, how to share networ', 57 | [], 58 | "EOF" 59 | ] 60 | port_number = random.randint(1024, 65535) 61 | server = Server(prog_path, port_number) 62 | server.start() 63 | time.sleep(2) 64 | 65 | def send_message(client_handle, message): 66 | if message != 'EOF': 67 | client_handle.sendline(message) 68 | else: 69 | client_handle.sendcontrol('d') 70 | if message != 'EOF': 71 | try: 72 | client_handle.expect("Enter message:", timeout=4) 73 | response = client_handle.before.replace("\r\n", "\n")[:-1] 74 | reply = response.split("\n")[-1] 75 | if message == reply: 76 | return 1 77 | else: 78 | return 0 79 | except pexpect.TIMEOUT, pexpect.EOF: 80 | return 0 81 | else: 82 | try: 83 | client_handle.expect('Enter message:', timeout=4) 84 | return 0 85 | except pexpect.TIMEOUT: 86 | return 0 87 | except pexpect.EOF: 88 | return 1 89 | 90 | def start_client(client_number): 91 | client = pexpect.spawn("%s c %d 127.0.0.1" % (prog_path, port_number)) 92 | try: 93 | i = client.expect(["Enter message:", 'Connection refused'], timeout=4) 94 | if i == 1: 95 | print 'Client%d: Connection refused.' % client_number 96 | return 0 97 | except pexpect.TIMEOUT, pexpect.EOF: 98 | print 'Client%d: cannot connect to the server.' % client_number 99 | return 0 100 | 101 | results = [1, 1, 1, 1] 102 | for i in range(len(messages)): 103 | message_list = [messages[i]] 104 | if i == 2: 105 | message_list = [] 106 | for j in range(20): 107 | message_list.append(''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(512))) 108 | for message in message_list: 109 | result = send_message(client, message) 110 | if result == 0: 111 | results[i] = 0 112 | break 113 | for i in range(4): 114 | if i == 0: 115 | print 'Client%d: Testing short word:' % client_number, 116 | elif i == 1: 117 | print ' Testing long sentence:', 118 | elif i == 2: 119 | print ' Testing multiple sentences:', 120 | else: 121 | print ' Testing EOF:', 122 | if results[i] == 0: 123 | print '\033[1;31;40mFAILED\033[0m' 124 | else: 125 | print '\033[1;32;40mPASSED\033[0m' 126 | client.close() 127 | if sum(results) == 4: 128 | return 1 129 | else: 130 | return 0 131 | 132 | if start_client(1): 133 | start_client(2) 134 | 135 | server.close() 136 | 137 | --------------------------------------------------------------------------------