#include <stdio.h> #include <string.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #include <errno.h> #include <signal.h> #define SERV_PORT 8000 int main(int argc, const char * argv[]) { struct sockaddr_in serverAdd; struct sockaddr_in clientAdd; bzero(&serverAdd, sizeof(serverAdd)); serverAdd.sin_family = AF_INET; serverAdd.sin_addr.s_addr = htonl(INADDR_ANY); serverAdd.sin_port = htons(SERV_PORT); socklen_t clientAddrLen; int listenfd = socket(AF_INET, SOCK_STREAM, 0); int yes = 1; setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes)); if (listenfd < 0) { printf("创建socket失败 "); return -1; } int bindResult = bind(listenfd, (struct sockaddr *)&serverAdd, sizeof(serverAdd)); if (bindResult < 0) { printf("绑定端口失败 "); close(listenfd); return -1; } listen(listenfd, 20); int connfd; unsigned char recvMsg[]; unsigned long long totalSize = 0; clientAddrLen = sizeof(clientAdd); connfd = accept(listenfd,(struct sockaddr *)&clientAdd,&clientAddrLen); if (connfd < 0) { print#include <stdio.h> #include <string.h> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #include <errno.h> #include <signal.h> #define SERV_PORT 8000 int main(int argc, const char * argv[]) { struct sockaddr_in serverAdd; struct sockaddr_in clientAdd; bzero(&serverAdd, sizeof(serverAdd)); serverAdd.sin_family = AF_INET; serverAdd.sin_addr.s_addr = htonl(INADDR_ANY); serverAdd.sin_port = htons(SERV_PORT); socklen_t clientAddrLen; int listenfd = socket(AF_INET, SOCK_STREAM, 0); int yes = 1; setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (void *)&yes, sizeof(yes)); if (listenfd < 0) { printf("创建socket失败 "); return -1; } int bindResult = bind(listenfd, (struct sockaddr *)&serverAdd, sizeof(serverAdd)); if (bindResult < 0) { printf("绑定端口失败 "); close(listenfd); return -1; } listen(listenfd, 20); int connfd; unsigned char recvMsg[]; unsigned long long totalSize = 0; clientAddrLen = sizeof(clientAdd); connfd = accept(listenfd,(struct sockaddr *)&clientAdd,&clientAddrLen); if (connfd < 0) { printf("连接失败 "); return -1; } else{ // 这里我们用于测试,只接收一个连接 close(listenfd); } close(connfd); return 0; }f("连接失败 "); return -1; } else{ // 这里我们用于测试,只接收一个连接 close(listenfd); } close(connfd); return 0; }
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.mushiming.com/mjsbk/11374.html