main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include <arpa/inet.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sys/socket.h>
  6. #include <sys/stat.h>
  7. #include <unistd.h>
  8. #define PORT 12580
  9. #define MAX_REQUEST_SIZE 1500
  10. #define MAX_RESPONSE_SIZE 1500
  11. #define EXIT_CODE 1
  12. #define min(a, b) ((a) < (b) ? (a) : (b))
  13. struct sockaddr_in address;
  14. int addrlen = sizeof(address);
  15. char buffer[MAX_REQUEST_SIZE] = {0};
  16. int opt = 1;
  17. void test_tcp_bind()
  18. {
  19. int tcp_sk_fd1, tcp_sk_fd2, tcp_sk_fd3;
  20. // create tcp sockets
  21. if ((tcp_sk_fd1 = socket(AF_INET, SOCK_STREAM, 0)) == 0)
  22. {
  23. perror("tcp socket (1) failed");
  24. exit(EXIT_CODE);
  25. }
  26. if ((tcp_sk_fd2 = socket(AF_INET, SOCK_STREAM, 0)) == 0)
  27. {
  28. perror("tcp socket (2) failed");
  29. exit(EXIT_CODE);
  30. }
  31. if ((tcp_sk_fd3 = socket(AF_INET, SOCK_STREAM, 0)) == 0)
  32. {
  33. perror("tcp socket (3) failed");
  34. exit(EXIT_CODE);
  35. }
  36. // TEST tcp bind diff ports
  37. if (bind(tcp_sk_fd1, (struct sockaddr *)&address, sizeof(address)) < 0)
  38. {
  39. perror("tcp bind (1) failed");
  40. exit(EXIT_CODE);
  41. }
  42. address.sin_port = htons(PORT+1);
  43. if (bind(tcp_sk_fd2, (struct sockaddr *)&address, sizeof(address)) < 0)
  44. {
  45. perror("tcp bind (2) failed");
  46. exit(EXIT_CODE);
  47. }
  48. printf("===TEST 4 PASSED===\n");
  49. // TEST tcp bind same ports
  50. address.sin_port = htons(PORT);
  51. if (bind(tcp_sk_fd3, (struct sockaddr *)&address, sizeof(address)) < 0)
  52. {
  53. perror("tcp bind (3) failed");
  54. // exit(EXIT_CODE);
  55. }
  56. printf("===TEST 5 PASSED===\n");
  57. if (close(tcp_sk_fd1) < 0)
  58. {
  59. perror("tcp close (1) failed");
  60. exit(EXIT_CODE);
  61. }
  62. if (close(tcp_sk_fd2) < 0)
  63. {
  64. perror("tcp close (2) failed");
  65. exit(EXIT_CODE);
  66. }
  67. if (close(tcp_sk_fd3) < 0)
  68. {
  69. perror("tcp close (3) failed");
  70. exit(EXIT_CODE);
  71. }
  72. printf("===TEST 6 PASSED===\n");
  73. }
  74. void test_udp_bind()
  75. {
  76. int udp_sk_fd1, udp_sk_fd2, udp_sk_fd3;
  77. // create tcp sockets
  78. if ((udp_sk_fd1 = socket(AF_INET, SOCK_DGRAM, 0)) == 0)
  79. {
  80. perror("udp socket (1) failed");
  81. exit(EXIT_CODE);
  82. }
  83. if ((udp_sk_fd2 = socket(AF_INET, SOCK_DGRAM, 0)) == 0)
  84. {
  85. perror("udp socket (2) failed");
  86. exit(EXIT_CODE);
  87. }
  88. if ((udp_sk_fd3 = socket(AF_INET, SOCK_DGRAM, 0)) == 0)
  89. {
  90. perror("udp socket (3) failed");
  91. exit(EXIT_CODE);
  92. }
  93. // TEST udp bind diff ports
  94. if (bind(udp_sk_fd1, (struct sockaddr *)&address, sizeof(address)) < 0)
  95. {
  96. perror("udp bind (1) failed");
  97. exit(EXIT_CODE);
  98. }
  99. address.sin_port = htons(PORT+1);
  100. if (bind(udp_sk_fd2, (struct sockaddr *)&address, sizeof(address)) < 0)
  101. {
  102. perror("udp bind (2) failed");
  103. exit(EXIT_CODE);
  104. }
  105. printf("===TEST 7 PASSED===\n");
  106. // TEST udp bind same ports
  107. address.sin_port = htons(PORT);
  108. if (bind(udp_sk_fd3, (struct sockaddr *)&address, sizeof(address)) < 0)
  109. {
  110. perror("udp bind (3) failed");
  111. // exit(EXIT_CODE);
  112. }
  113. printf("===TEST 8 PASSED===\n");
  114. if (close(udp_sk_fd1) < 0)
  115. {
  116. perror("udp close (1) failed");
  117. exit(EXIT_CODE);
  118. }
  119. if (close(udp_sk_fd2) < 0)
  120. {
  121. perror("udp close (2) failed");
  122. exit(EXIT_CODE);
  123. }
  124. if (close(udp_sk_fd3) < 0)
  125. {
  126. perror("udp close (3) failed");
  127. exit(EXIT_CODE);
  128. }
  129. printf("===TEST 9 PASSED===\n");
  130. }
  131. void test_all_ports() {
  132. int count = 0;
  133. while (1) {
  134. int tcp_fd;
  135. if ((tcp_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
  136. {
  137. perror("socket failed");
  138. exit(EXIT_CODE);
  139. }
  140. address.sin_port = htons(0);
  141. if (bind(tcp_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
  142. {
  143. perror("bind failed");
  144. // exit(EXIT_CODE);
  145. break;
  146. }
  147. count++;
  148. }
  149. printf("===TEST 10===\n");
  150. printf("count: %d\n", count);
  151. }
  152. int main(int argc, char const *argv[])
  153. {
  154. int server_fd;
  155. int udp_sk_fd;
  156. // 创建socket
  157. if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
  158. {
  159. perror("tcp socket failed");
  160. exit(EXIT_CODE);
  161. }
  162. if ((udp_sk_fd = socket(AF_INET, SOCK_DGRAM, 0)) == 0)
  163. {
  164. perror("udp socket failed");
  165. exit(EXIT_CODE);
  166. }
  167. // 设置地址和端口
  168. address.sin_family = AF_INET;
  169. address.sin_addr.s_addr = INADDR_ANY;
  170. address.sin_port = htons(PORT);
  171. // TEST socket's bind
  172. if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
  173. {
  174. perror("tcp bind failed");
  175. exit(EXIT_CODE);
  176. }
  177. address.sin_port = htons(PORT);
  178. if (bind(udp_sk_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
  179. {
  180. perror("udp bind failed");
  181. exit(EXIT_CODE);
  182. }
  183. printf("===TEST 1 PASSED===\n");
  184. // TEST socket's listen
  185. if (listen(server_fd, 3) < 0)
  186. {
  187. perror("listen failed");
  188. exit(EXIT_CODE);
  189. }
  190. printf("===TEST 2 PASSED===\n");
  191. // TEST socket's close
  192. if (close(server_fd) < 0)
  193. {
  194. perror("tcp close failed");
  195. exit(EXIT_CODE);
  196. }
  197. if (close(udp_sk_fd) < 0)
  198. {
  199. perror("udp close failed");
  200. exit(EXIT_CODE);
  201. }
  202. printf("===TEST 3 PASSED===\n");
  203. test_tcp_bind();
  204. test_udp_bind();
  205. test_all_ports();
  206. return 0;
  207. }