1. 程式人生 > >TCP 端口聯通測試

TCP 端口聯通測試

accept line error -s == lose con eas can

  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 端口聯通測試