int main(int argc, char **argv) { int port, listenfd, connfd; struct sockaddr_in clientaddr; int clientlen = sizeof(struct sockaddr_in); Pool pool; if(argc != 2){ fprintf(stderr, "Usage: %s <port>\n", argv[0]); exit(0); } port = atoi(argv[1]); listenfd = open_listenfd(port); init_pool(listenfd, &pool); while(1){ /* wait for listening/connected descriptors to become ready */ pool.ready_set = pool.read_set; pool.nready = select(pool.maxfd + 1, &pool.ready_set, NULL, NULL, NULL); /* If listening descriptor become ready, add new client to pool */ if(FD_ISSET(listenfd, &pool.ready_set)){ connfd = accept(listenfd, (SA *)&clientaddr, &clientlen); add_client(connfd, &pool); } /* echo a textline from each ready connected descriptor */ check_clients(&pool); } return 0; }
int main(int argc, char **argv) { int listenfd, *connfdp; char* port; unsigned int clientlen=sizeof(struct sockaddr_in); struct sockaddr_in clientaddr; pthread_t tid; get_conf(); /* if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(0); } */ port = portOpt; listenfd = open_listenfd(port); while (1) { connfdp = malloc(sizeof(int)); *connfdp = accept(listenfd, (struct sockaddr*)&clientaddr, &clientlen); pthread_create(&tid, NULL, thread, connfdp); } destroy_conf(); }
int main(int argc, char *argv[]) { int listenfd, connfd, port, clientlen; struct sockaddr_in clientaddr; char *clientaddress; if(argc != 3) { fprintf(stderr, "usage: %s <port> <filename>\n", argv[0]); } port = atoi(argv[1]); listenfd = open_listenfd(port); while(1) { clientlen = sizeof(clientaddr); connfd = accept(listenfd, (struct sockaddr *)&clientaddr, (socklen_t *)&clientlen); clientaddress = inet_ntoa(clientaddr.sin_addr); printf("server connect to %s \n", clientaddress); ser_action(connfd, argv[2]); close(connfd); } exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { int listenfd,port,clientlen; int *connfdp; pthread_t tid; struct sockaddr_in clientaddr; clientlen = sizeof(clientaddr); if(argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(1); } //init signal(SIGPIPE, SIG_IGN); sem_init(&mutex, 0, 1); num_blocked_address = read_blocked_address(); logfd = log_file_init(); //listen port = atoi(argv[1]); listenfd = open_listenfd(port); while(1) { connfdp = malloc(sizeof(int)); *connfdp = accept(listenfd, (SA*)&clientaddr, &clientlen); pthread_create(&tid, NULL, thread, connfdp); } }
int Open_listenfd(int port) { int rc; if ((rc = open_listenfd(port)) < 0) unix_error("Open_listenfd error"); return rc; }
int Open_listenfd(int port) { int rt; rt = open_listenfd(port); if( rt == -1) unix_error("open_listenfd error"); return rt; }
int web_open_listenfd(char *port) { int rc; if ((rc = open_listenfd(port)) < 0) web_error("Open_listenfd error"); return rc; }
int main(void) { default_port = DEFAULT_PORT; int listenfd; char *pPort = getenv("PORT"); if (pPort != NULL) default_port = (u_short) strtol(pPort, (char **)NULL, 10); listenfd = open_listenfd(default_port); if (listenfd > 0) { dbgprintf("listen on port %d, fd is %d\n", default_port, listenfd); } else { perror("ERROR"); exit(listenfd); } /*Ignore SIGPIPE signal, so if browser cancels the request, it won't kill the whole process. */ signal(SIGPIPE, SIG_IGN); #ifdef NOPE_THREADS struct rlimit limit; limit.rlim_cur = MAX_NO_FDS * 4; limit.rlim_max = MAX_NO_FDS * 4; setrlimit(RLIMIT_NOFILE, &limit); /* Get max number of files. */ getrlimit(RLIMIT_NOFILE, &limit); dbgprintf("The soft limit is %llu\n", limit.rlim_cur); dbgprintf("The hard limit is %llu\n", limit.rlim_max); initialize_threads(); #endif #ifdef NOPE_PROCESSES for (i = 0; i < NOPE_PROCESSES; i++) { int pid = fork(); if (pid == 0) { // child select_loop(listenfd); } else if (pid > 0) { // parent dbgprintf("child pid is %d\n", pid); } else { perror("fork"); } } #else /* Non-blocking if single processes */ if (FCNTL_NONBLOCK(listenfd) < 0) perror("fcntl"); #endif select_loop(listenfd); return 0; }
int main(int argc, char *argv[]) { signal(SIGUSR1, handle_signal); global_init(); int listenfd = open_listenfd(); struct epoll_event event; struct sockaddr_in peeraddr; socklen_t socklen; int sock; int nready, i; char buf[1024]; epollfd = epoll_create1(EPOLL_CLOEXEC); event.data.fd = listenfd; event.events = EPOLLIN | EPOLLET; epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &event); while (g_switch == 0) { nready = epoll_wait(epollfd, events, MAX_EVENTS, -1); if (nready == -1) { if (errno == EINTR) continue; handle_err(1, "epoll_wait"); } for (i = 0; i < nready; ++i) { if (events[i].data.fd == listenfd) { socklen = sizeof(peeraddr); sock = accept4(listenfd, (struct sockaddr *) &peeraddr, &socklen, SOCK_NONBLOCK); handle_err(sock == -1, "accept4"); printf("new client %s:%hu accepted\n", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port)); sprintf(buf, "recv from (%s:%hu)\n", inet_ntoa(peeraddr.sin_addr), ntohs(peeraddr.sin_port)); write_log(buf); event.data.fd = sock; event.events = EPOLLIN | EPOLLET; epoll_ctl(epollfd, EPOLL_CTL_ADD, sock, &event); } else { int *ptr = malloc(sizeof(int)); *ptr = events[i].data.fd; threadpool_add(thp, do_task, (void *)(ptr), 0); } } } global_destroy(); return 0; }
//#include "MapBlock.h" AppServer::AppServer(int port) { User::initializeDatabase(); //MapBlock::connectDatabase(); listen_fd = open_listenfd(port); if(listen_fd < 0) { perror("error in open_listenfd\n"); } accept_watcher.set <AppServer, &AppServer::accept_cb > (this); accept_watcher.set(listen_fd, ev::READ); }
int main(int argc, const char *argv[]) { int fd; int listenfd, connfd, port, clientlen; char buf[N]; int n = 0; struct sockaddr_in clientaddr; if (argc != 3) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(0); } port = atoi(argv[1]); listenfd = open_listenfd(port); Signal(SIGCHLD, sigchld_handler); bzero(buf, N); clientlen = sizeof(clientaddr); while (1) { connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen); printf("Connected one\n"); if (Fork() == 0) { Close(listenfd); /* Child closes its listening socket */ fd = Open(argv[2], O_RDONLY | O_EXCL, 0664); /* Open file for transferring */ while ((n = read(fd, buf, N)) > 0) { //write(connfd, buf, n); printf("%s\n", buf); write(connfd, buf, n); } //printf("Done!\n"); //printf("Waiting for the next one\n"); Close(connfd); exit(0); } Close(connfd); /* Parent closes connected socket */ // important } exit(0); }
int main(int argc, char *argv[]) { int port; int listenfd, connfd; struct sockaddr_in client_addr; socklen_t sockaddr_len; #ifndef DEBUG if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); return 1; } port = atoi(argv[1]); #else port = 8888; #endif signal_w(SIGCHLD, sigchld_handler); listenfd = open_listenfd(port); if (listenfd < 0) { fprintf(stderr, "error: unable to open socket listen descriptor\n"); return 1; } sockaddr_len = sizeof(client_addr); while (1) { memset(&client_addr, 0, sockaddr_len); if ((connfd = accept(listenfd, (struct sockaddr *) &client_addr, &sockaddr_len)) < 0) { if (errno != EINTR && errno != ECONNABORTED) { fprintf(stderr, "error: unable to accept a new connect descriptor\n"); return 1; } } else { printf("connection from %s, port %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); if (fork() == 0) { /* child process */ close(listenfd); handle_request(connfd); close(connfd); exit(0); } else { /* parent process */ close(connfd); } } } return 0; }
int main(void){ int i; struct sockaddr_in clientaddr; int default_port = 4242, nChildren = 15, listenfd, connfd; socklen_t clientlen = sizeof clientaddr; char* pPort = getenv ("PORT"); if (pPort!=NULL) default_port = (u_short) strtol (pPort,(char **)NULL, 10); char* pChildren = getenv ("CHILDREN"); if (pChildren!=NULL) nChildren = (u_short) strtol (pChildren,(char **)NULL, 10); listenfd = open_listenfd(default_port); if (listenfd > 0) { printf("listen on port %d, fd is %d\n", default_port, listenfd); } else { perror("ERROR"); exit(listenfd); } // Ignore SIGPIPE signal, so if browser cancels the request, it // won't kill the whole process. signal(SIGPIPE, SIG_IGN); for(i = 0; i < nChildren; i++) { int pid = fork(); if (pid == 0) { // child selectLoop(listenfd); } else if (pid > 0) { // parent printf("child pid is %d\n", pid); } else { perror("fork"); } } selectLoop(listenfd); return 0; }
int main(int args, char **agrv) { int listenfd, servefd; unsigned int port = PORT, clientlen; char recv; struct sockaddr_in clientaddr; if (args > 1) sscanf(agrv[1], "%d", &port); listenfd = open_listenfd(port); while (1) { clientlen = sizeof(clientaddr); servefd = accept(listenfd, (struct sockaddr *)&clientaddr, &clientlen); while (read(servefd, &recv, 1) != -1) { printf("RECV: %c\n", recv); write(servefd, &recv, 1); } } return 0; }
int main(int argc,char *argv[]) { int listenfd,connfd,port,clientlen; struct sockaddr_in clientaddr; if(argc!=2) { fprintf(stderr,"usage: %s <port>\n",argv[0]); return 0; } port=atoi(argv[1]); listenfd=open_listenfd(port); while(1) { clientlen=sizeof(clientaddr); connfd=accept(listenfd,(SA *)&clientaddr,&clientlen); doit(connfd); close(connfd); } return 0; }
int main(int argc, char **argv) { typedef unsigned int socklen_t; socklen_t clientlen; int listenfd, connfd, port; struct sockaddr_in clientaddr; if (argc != 2) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(1); } port = atoi(argv[1]); listenfd = open_listenfd(port); while (1) { clientlen = sizeof(clientaddr); connfd = accept(listenfd, (struct sockaddr *)&clientaddr, &clientlen); doit(connfd); close(connfd); } }
int main(int argc, char *argv[]) { int listenfd = open_listenfd(PORT); int clientfd; struct sockaddr_in client_addr; socklen_t addrlen = sizeof(client_addr); char buf[BUFSIZE] = {0}; int nread; printf("server: started on port: %d.\n", PORT); while (1) { clientfd = accept(listenfd, (struct sockaddr *)&client_addr, &addrlen); if (clientfd < 0) unix_error("accept"); printf("server: accept a new connection\n"); while ((nread = rio_readlinen(clientfd, buf, BUFSIZE)) > 0) { printf("server: received \"%s\", nread: %d\n", buf, nread); if (rio_writen(clientfd, buf, nread) < 0) unix_error("rio_writen"); } if (nread == -1) unix_error("rio_readlinen"); else if (nread == 0) { /* client socket has been closed */ printf("server: client socket has been closed.\n"); if (close(clientfd) != 0) unix_error("close"); } } return 0; }
// main function: // get the user input for the file directory and port number int main(int argc, char** argv){ struct sockaddr_in clientaddr; int port = 9999, listenfd, connfd, pid; // get the name of the current working directory // user input checking if (argc != 3 || -1 == chdir(argv[1]) || (port = atoi(argv[2])) <= 0) { printf("usage: working_directory(test) port_number(1-9999)\n"); return 1; } listenfd = open_listenfd(port); // ignore SIGPIPE signal, so if browser cancels the request, it // won't kill the whole process. signal(SIGPIPE, SIG_IGN); // init process collect zombie processes signal(SIGCHLD, SIG_IGN); while(1){ // permit an incoming connection attempt on a socket. socklen_t clen = sizeof(clientaddr); connfd = accept(listenfd, (SA *) &clientaddr, &clen); // fork children to handle parallel clients pid = fork(); if (0 == pid) { // handle one HTTP request/response transaction process(connfd, &clientaddr); close(connfd); exit(0); } } return 0; }
int main(int argc, char *argv[]) { int listenfd, connfd; fd_set readset; int maxfd, i, nready, clientlen; struct sockaddr_in client_addr; int port = 9527; clientlen = sizeof(client_addr); listenfd = open_listenfd(port); maxfd = listenfd; FD_ZERO(&readset); while (1) { FD_SET(STDIN_FILENO, &readset); FD_SET(listenfd, &readset); fprintf(stderr, "selecting...\n"); nready = select(maxfd + 1, &readset, NULL, NULL, NULL); if (nready < 0) { /* error */ return -1; } else if (nready == 0) { /* timeout */ fprintf(stderr, "time out\n"); continue; } else { fprintf(stderr, "selected.\n"); if (FD_ISSET(listenfd, &readset)) { connfd = accept(listenfd, (struct sockaddr *)&client_addr, &clientlen); echo(connfd); } if (FD_ISSET(STDIN_FILENO, &readset)) { command(); } } } return 0; }
int main(int argc, char** argv){ int listenfd, connfd, port, clientlen; struct sockaddr_in clientaddr; struct hostent *hp; char* haddrp; if(argc != 2){ fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(0); } port = atoi(argv[1]); if((listenfd = open_listenfd(port)) < 0){ fprintf(stderr, "open_listenfd error\n"); exit(0); } while(1){ clientlen = sizeof(clientaddr); if((connfd = accept(listenfd,(struct sockaddr*)&clientaddr, &clientlen)) < 0){ fprintf(stderr, "accept error\n"); exit(0); } if((hp = gethostbyaddr((const char*)&clientaddr.sin_addr.s_addr, sizeof(clientaddr.sin_addr.s_addr), AF_INET)) == NULL ){ fprintf(stderr, "gethostbyaddr error\n"); exit(0); } haddrp = inet_ntoa(clientaddr.sin_addr); printf("Server connected to %s (%s)\n", hp->h_name, haddrp); echo(connfd); close(connfd); } return 0; }
/* $begin main */ int main(int argc,char **argv) { readcnt=0; struct sockaddr_in clientaddr; int listenfd,*connfd; socklen_t clientlen; char* port; pthread_t tid; /* Semaphores initialized here. * mut_fd to protect connfd * mutex and w are used in cache */ sem_init(&mutex,0,1); sem_init(&mut_fd,0,1); sem_init(&w,0,1); mycache=cache_init(); Signal(SIGPIPE, SIG_IGN); if (argc!=2){ fprintf(stderr,"usage: %s <port>\n",argv[0]); exit(1); } port=argv[1]; listenfd=open_listenfd(port); while(1){ connfd=Malloc(sizeof(int)); clientlen=sizeof(clientaddr); P(&mut_fd); *connfd=Accept(listenfd,(SA *)&clientaddr,&clientlen); Pthread_create(&tid,NULL,thread,connfd); } return 0; }
void* start_listen(void* args){ int listenfd,optval; struct sockaddr_in clientaddr; struct hostent *hp; socklen_t clientlen; int lisfd; pthread_t tid; int args1[2]; int local_port = ((int*)args)[0]; int target_port = ((int*)args)[1]; listenfd = open_listenfd(local_port); optval = 1; setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,(const void*)&optval,sizeof(int)); while(1){ clientlen = sizeof(clientaddr); lisfd = accept(listenfd, (SA*)&clientaddr,&clientlen); hp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,sizeof(clientaddr.sin_addr.s_addr),AF_INET); args1[0] = lisfd; args1[1] = target_port; Pthread_create(&tid,NULL,nodeTalk,(void*)args1); Pthread_detach(tid); } }
int main(int argc, char* argv[]) { int rc; char *conf_file = CONF; /* * read confile file */ char conf_buf[BUFLEN]; zxc_conf_t cf; rc = read_conf(conf_file, &cf, conf_buf, BUFLEN); /* * initialize listening socket */ int listenfd; struct sockaddr_in clientaddr; socklen_t inlen = 1; memset(&clientaddr, 0, sizeof(struct sockaddr_in)); listenfd = open_listenfd(cf.port); rc = make_socket_non_blocking(listenfd); /* * create epoll and add listenfd to ep */ int epfd = zxc_epoll_create(0); struct epoll_event event; zxc_http_request_t *request = (zxc_http_request_t *)malloc(sizeof(zxc_http_request_t)); zxc_init_request_t(request, listenfd, &cf); event.data.ptr = (void *)request; event.events = EPOLLIN | EPOLLET; zxc_epoll_add(epfd, listenfd, &event); /* create thread pool */ zxc_threadpool_t *tp = threadpool_init(cf.thread_num); /* epoll_wait loop */ while (1) { int n; n = zxc_epoll_wait(epfd, events, MAXEVENTS, -1); int i, fd; for (i=0; i<n; i++) { zxc_http_request_t *r = (zxc_http_request_t *)events[i].data.ptr; fd = r->fd; if (listenfd == fd) { /* we hava one or more incoming connections */ while(1) { int infd = accept(listenfd, (struct sockaddr *)&clientaddr, &inlen); if (infd == -1) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { /* we have processed all incoming connections */ break; } else { break; } } rc = make_socket_non_blocking(infd); zxc_http_request_t *request = (zxc_http_request_t *)malloc(sizeof(zxc_http_request_t)); if (request == NULL) { break; } zxc_init_request_t(request, infd, &cf); event.data.ptr = (void *)request; event.events = EPOLLIN | EPOLLET; zxc_epoll_add(epfd, infd, &event); } // end of while of accept } else { if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) { close(fd); continue; } /* do_request(infd); close(infd); */ rc = threadpool_add(tp, do_request, events[i].data.ptr); } } //end of for } // end of while(1) threadpool_destroy(tp, 1); return 0; }
int main(int argc, char **argv) { struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = SIG_IGN; sa.sa_flags = 0; if(sigaction(SIGPIPE, &sa, NULL)) { LOG_ERR("SIGPIPE ERR"); return 0; } int listenfd; listenfd = open_listenfd(8888); make_socket_non_blocking(listenfd); int epfd = tiny_epoll_create(0); struct epoll_event event; event.data.fd = listenfd; event.events = EPOLLIN | EPOLLET; tiny_epoll_add(epfd, listenfd, &event); tiny_threadpool_t *tp = threadpool_init(2); while(1) { int n = tiny_epoll_wait(epfd, events, MAXEVENTS, -1); int i = 0; for(;i < n;++i){ if (listenfd == events[i].data.fd) { while(1) { struct sockaddr_in clientaddr; socklen_t inlen = sizeof(clientaddr); int infd = accept(listenfd, (struct sockaddr*)&clientaddr,&inlen); if (infd == -1) { if((errno == EAGAIN) || (errno == EWOULDBLOCK)) break; else{ LOG_ERR("accept err"); break; } } LOG_INFO("new client fd : %d",infd); make_socket_non_blocking(infd); event.data.fd = infd; event.events = EPOLLIN | EPOLLET; tiny_epoll_add(epfd, infd, &event); } } else { if((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || !(events[i].events & EPOLLIN)) { LOG_ERR("epoll error fd : %d",events[i].data.fd); close(events[i].data.fd); continue; } LOG_INFO("new task from fd : %d", events[i].data.fd); threadpool_add(tp,accept_request,&(events[i].data.fd)); } } } LOG_INFO("begin destroy"); if(threadpool_destroy(tp,1) < 0) LOG_ERR("idestroy err"); return 0; }
sock(int port){assert_perror((listen_socket = open_listenfd(port)) < 0);instance = this;}
int main(int argc, char* argv[]) { int rc; int opt = 0; int options_index = 0; char *conf_file = CONF; /* * parse argv * more detail visit: http://www.gnu.org/software/libc/manual/html_node/Getopt.html */ if (argc == 1) { usage(); return 0; } while ((opt=getopt_long(argc, argv,"Vc:?h",long_options,&options_index)) != EOF) { switch (opt) { case 0 : break; case 'c': conf_file = optarg; break; case 'V': printf(PROGRAM_VERSION"\n"); return 0; case ':': case 'h': case '?': usage(); return 0; } } debug("conffile = %s", conf_file); if (optind < argc) { log_err("non-option ARGV-elements: "); while (optind < argc) log_err("%s ", argv[optind++]); return 0; } /* * read confile file */ char conf_buf[BUFLEN]; zv_conf_t cf; rc = read_conf(conf_file, &cf, conf_buf, BUFLEN); check(rc == ZV_CONF_OK, "read conf err"); /* * install signal handle for SIGPIPE * when a fd is closed by remote, writing to this fd will cause system send * SIGPIPE to this process, which exit the program */ struct sigaction sa; memset(&sa, '\0', sizeof(sa)); sa.sa_handler = SIG_IGN; sa.sa_flags = 0; if (sigaction(SIGPIPE,&sa,NULL)) { log_err("install sigal handler for SIGPIPI failed"); return 0; } /* * initialize listening socket */ int listenfd; struct sockaddr_in clientaddr; // initialize clientaddr and inlen to solve "accept Invalid argument" bug socklen_t inlen = 1; memset(&clientaddr, 0, sizeof(struct sockaddr_in)); listenfd = open_listenfd(cf.port); rc = make_socket_non_blocking(listenfd); check(rc == 0, "make_socket_non_blocking"); /* * create epoll and add listenfd to ep */ int epfd = zv_epoll_create(0); struct epoll_event event; zv_http_request_t *request = (zv_http_request_t *)malloc(sizeof(zv_http_request_t)); zv_init_request_t(request, listenfd, &cf); event.data.ptr = (void *)request; event.events = EPOLLIN | EPOLLET; zv_epoll_add(epfd, listenfd, &event); /* create thread pool */ zv_threadpool_t *tp = threadpool_init(cf.thread_num); /* epoll_wait loop */ while (1) { int n; n = zv_epoll_wait(epfd, events, MAXEVENTS, -1); int i, fd; for (i=0; i<n; i++) { zv_http_request_t *r = (zv_http_request_t *)events[i].data.ptr; fd = r->fd; if (listenfd == fd) { /* we hava one or more incoming connections */ while(1) { debug("## ready to accept"); int infd = accept(listenfd, (struct sockaddr *)&clientaddr, &inlen); if (infd == -1) { if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) { /* we have processed all incoming connections */ break; } else { log_err("accept"); break; } } rc = make_socket_non_blocking(infd); check(rc == 0, "make_socket_non_blocking"); debug("new connection fd %d", infd); zv_http_request_t *request = (zv_http_request_t *)malloc(sizeof(zv_http_request_t)); if (request == NULL) { log_err("malloc(sizeof(zv_http_request_t))"); break; } zv_init_request_t(request, infd, &cf); event.data.ptr = (void *)request; event.events = EPOLLIN | EPOLLET; zv_epoll_add(epfd, infd, &event); } // end of while of accept debug("## end accept"); } else { if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) { log_err("epoll error fd: %d", r->fd); close(fd); continue; } /* do_request(infd); close(infd); */ log_info("new task from fd %d", fd); rc = threadpool_add(tp, do_request, events[i].data.ptr); check(rc == 0, "threadpool_add"); } } //end of for } // end of while(1) return 0; }
int main(int argc, char **argv){ char *c; unsigned short port; thread_param tp; pthread_t eth_tid, tap_tid; char *remote_host; int remote_port; char *local_interface; if(argc != 4 || argc != 3){ printf("Error\n\t Usage for first proxy:\n\t\t cs352proxy <port> <local interface> \n\t Usage for second proxy: \n\t\t cs352proxy <remote host> <remote port> <local interface>\n"); return -1; } switch(argc){ // 1st proxy case 3: for(c=argv[1]; isdigit(*c)||*c=='\0'; c++); if(*c=='\0'){ tp.ethfd=atoi(argv[1]); if(tp.ethfd<1024||tp.ethfd>65535){ perror("ERROR: port must be from " "1024-65535.\n"); exit(1); } port=(unsigned short)tp.ethfd; } else{ perror("ERROR: port parameter " "not a decimal number.\n"); exit(1); } if((tp.ethfd=open_listenfd(port))<0){ perror("error opening ethernet device\n"); exit(1); } if((tp.tapfd=allocate_tunnel(argv[2], IFF_TAP|IFF_NO_PI))<0){ perror("error opening tap device\n"); exit(1); } /** * 1st thread listens to TCP socket * 2nd thread listens to tap device */ Pthread_create(ð_tid, NULL, eth_thread, &tp); Pthread_create(&tap_tid, NULL, tap_thread, &tp); Pthread_join(eth_tid, NULL); Pthread_join(tap_tid, NULL); close(tp.ethfd); close(tp.tapfd); break; // 2nd proxy case 4: for(c=argv[2]; isdigit(*c)||*c=='\0'; c++); if(*c=='\0'){ tp.ethfd=atoi(argv[2]); if(tp.ethfd<1024||tp.ethfd>65535){ perror("ERROR: port must be from " "1024-65535.\n"); exit(1); } port=(unsigned short)tp.ethfd; } else{ perror("ERROR: port parameter " "not a decimal number.\n"); exit(1); } if((tp.ethfd=open_clientfd(argv[1], port))<0){ perror("error opening ethernet device\n"); exit(1); } if((tp.tapfd=allocate_tunnel(argv[3], IFF_TAP|IFF_NO_PI))<0){ perror("error opening tap device\n"); exit(1); } /** * 1st thread listens to TCP socket * 2nd thread listens to tap device */ Pthread_create(ð_tid, NULL, eth_thread, &tp); Pthread_create(&tap_tid, NULL, tap_thread, &tp); Pthread_join(eth_tid, NULL); Pthread_join(tap_tid, NULL); close(tp.ethfd); close(tp.tapfd); break; default: perror("ERROR: invalid parameters.\n"); exit(1); } return 0; }
int main(int argc, char **argv){ int fd_setsize = 2; fd_set read_set,ready_set; //IO多路复用的描述符集合 int clientfd[fd_setsize]; int maxfd; //最大描述符 //////初始化 int listenfd = open_listenfd(); FD_ZERO(&read_set); //创建一个空的fd_set集合 FD_SET(STDIN_FILENO,&read_set); //标准输入放入监控集合 FD_SET(listenfd,&read_set); //套接字描述符放入监控集合 maxfd = listenfd; int n; for(n=0; n<fd_setsize; n++){ clientfd[n] = -1; } ///////轮询select while(1){ //这种多路复用的方式,可以让一个进程处理多个IO,和java中的nio一样 ready_set = read_set; //select返回后这个集合里面就是可以读的描述符 select(maxfd+1,&ready_set,NULL,NULL,NULL); //select每次都会修改第二个参数的执指向 //////////////////////接收客户端 add_client if(FD_ISSET(listenfd,&ready_set)){ struct sockaddr_in clientaddr; //客户端套接字地址 int addrlen; //套接字地址的字节个数 int connfd = accept(listenfd,(struct sockaddr *)&clientaddr,&addrlen); //将已经建立连接的套接字描述符放入集合 int i; for(i = 0; i<fd_setsize; i++){ if(clientfd[i] < 0){ //如果池中有空位 clientfd[i]=connfd; FD_SET(connfd,&read_set); //更新最大描述符 if(connfd > maxfd){ maxfd = connfd; } break; } } //如果池中没有空位 if(i == fd_setsize){ printf("接收的客户端太多了,丢弃该连接:%d!\n",connfd); close(connfd); } } /////////////////处理客户端的可读信息 int j; for(j=0; j<fd_setsize; j++){ int connfd = clientfd[j]; if((connfd>0) && FD_ISSET(connfd,&ready_set)){ //集合中该描述符是否准备好 char c[1024]; int count = read(connfd,c,1023); if(count == 0){ //EOF 文件结尾 close(connfd); //关闭描述符 FD_CLR(connfd,&read_set); //清除读集合 clientfd[j] = -1; //删除在池中站得位置 printf("clear connfd: %d\n",connfd); }else if(count == -1){ printf("read connfd: %d error!",connfd); }else{ c[count] = '\0'; printf("server received connfd[%d] data: %s\n",connfd,c); } } } } }
int main(int argc, char *argv[]) { printf("%s", "start\n"); int listen_fd; int rcode; struct epoll_event *events; if (argc != 2) { fprintf(stderr, "usage: %s [port]\n", argv[0]); exit(EXIT_FAILURE); } struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = SIG_IGN; sa.sa_flags = 0; if (sigaction(SIGPIPE, &sa, NULL)) { printf("ignore SIGPIPE\n"); } struct sockaddr_in client_addr; socklen_t client_len = 1; memset(&client_addr, 0, sizeof(struct sockaddr_in)); /* create and bind the port, and then set the socket to non blocking mode */ listen_fd = open_listenfd(atoi(argv[1])); debug("listen fd = %d", listen_fd); rcode = make_socket_non_blocking(listen_fd); if (rcode == -1) { log_err("error when making socket non blocking"); abort(); } /* create epoll event */ int efd = epoll_create1(0); if (efd == -1) { log_err("epoll_create"); abort(); } struct epoll_event event; events = (struct epoll_event *)malloc(sizeof(struct epoll_event) * MAXEVENTS); http_request_t *request = (http_request_t *)malloc(sizeof(http_request_t)); http_request_init(request, listen_fd); event.data.ptr = (void *)request; event.events = EPOLLIN | EPOLLET; /* register the listen event */ rcode = epoll_ctl(efd, EPOLL_CTL_ADD, listen_fd, &event); if (rcode == -1) { perror("epoll_ctl"); abort(); } threadpool_t *tp = threadpool_init(NUM_OF_THREADS); /* event loop */ while (1) { int n = epoll_wait(efd, events, MAXEVENTS, -1); /* process each incoming IO event */ int i; for (i = 0; i < n; i++) { http_request_t *r = (http_request_t *)events[i].data.ptr; int fd = r->fd; debug("event fd = %d", fd); if (fd == listen_fd) { /* incoming connection event */ while (1) { int client_fd; debug("waiting for accept"); client_fd = accept(listen_fd, (struct sockaddr *)&client_addr, &client_len); if (client_fd == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) { // we have already processed the incoming connection debug("incoming connection processed\n"); break; } else { log_err("error occured when accepting connection\n"); break; } } rcode = make_socket_non_blocking(client_fd); if (rcode == -1) { if (errno == EAGAIN || errno == EWOULDBLOCK) // we have already processed the incoming connection break; log_err("fail to accept the connection\n"); break; } debug("new connection fd %d", client_fd); http_request_t *request = (http_request_t *)malloc(sizeof(http_request_t)); http_request_init(request, client_fd); event.data.ptr = (void *)request; event.events = EPOLLIN | EPOLLET; /* add the new event into epoll */ rcode = epoll_ctl(efd, EPOLL_CTL_ADD, client_fd, &event); if (rcode == - 1) { log_err("fail in epoll_ctl in epoll_wait"); abort(); } } debug("end accept"); } else if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) { /* an error has occured on this fd, or the socket is not ready for reading */ log_err("error events: %d", events[i].events); if (events[i].events & EPOLLERR) log_err("EPOLLERR"); if (events[i].events & EPOLLHUP) log_err("EPOLLHUP"); if (!(events[i].events & EPOLLIN)) log_err("EPOLLIN"); close(fd); continue; } else { /* incoming data read event */ /* add the event to the thread pool list */ threadpool_add(tp, handle_http, events[i].data.ptr); debug("thread count: %d", tp->thread_count); debug("thread queue size: %d", tp->queue_size); } } } threadpool_destroy(tp); return 0; }