标签:inet_addr main scanf null inf luci lsp 测试的 pac
1 #include <stdio.h> 2 #include 3 #include 4 #include 5 #include 6 7 int 8 main(int argc, char **argv) { 9 if (argc != 2) { 10 printf("选择一个需要测试的端口, Please!\n"); 11 return 0; 12 } 13 int fd = socket(AF_INET, SOCK_STREAM, 0); 14 if (fd < 0) { 15 perror(*argv); 16 return 0; 17 } 18 struct sockaddr_in addr = { 0 }; 19 addr.sin_family = AF_INET; 20 int port; 21 if (sscanf(argv[1], "%d", &port) == 0) { 22 printf("端口号错误!\n"); 23 return 0; 24 } 25 printf("INFO: port %d\n", port); 26 addr.sin_port = htons(port); 27 addr.sin_addr.s_addr = inet_addr("0.0.0.0"); 28 if (bind(fd, (void *)&addr, sizeof addr) != 0) { 29 perror(*argv); 30 return 0; 31 } 32 if (listen(fd, 10) != 0) { 33 perror(*argv); 34 return 0; 35 } 36 while (1) { 37 int conn = accept(fd, NULL, NULL); 38 write(conn, "Hello", 5); 39 close(conn); 40 } 41 return 0; 42 } 43
TCP 端口联通测试
原文地址:https://www.cnblogs.com/develon/p/10031649.html