/**-------------------------------------------------------* 测试在同一个sock fd上, 连续多次调用bind, listen.* 验证找一个可用端口号, 以及出错处是在socket, bind, or listen*/#include <unistd.h>#include <errno.h>#include <sys/types.h> /* See NOTES */#include <sys/socket.h>#include <stdio.h>#include <arpa/inet.h>using namespace std;typedef sockaddr SA;int socket_create(const char* ip, const int port){if (ip == NULL || port < 0) {return -1;}/* create socket */int sock = socket(AF_INET, SOCK_STREAM, 0);if (sock < 0) {perror("socket error");return -1;}/* set sock option SO_REUSEADDR for re-bind when calling socket failed */int op = 1;setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &op, sizeof(op));struct sockaddr_in local;local.sin_family = AF_INET;local.sin_port = htons(static_cast<uint16_t>(port));local.sin_addr.s_addr = inet_addr(ip);if (bind(sock, reinterpret_cast<SA*>(&local), sizeof(local)) < 0) {perror("bind error");return -1;}if (listen(sock, 10) < 0) {perror("listen error");return -1;}return sock;}int test_bindTwoTimes(const char* ip, const int port){if (ip == NULL || port < 0) {return -1;}/* create socket */int sock = socket(AF_INET, SOCK_STREAM, 0);if (sock < 0) {perror("socket error");return -1;}/* set sock option SO_REUSEADDR for re-bind when calling socket failed */int op = 1;setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &op, sizeof(op));struct sockaddr_in local;local.sin_family = AF_INET;local.sin_port = htons(static_cast<uint16_t>(port));local.sin_addr.s_addr = inet_addr(ip);if (bind(sock, reinterpret_cast<SA*>(&local), sizeof(local)) < 0) { /* first time */perror("bind1 error");return -1;}if (bind(sock, reinterpret_cast<SA*>(&local), sizeof(local)) < 0) { /* second time */perror("bind2 error");return -1;}if (listen(sock, 10) < 0) {perror("bind error");return -1;}return sock;}int test_listenTwoTimes(const char* ip, const int port){if (ip == NULL || port < 0) {return -1;}/* create socket */int sock = socket(AF_INET, SOCK_STREAM, 0);if (sock < 0) {perror("socket error");return -1;}/* set sock option SO_REUSEADDR for re-bind when calling socket failed */int op = 1;setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &op, sizeof(op));struct sockaddr_in local;local.sin_family = AF_INET;local.sin_port = htons(static_cast<uint16_t>(port));local.sin_addr.s_addr = inet_addr(ip);if (bind(sock, reinterpret_cast<SA*>(&local), sizeof(local)) < 0) {perror("bind error");return -1;}if (listen(sock, 10) < 0) {perror("listen1 error");return -1;}if (listen(sock, 10) < 0) {perror("listen2 error");return -1;}return sock;}int main(){printf("Test on different socket fd\n");{printf("Test Case: background\n");const char* ip = "0.0.0.0";int port = 1025; /* 使用well-known端口号(0~1023)需要root权限*/int sockfd = socket_create(ip, port);if (sockfd >= 0) {printf("Success to create sockfd %d on %s:%d\n", sockfd, ip, port);}else {printf("Fail to create sockfd %d on %s:%d\n", sockfd, ip, port);}close(sockfd);}{printf("Test Case1: bind two times on same sockfd\n");const char* ip = "0.0.0.0";int port = 1025;int sockfd = test_bindTwoTimes(ip, port);if (sockfd >= 0) {printf("Success to create sockfd %d on %s:%d\n", sockfd, ip, port);}else {printf("Fail to create sockfd %d on %s:%d\n", sockfd, ip, port);}close(sockfd);}{printf("Test Case2: bind two times on same sockfd\n");const char* ip = "0.0.0.0";int port = 65536;int sockfd = test_bindTwoTimes(ip, port);if (sockfd >= 0) {printf("Success to create sockfd %d on %s:%d\n", sockfd, ip, port);}else {printf("Fail to create sockfd %d on %s:%d\n", sockfd, ip, port);}close(sockfd);}{printf("Test Case1: listen two times on same sockfd\n");const char* ip = "0.0.0.0";int port = 1025;int sockfd = test_listenTwoTimes(ip, port);if (sockfd >= 0) {printf("Success to create sockfd %d on %s:%d\n", sockfd, ip, port);}else {printf("Fail to create sockfd %d on %s:%d\n", sockfd, ip, port);}close(sockfd);}{printf("Test Case2: listen two times on same sockfd\n");const char* ip = "0.0.0.0";int port = 65536;int sockfd = test_listenTwoTimes(ip, port);if (sockfd >= 0) {printf("Success to create sockfd %d on %s:%d\n", sockfd, ip, port);}else {printf("Fail to create sockfd %d on %s:%d\n", sockfd, ip, port);}close(sockfd);}///////////////////printf("\nTest on same socket fd\n");{printf("Test Case: background\n");const char* ip = "0.0.0.0";int port = 1025; /* 使用well-known端口号(0~1023)需要root权限*/int sockfd = socket_create(ip, port);if (sockfd >= 0) {printf("Success to create sockfd %d on %s:%d\n", sockfd, ip, port);}else {printf("Fail to create sockfd %d on %s:%d\n", sockfd, ip, port);}}{printf("Test Case1: bind two times on same sockfd\n");const char* ip = "0.0.0.0";int port = 1025;int sockfd = test_bindTwoTimes(ip, port);if (sockfd >= 0) {printf("Success to create sockfd %d on %s:%d\n", sockfd, ip, port);}else {printf("Fail to create sockfd %d on %s:%d\n", sockfd, ip, port);}}{printf("Test Case2: bind two times on same sockfd\n");const char* ip = "0.0.0.0";int port = 65536;int sockfd = test_bindTwoTimes(ip, port);if (sockfd >= 0) {printf("Success to create sockfd %d on %s:%d\n", sockfd, ip, port);}else {printf("Fail to create sockfd %d on %s:%d\n", sockfd, ip, port);}}{printf("Test Case1: listen two times on same sockfd\n");const char* ip = "0.0.0.0";int port = 1025;int sockfd = test_listenTwoTimes(ip, port);if (sockfd >= 0) {printf("Success to create sockfd %d on %s:%d\n", sockfd, ip, port);}else {printf("Fail to create sockfd %d on %s:%d\n", sockfd, ip, port);}}{printf("Test Case2: listen two times on same sockfd\n");const char* ip = "0.0.0.0";int port = 65536;int sockfd = test_listenTwoTimes(ip, port);if (sockfd >= 0) {printf("Success to create sockfd %d on %s:%d\n", sockfd, ip, port);}else {printf("Fail to create sockfd %d on %s:%d\n", sockfd, ip, port);}}return 0;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。