int main(){ int listenfd; listenfd = socket_bind(HOST, PORT); listen(listenfd, LISTENQ); do_epoll(listenfd); return 0; }
int main(int argc, char **argv) { init_daemon(argv[0],LOG_INFO); if(get_arg("home_dir")==0) { sprintf(home_dir,"%s","/tmp"); } if(get_arg("ip")==0) { get_addr("eth0"); } if(get_arg("port")==0) { sprintf(port,"%s","80"); } if(get_arg("back")==0) { sprintf(back,"%s","5"); } int sock_fd = socket_bind(ip, atoi((const char *)&port)); do_epoll(sock_fd); close(sock_fd); return 0; }
int main(int argc, const char *argv[]) { if(signal(SIGPIPE, SIG_IGN) == SIG_ERR) ERR_EXIT("signal"); int listenfd = socket(PF_INET, SOCK_STREAM, 0); if(listenfd == -1) ERR_EXIT("listefd"); int on = 1; if(setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) ERR_EXIT("SETSOCKET"); struct sockaddr_in servaddr; servaddr.sin_family = AF_INET; servaddr.sin_port = htons(6666); servaddr.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) { ERR_EXIT("bind"); } if(listen(listenfd, SOMAXCONN) < 0) ERR_EXIT("listen"); do_epoll(listenfd); close(listenfd); return 0; }
//main函数 int main( int argc, char* argv[] ) { int listenfd = socket_bind( IPADDRESS, PORT ); ::listen( listenfd, LISTENQ ); do_epoll( listenfd ); return 0; }
int main(int argc, const char *argv[]) { int listenfd = listenf(); do_epoll(listenfd); close(listenfd); return 0; }
int main(int argc, char *argv[]) { int listenfd; listenfd = socket_bind(IPADDRESS, PORT); listen(listenfd, LISTENQ); do_epoll(listenfd); return EXIT_SUCCESS; }
int main(int argc, const char *argv[]) { if(signal(SIGPIPE, SIG_IGN) == SIG_ERR) ERR_EXIT("signal"); int fd_server; preprocess(&fd_server); do_epoll(fd_server); close(fd_server); return 0; }
int main(int argc, const char *argv[]) { if(signal(SIGPIPE, SIG_IGN) == SIG_ERR) ERR_EXIT("signal"); int listenfd = get_listen_fd(); do_epoll(listenfd); close(listenfd); return 0; }
int main(){ int fd, cli_sockfd; int addlen; int n; char recvline[MAXSIZE],sendline[MAXSIZE]; struct sockaddr_in serv_in; struct sockaddr_in cli_addr; fd = socket(AF_INET, SOCK_STREAM, 0); if(fd == -1){ perror("socket error: "); exit(1); } bzero(&serv_in, sizeof(serv_in)); serv_in.sin_family = AF_INET; inet_pton(AF_INET, IPADDR, &serv_in.sin_addr); serv_in.sin_port = htons(PORT); if(bind(fd, (struct sockaddr *)&serv_in, sizeof(serv_in)) == -1) { perror("bind error: "); exit(1); } if(listen(fd, 5) == -1){ perror("listen error: "); exit(1); } printf("listen the port: "); do_epoll(fd); /*for( ; ; ){ addlen = sizeof(struct sockaddr); cli_sockfd = accept(fd ,(struct sockaddr *)&cli_addr, &addlen); if(cli_sockfd == -1) { perror("accept error: "); //exit(1); } while(1){ printf("waiting for client...\n"); n = recv(cli_sockfd, recvline, 1024, 0); if(n == -1){ printf("recv error\n"); } recvline[n] = '\n'; printf("recv data is:%s\n", recvline); sprintf(sendline, "%s", "send msg"); printf("%s", sendline); send(cli_sockfd, sendline, strlen(sendline), 0); } close(cli_sockfd); } close(fd);*/ return 0; }
void Server_Thread() { listenfd = socket(AF_INET, SOCK_STREAM,0); if(listenfd < 0) ERR_EXIT("listenfd created ERR"); struct sockaddr_in serveraddr; bzero((char *)&serveraddr,sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); serveraddr.sin_port = htons(PORT); if(bind(listenfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr)) < 0) ERR_EXIT("bind ERR"); if(listen(listenfd,1024) < 0) ERR_EXIT("listen ERR"); do_epoll(listenfd); /*struct sockaddr_in clientaddr; int connfd; socklen_t clientlen = sizeof(clientaddr); bool addbool; socklen_t serv_len = sizeof(servaddr); while(true) { connfd = accept(listenfd,(struct sockaddr *)&clientaddr, &clientlen); if(connfd < 0) { do_log("accept ERR"); continue; } if(!servaddr_init) { getsockname(connfd, (struct sockaddr *)&servaddr, &serv_len); // do_log(inet_ntoa(servaddr.sin_addr)); servaddr_init=true; } char log[50]; sprintf(log,"accept a client IP:%s,port:%d",inet_ntoa(clientaddr.sin_addr),ntohs(clientaddr.sin_port)); do_log(log); Client_tmp *mClient_tm=(Client_tmp*)malloc(sizeof(Client_tmp)); mClient_tm->client=NULL; mClient_tm->sockfd=connfd; mClient_tm->msockaddr_in=clientaddr; addbool=mThreadsManager->AddJob((void *(*)(void *))recvFromClient,(void *)mClient_tm); if(!addbool) do_log("mThreadsManager->AddJob ERR"); }*/ }
int main(int argc, char *argv[]) { int listenfd; struct sockaddr_in server_addr; int opt = 1; //套接字选项 int result; listenfd = socket(AF_INET, SOCK_STREAM, 0); if(listenfd == -1){ perror("socket error"); exit(1); } setNonblocking(listenfd); memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl(INADDR_ANY); //接受任意IP server_addr.sin_port = htons(PORT); //设置socket状态 //SOL_SOCKET:存取socket层, SO_REUSEADDR:允许在bind()过程中本地址可重复使用 setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); //绑定套接字 result = bind(listenfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); if(result == -1){ perror("socket bind error"); exit(1); } result = listen(listenfd, LISTENQ); if(result == -1){ perror("socket listen error"); exit(1); } do_epoll(listenfd); return 0; }
int main(int argc, char **argv) { int i, npass, ninit, nstep; unsigned long elapsed = 0; struct sockaddr_in saddr; if(5 != argc) { fprintf(stderr, "Usage: %s <npass> <nceil> <ninit> <nstep>\n\n" "<npass> pass of calls of select/poll/epoll in a test round.\n" "<nceil> max number of fds to handle.\n" "<ninit> initial number of fds to handle.\n" "<nstep> incremental number of fds for a next test.\n", argv[0]); exit(1); } npass = atoi(argv[1]); nceil = atoi(argv[2]); ninit = atoi(argv[3]); nstep = atoi(argv[4]); /* alloc memory */ fds = calloc(nceil, sizeof(int)); pfds = calloc(nceil, sizeof(struct pollfd)); evs = calloc(nceil, sizeof(struct epoll_event)); rdset = calloc(nceil, 8); assert(fds && pfds && evs && rdset); /* (nceil-1) sockets which have no data to receive */ memset(&saddr, 0, sizeof(saddr)); saddr.sin_family = AF_INET; saddr.sin_port = htons(12345); inet_pton(AF_INET, "127.0.0.1", &saddr.sin_addr); for(i = 0; i < nceil - 1; i++) { fprintf(stderr, "build connection #%d\n", i); fds[i] = socket(AF_INET, SOCK_STREAM, 0); assert(fds[i] >= 0); assert(0 == connect(fds[i], (struct sockaddr *)&saddr, sizeof(saddr))); } /* the only socket that data would be received from */ saddr.sin_port = htons(12346); fds[i] = socket(AF_INET, SOCK_STREAM, 0); assert(fds[i] >= 0); assert(0 == connect(fds[i], (struct sockaddr *)&saddr, sizeof(saddr))); rdset_nbytes = MY_FD_SET_NBYTES(fds[i]); int round = 0; for(ncur = ninit; ncur <= nceil; ncur += nstep) { //fprintf(stderr, "round with number of fds: %d\n", ncur); result[0][round] = ncur; elapsed = 0; for(i = 0; i < npass; i++) { while(!is_ready()); elapsed += do_select(); } result[1][round] = elapsed / npass; //fprintf(stderr, "elapsed time of select(): %dus\n", elapsed / npass); elapsed = 0; for(i = 0; i < npass; i++) { while(!is_ready()); elapsed += do_poll(); } result[2][round] = elapsed / npass; //fprintf(stderr, "elapsed time of poll(): %dus\n", elapsed / npass); elapsed = 0; for(i = 0; i < npass; i++) { while(!is_ready()); elapsed += do_epoll(); } result[3][round] = elapsed / npass; //fprintf(stderr, "elapsed time of epoll(): %dus\n", elapsed / npass); round++; } printf("# #fds\tselect\tpoll\tepoll\n"); for(i = 0; i < round; i++) { printf("%lu\t%lu\t%lu\t%lu\n", result[0][i], result[1][i], result[2][i], result[3][i]); } for(i = 0; i < nceil; i++) { close(fds[i]); } free(fds); free(pfds); free(evs); free(rdset); }