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