int main(int argc, char **argv) { struct sockaddr_in peer; char *hostname; char *servicename; int peerlen; socket_t client_socket; socket_t server_socket; const int on = 1; if (argc == 2) { hostname = NULL; servicename = argv[1]; } else if (argc == 3) { hostname = argv[1]; servicename = argv[2]; } else { error(1, errno, "count of parametrs mismatch\n"); } server_socket = tcp_server(hostname, servicename); do { peerlen = sizeof(peer); client_socket = accept(server_socket, (struct sockaddr *) &peer, &peerlen); if (client_socket < 0) error(1, errno, "error function accept\n"); server(client_socket, &peer); close_socket(client_socket); } while (1); close_socket(server_socket); exit(0); }
int do_srv_epoll(unsigned short port) { int epfd = epoll_create(1); printf("epfd:%d\n",epfd); if(epfd==-1) { perror("kqueue"); return -1; } int fd = tcp_server(port); if(fd==-1) { perror("tcp_server"); return 1; } if(reg_epoll_r(epfd,fd,11)==-1) { perror("kevent"); return -1; } printf("Server Startupt at : %d\n",port); for(;;) { if(wait_event(epfd,fd)==-1) return -1; } return 0; }
int main(int argc, const char *argv[]) { if(signal(SIGCHLD, SIG_IGN) == SIG_ERR) ERR_EXIT("signal child"); int listenfd = tcp_server("192.168.153.131", 8976); while(1) { int peerfd = accept(listenfd, NULL, NULL); if(peerfd == -1) ERR_EXIT("accept"); printf("%s connected\n", get_tcp_info(peerfd)); // 直接传值可能会产生问题,因此我们从堆上开辟空间 int *pfd = (int *)malloc(sizeof(int)); *pfd = peerfd; pthread_t tid; if(pthread_create(&tid, NULL, thread_func, pfd)) { free(pfd); //线程启动失败,避免内存泄露 } } close(listenfd); return 0; }
void server_main(int ac, char **av) { int newsock, sock; if (ac != 2) { fprintf(stderr, "usage: %s -s\n", av[0]); exit(1); } GO_AWAY; signal(SIGCHLD, child); sock = tcp_server(TCP_XACT, SOCKOPT_NONE); for (;;) { newsock = tcp_accept(sock, SOCKOPT_NONE); switch (fork()) { case -1: perror("fork"); break; case 0: doserver(newsock); exit(0); default: close(newsock); break; } } /* NOTREACHED */ }
void server_main() { int data, newdata; GO_AWAY; data = tcp_server(TCP_DATA, SOCKOPT_WRITE|SOCKOPT_REUSE); if (data < 0) { perror("server socket creation"); exit(1); } signal(SIGCHLD, sigchld_wait_handler); for ( ;; ) { newdata = tcp_accept(data, SOCKOPT_WRITE); switch (fork()) { case -1: perror("fork"); break; case 0: source(newdata); exit(0); default: close(newdata); break; } } }
int link_wait(link_t * ld, int service) { int rc; int sdnew; if (ld->fStdio) return 0; rc = tcp_init(SF_ZOMBIES); if (rc < 0) return (-1); rc = tcp_socket(0); if (rc < 0) return (-1); ld->fd = rc; rc = tcp_server(ld->fd, service, 8, NULL); if (rc < 0) return (-1); while(1) { do { sdnew = tcp_accept(ld->fd); } while(sdnew == (-1) && errno == EINTR); if (sdnew < 0) break; rc = tcp_forkclient(ld->fd, sdnew); if (rc == 0) // child { ld->fd = sdnew; break; } } return 0; }
/* Main */ int main(int argc, char **argv) { /* Setup main */ setbuf(stdout, NULL); signal(SIGINT, on_sigint); parse(argc, argv); if ((epoll = epoll_create(1)) < 0) error("Error creating epoll"); /* Setup initial peers */ bc_client(&bc, BC_PORT); mc_client(&mc, MC_GROUP, MC_PORT); tcp_server(&tcp, TCP_HOST, TCP_PORT); web_server(&web, WEB_HOST, WEB_PORT); /* Run main loop */ while (1) { errno = 0; int count = epoll_wait(epoll, events, MAX_EVENTS, MAX_WAIT); if (errno == EINTR) continue; if (count < 0) error("Error waiting for event"); for (int i = 0; i < count; i++) { poll_t *poll = events[i].data.ptr; poll->ready(poll->data); } } return 0; }
int main(int argc,char *argv[]) { if(argc != 5) { printf("Incorrect format\n"); printf("\n Usage: %s <listenportno> <ip of server> <connectportno> <protocol>\n",argv[0]); return 1; } char *listenportno = argv[1]; char *connectportno = argv[3]; char *ip = argv[2]; if(strcmp(argv[3],"udp")==0) isudpset = 1; else isudpset = 0; pid_t pid; pid = fork(); if(pid == 0) { if(isudpset) udp_server(listenportno); else tcp_server(listenportno); } else if(pid > 0) { if(isudpset) udp_client(ip,connectportno); else tcp_client(ip,connectportno); } return 0; }
int main(int argc, char *argv[]) { int listenfd, clientfd; char ch; int port_flag = 0; char *port = NULL; while ((ch = getopt(argc, argv, "p:")) != -1) { switch (ch) { case 'p': port = optarg; printf("port is %s\n", port); port_flag = 1; break; case '?': fprintf(stderr, "Unknown command opt: %c\n", (char)optopt); break; } } if (!port_flag) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(EXIT_FAILURE); } // start listen signal(SIGCHLD, SIG_IGN); pid_t pid; listenfd = tcp_server(NULL, atoi(port)); while (1) { if ((clientfd = accept(listenfd, NULL, NULL)) < 0) { bug("listenfd %d clientfd %d\n", listenfd, clientfd); err_sys("accept"); } pid = fork(); if (pid < 0) err_sys("fork"); if (pid == 0) // in child proces { close(listenfd); doit(clientfd); exit(EXIT_SUCCESS); } else if (pid > 0) // in parent process { close(clientfd); } } exit(EXIT_SUCCESS); }
int main(int argc, char **argv) { if (argc < 3 || sscanf(argv[1], "%d", &argc) != 1 || access(argv[2], F_OK) == -1) { printf("Usage: %s [port] [sock name]\n", *argv); return 0; } tcp_server(argv[1], 10, handler, argv[2], strlen(argv[2]) + 1); return 0; }
int main(int ac, char **av) { int i, prog; #ifdef sgi int ncpus = sysmp(MP_NPROCS); #endif for (i = 1; i < ac; ++i) { if (av[i][0] != '-') { break; } switch (av[i][1]) { case 'D': Dflg = 1; break; /* Allow directories */ case 'd': dflg = 1; break; /* debugging */ case 'f': fflg = atoi(&av[i][2]); break; /* # of threads */ case 'l': lflg = 1; break; /* logging */ case 'n': nflg = 1; break; /* fake file i/o */ case 'z': zflg = 1; break; /* all files are 0 size */ default: fprintf(stderr, "Barf.\n"); exit(1); } } if (getenv("DOCROOT")) { if (chdir(getenv("DOCROOT")) == -1) { perror(getenv("DOCROOT")); exit(1); } } if (atoi(av[ac - 1]) != 0) { prog = -atoi(av[ac - 1]); } else { prog = -80; } /* * Steve - why is this here? */ signal(SIGPIPE, SIG_IGN); data = tcp_server(prog, SOCKOPT_REUSE); bufs[0] = valloc(XFERSIZE); bufs[1] = valloc(XFERSIZE); bufs[2] = valloc(XFERSIZE); logfile = open(LOGFILE, O_CREAT|O_APPEND|O_WRONLY, 0666); signal(SIGINT, die); signal(SIGHUP, die); signal(SIGTERM, die); for (i = 1; i < fflg; ++i) { if (fork() <= 0) { break; } } handle_scheduler(i, 0, 0); worker(); return(0); }
int main(int argc,char **argv) { struct sockaddr_in peer; SOCKET s; SOCKET s1; int peerlen = sizeof(peer); int n; char buf[10]; INIT(); if(argc == 2) { s = tcp_server(NULL,argv[1]); } else { s = tcp_server(argv[1],argv[2]); } s1 = accept(s,(struct sockaddr*)&peer,(socklen_t *)&peerlen); if(!isvalidsock(s1)) { error(1,errno,"accept failed"); } for(;;) { n = readvrec(s1,buf,sizeof(buf)); if(n < 0) { error(0,errno,"readvrec returned error"); } else if(n == 0) { error(1,0,"client disconnected\n"); } else { write(1,buf,n); } } EXIT(0); }
int main(int argc, char *argv[]) { pid_t child_pid, parent_pid; struct sockaddr_in sin; int listen_fd; u_short port; socklen_t len; listen_fd = socket(PF_INET, SOCK_STREAM, 0); if (listen_fd < 0) err(-1, "socket"); /* * We use the loopback, but let the kernel select a port for the * server socket. */ bzero(&sin, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_len = sizeof(sin); sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); if (bind(listen_fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) err(-1, "bind"); if (listen(listen_fd, -1) < 0) err(-1, "listen"); /* * Query the port so that the client can use it. */ bzero(&sin, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_len = sizeof(sin); len = sizeof(sin); if (getsockname(listen_fd, (struct sockaddr *)&sin, &len) < 0) err(-1, "getsockname"); port = sin.sin_port; printf("Using port %d\n", ntohs(port)); if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) err(-1, "signal"); parent_pid = getpid(); child_pid = fork(); if (child_pid < 0) err(-1, "fork"); if (child_pid == 0) { child_pid = getpid(); tcp_server(parent_pid, listen_fd); } else tcp_client(child_pid, port); return (0); }
void server_main() { int newsock, sock; GO_AWAY; signal(SIGCHLD, sigchld_wait_handler); sock = tcp_server(TCP_XACT, SOCKOPT_REUSE); for (;;) { newsock = tcp_accept(sock, SOCKOPT_NONE); #ifdef CONFIG_NOMMU switch (vfork()) { #else switch (fork()) { #endif case -1: perror("fork"); break; case 0: doserver(newsock); #ifdef CONFIG_NOMMU _exit(0); #else exit(0); #endif default: close(newsock); break; } } /* NOTREACHED */ } void doserver(int sock) { int n; if (read(sock, &n, sizeof(int)) == sizeof(int)) { int msize = ntohl(n); char* buf = (char*)malloc(msize); for (n = 0; read(sock, buf, msize) > 0; n++) { write(sock, buf, msize); } free(buf); } else { /* * A connection with no data means shut down. */ tcp_done(TCP_XACT); kill(getppid(), SIGTERM); exit(0); } }
int main(int argc, char const *argv[]) { check_permission(); setup_signal_chld(); parseconf_load_file("ftpserver.conf"); print_conf(); init_hash(); //创建一个监听fd int listenfd = tcp_server(tunable_listen_address, tunable_listen_port); pid_t pid; session_t sess; session_init(&sess); p_sess = &sess; //配置全局变量 while(1) { struct sockaddr_in addr; int peerfd = accept_timeout(listenfd, &addr, tunable_accept_timeout); if(peerfd == -1 && errno == ETIMEDOUT) continue; else if(peerfd == -1) ERR_EXIT("accept_timeout"); //获取ip地址,并在hash中添加一条记录 uint32_t ip = addr.sin_addr.s_addr; sess.ip = ip; add_clients_to_hash(&sess, ip); if((pid = fork()) == -1) ERR_EXIT("fork"); else if(pid == 0) { close(listenfd); sess.peerfd = peerfd; limit_num_clients(&sess); session_begin(&sess); exit(EXIT_SUCCESS); } else { //pid_to_ip add_pid_ip_to_hash(pid, ip); close(peerfd); } } return 0; }
int main(int argc, char *argv[]) { if (getuid()) { fprintf(stderr, "the ftpserver must started by root\n"); exit(EXIT_FAILURE); } if (signal(SIGCHLD, handle_sigchld) == SIG_ERR) ERR_EXIT("signal"); parse_load_file("ftpserver.conf"); printf("parse_load_file success\n"); // printconfig(); ip_to_clients = hash_alloc(256, hash_func); pid_to_ip = hash_alloc(256, hash_func); int listenfd; listenfd = tcp_server(listen_address, listen_port); printf("tcp_server success, listenfd = %d\n", listenfd); int connfd; pid_t pid; session_t sess; while (1) { struct sockaddr_in addr; connfd = accept_time_out(listenfd, &addr, accept_timeout); if (connfd == -1) { // printf("don't has connection in %d seconds\n", accept_timeout); continue; } uint32_t ip = addr.sin_addr.s_addr; printf("connect success\n"); session_init(&sess); num_of_clients++; sess.curr_clients = num_of_clients; sess.curr_ip_clients = add_ip_to_hash(ip_to_clients, ip); sess.ip = ip; p_sess = &sess; pid = fork(); if (pid == -1) { ERR_EXIT("fork error"); } else if (pid == 0) { //子进程 close(listenfd); sess.peerfd = connfd; session_begin(&sess); //建立一个会话 exit(EXIT_SUCCESS); } else { //父进程 close(connfd); hash_add_entry(pid_to_ip, &pid, sizeof(pid), &ip, sizeof(ip)); } } return 0; }
int main(int argc, char *argv[]) { if (argc == 1) { tcp_server(); } tcp_client(argv[1]); return 0; }
int main(int argc, char *argv[]) { threadpool_t *pool = threadpool_init(20, 40); // 20个线程,40工作 int listenfd, clientfd; char ch; int port_flag = 0; char *port = NULL; while ((ch = getopt(argc, argv, "p:")) != -1) { switch (ch) { case 'p': port = optarg; printf("port is %s\n", port); port_flag = 1; break; case '?': fprintf(stderr, "Unknown command opt: %c\n", (char)optopt); break; } } if (!port_flag) { fprintf(stderr, "usage: %s <port>\n", argv[0]); exit(EXIT_FAILURE); } /* 设置信号 */ signal(SIGPIPE, SIG_IGN); listenfd = tcp_server(NULL, atoi(port)); while (1) { if ((clientfd = accept(listenfd, NULL, NULL)) < 0) { err_sys("accept"); } /* 用线程池来处理连接 */ int *tmp_fd = malloc(sizeof(int)); *tmp_fd = clientfd; threadpool_add_job(pool, doit, (void *)tmp_fd); } // threadpool_destory(pool); // 这里应该接收ctrl c 信号,然后调用threadpool_destory函数 exit(EXIT_SUCCESS); }
void server(void* cookie) { int pid, rc; state_t* state = (state_t*)cookie; pid = getpid(); state->pid = 0; if (state->fid_f == open_file) { /* Create a temporary file for clients to open */ sprintf(state->fname, "lat_selectXXXXXX"); state->fid = mkstemp(state->fname); if (state->fid <= 0) { char buf[L_tmpnam+128]; sprintf(buf, "lat_select: Could not create temp file %s", state->fname); perror(buf); exit(1); } close(state->fid); return; } /* Create a socket for clients to connect to */ state->sock = tcp_server(TCP_SELECT, SOCKOPT_REUSE); if (state->sock <= 0) { perror("lat_select: Could not open tcp server socket"); exit(1); } /* Start a server process to accept client connections */ switch(state->pid = fork()) { case 0: /* child server process */ while (pid == getppid()) { int newsock = tcp_accept(state->sock, SOCKOPT_NONE); rc = read(newsock, &state->fid, 1); if (rc < 0) DIE_PERROR("read failed"); close(newsock); } exit(0); case -1: /* error */ perror("lat_select::server(): fork() failed"); exit(1); default: break; } }
int main() { pid_t pid; pid = fork(); if (pid == 0) { tcp_server(); } else { udp_server(); } return 0; }
static void privop_pasv_listen(session_t *sess) { sess->pasv_listen_fd = tcp_server(tunable_listen_address, 0); struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); if (getsockname(sess->pasv_listen_fd, (struct sockaddr *)&addr, &addrlen) < 0) { priv_sock_send_result(sess->parent_fd, PRIV_SOCK_RESULT_BAD); ERR_EXIT("getsockname"); } priv_sock_send_result(sess->parent_fd, PRIV_SOCK_RESULT_OK); priv_sock_send_int(sess->parent_fd, (int)addr.sin_port); }
int main(int argc, char **argv) { struct tm tm; // pid = getpid(); init_abt_log(NULL, LOG_NOTICE); if (sqlite3_config(SQLITE_CONFIG_MULTITHREAD) != SQLITE_OK) { //多线程模式防止并发锁 log_error(LOG_ERROR, "mutile thread error"); } if (init_ds3231() < 0) { log_error(LOG_EMERG, "init_ds3231"); return -1; } getTime(&tm); if (cache_init() < 0) { log_error(LOG_EMERG, "cache_init"); return -1; } if (init_at24c02b() < 0) { log_error(LOG_NOTICE, "init_at24c02b"); } g_dev_version = zigdev_version(); if (zigdev_init() < 0) { log_error(LOG_NOTICE, "zigdev_init"); return -1; } signal_propose(); zigdev_test(); //zigadd_dev(0x256833, 1, 1, 0, 1); tcp_server(NULL); cache_destroy(); close_abt_log(); return 0; }
static void privop_pasv_listen(session_t *sess) { char ip[20] = {0}; getlocalip(ip); // 服务器 bind 的必须是随机端口,因此此时可能有多个nobody 进程在listen, 这样才不会冲突 sess->pasv_listen_fd = tcp_server(ip, 0); struct sockaddr_in localaddr; socklen_t addrlen = sizeof(localaddr); if (getsockname(sess->pasv_listen_fd, (struct sockaddr *)&localaddr, &addrlen) < 0) ERR_EXIT("getsockname"); unsigned short port = ntohs(localaddr.sin_port); priv_sock_send_int(sess->nobody_fd, (int)port); }
int main(int argc, const char *argv[]) { int listenfd = tcp_server("192.168.1.26", 8976); int peerfd = accept(listenfd, NULL, NULL); printf("%s connected\n", get_tcp_info(peerfd)); do_service(peerfd); close(peerfd); close(listenfd); return 0; }
int main(int argc, char *argv[]) { pid_t child_pid, parent_pid; if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) err(-1, "signal"); parent_pid = getpid(); child_pid = fork(); if (child_pid < 0) err(-1, "fork"); if (child_pid == 0) { child_pid = getpid(); tcp_server(parent_pid); return (0); } else tcp_client(child_pid, 0); (void)kill(child_pid, SIGTERM); sleep(5); parent_pid = getpid(); child_pid = fork(); if (child_pid < 0) err(-1, "fork"); if (child_pid == 0) { child_pid = getpid(); tcp_server(parent_pid); return (0); } else tcp_client(child_pid, 1); (void)kill(child_pid, SIGTERM); return (0); }
static void privop_pasv_listen(session_t *sess) { char ip[16]; getlocalip(ip); sess->pasv_listen_fd = tcp_server(ip, 0); struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); if(getsockname(sess->pasv_listen_fd, (struct sockaddr *)&addr, &addrlen) < 0) { ERR_EXIT("getsockname"); } unsigned short port = ntohs(addr.sin_port); priv_sock_send_int(sess->parent_fd, (int)port); }
/************************************************************************* * \brief main *************************************************************************/ int main(int argc, char* argv[]) { pid_t pid; pid = fork(); if (pid == 0) { tcp_server(); } else { udp_server(); } return 0; }
void server_main() { int newsock, sock; char c ='1'; GO_AWAY; sock = tcp_server(TCP_CONNECT, SOCKOPT_NONE|SOCKOPT_REUSE); for (;;) { newsock = tcp_accept(sock, SOCKOPT_NONE); if (read(newsock, &c, 1) > 0) { tcp_done(TCP_CONNECT); exit(0); } close(newsock); } /* NOTREACHED */ }
int main( int argc, char **argv ) { char *bp; SOCKET s; SOCKET s1; INIT(); init_smb( TRUE ); s = tcp_server( NULL, argv[ 1 ] ); s1 = accept( s, NULL, NULL ); if ( !isvalidsock( s1 ) ) error( 1, errno, "accept failure" ); for ( ;; ) { bp = smbrecv( s1 ); fputs( bp, stdout ); smbfree( bp ); } EXIT( 0 ); }
int main( int argc, char **argv ) { SOCKET s; SOCKET s1; int rc; int i = 1; int pkt[ 3 ]; INIT(); s = tcp_server( NULL, argv[ 1 ] ); s1 = accept( s, NULL, NULL ); if ( !isvalidsock( s1 ) ) error( 1, errno, "accept failure" ); for ( ;; ) { rc = recv( s1, ( char * )pkt, sizeof( pkt ), 0 ); if ( rc != TWOINTS && rc != THREEINTS ) error( 1, 0, "recv returned %d\n", rc ); printf( "Packet %d has %d values in %d bytes\n", i++, ntohl( pkt[ 0 ] ), rc ); } }