int main(int argc, char *argv[]) { int server_sock = 0; int client1_sock = 0; int client2_sock = 0; if(argc == 2 && strcmp("-d", argv[1]) == 0) { printf("Running in debug mode.\n"); debug = 1; } signal(SIGCHLD, reap_terminated_child); //set up the server dprintf("Initializing server.\n"); server_sock = get_server_socket(HOST, HTTPPORT); if(start_server(server_sock, BACKLOG) == -1) { printf("Error starting server: %s.\n", strerror(errno)); exit(1); } while(1) { //client 1 has already connected to the server if(client1_sock != 0) { client2_sock = accept_client(server_sock); dprintf("Received connection from second client.\n"); //fork for subserver if (!fork()) { // child process, so start the subserver close(server_sock); //no longer needed in child process dprintf("Preparing to play.\n"); subserver(client1_sock, client2_sock); } else { //parent process //reset client sockets for more connections close(client1_sock); close(client2_sock); client1_sock = 0; client2_sock = 0; } } else { //client 1 has not connected yet client1_sock = accept_client(server_sock); dprintf("Received connection from first client.\n"); char msg = P_WAIT; //tell the client just to wait if(send(client1_sock, &msg, sizeof(msg), 0) < 0) { printf("Unable to send: %s\n", strerror(errno)); exit(1); } } } }
// // Handles the IDA window messages for the hidden control window. // static LRESULT CALLBACK control_window_proc(HWND wnd, UINT msg, WPARAM wp, LPARAM lp) { switch (msg) { case WM_NETEVENT: { switch (WSAGETSELECTEVENT(lp)) { case FD_ACCEPT: accept_client(); break; case FD_READ: process_client((SOCKET)wp); break; case FD_CLOSE: close_client((SOCKET)wp); break; } } break; default: return DefWindowProc(wnd, msg, wp, lp); } return 0; }
int main(int argc, const char *argv[]) { int listen_fd, new_fd; listen_fd = init_server(); // init server, listen while (true) { new_fd = accept_client(listen_fd); if (new_fd == -1) { perror("accept"); continue; } /* * if accepted, fork() */ if (!fork()) { // this is the child process close(listen_fd); // child doesn't need the listener if (send(new_fd, "Hello, world!", 13, 0) == -1) perror("send"); bind_file(new_fd); start_service(); close_connection(); close(new_fd); exit(0); } close(new_fd); } return 0; }
RsiServer::RsiServer(int port, SysInfo *sysinfo){ this->_sysinfo = sysinfo; if(signal(SIGCHLD, sig_child) == SIG_ERR){ LOG_ERROR("could not bind SIGCHLD to sig_child"); exit(-1); } if(signal(SIGINT, sig_int) == SIG_ERR){ LOG_ERROR("could not bind SIGINT to sig_int"); exit(-1); } int server_sockfd = listen_port(port); int keep_alive = 1; if(setsockopt(server_sockfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&keep_alive, sizeof(keep_alive)) == -1){ LOG_ERROR("Could not set keep_alive option"); exit(-1); } while(1){ int client_sockfd = accept_client(server_sockfd); pid_t pid = fork(); if(pid == 0){ // child process close(server_sockfd); while (true) { if(communicate(client_sockfd) < 0) exit(-1); } } else{ close(client_sockfd); } } }
void check_new_connections() { int sockfd = accept_client(sck); if(sockfd != -1) { printf("\nNew client connected: %s\n", getpeer(sockfd).c_str()); clients.push_back(sockfd); } }
/** * Starts the server and transits into daemon mode, if not in debug mode. * Loops forever, accepting stream (TCP) connections. * Child is forked when a client connects. * * @param flag user-provided flags. */ void run_server(struct flags* flag) { int server_sock; /* start listening for clients */ server_sock = setup_server_socket(flag); /* attach signal handlers */ if (signal(SIGCHLD, server_sig_handler) == SIG_ERR) { err(EXIT_FAILURE, "cannot catch SIGCHLD"); } if (signal(SIGHUP, server_sig_handler) == SIG_ERR) { err(EXIT_FAILURE, "cannot catch SIGCHUP"); } /* Start accepting connections */ listen(server_sock, BACKLOG); /* daemonize if not in debug mode */ if (!flag->dflag) { if (daemon(1, 1) < 0) { errx(EXIT_FAILURE, "cannot transit into daemon mode"); } } /* Handle clients */ do { accept_client(flag, server_sock); } while (1); close(server_sock); }
/* * Autor: Bartosz Fr¹ckowiak * wwww: http://www.batas2.boo.pl/ */ int main(int argc, char *argv[]) { int fd = -1, sd = -1; int port = -1; struct t_call *call; if(argc != 2){ printf("Sposob uruchomienia: %s <port>\n", argv[0]); exit(1); }else{ port = atoi(argv[1]); } fd = local_work(port, &call); while (1) { if (t_listen(fd, call) < 0) error("serwer->t_listen", fd); if ((sd = accept_client(fd, call)) != -1) serwer_work(sd); } if (t_close(fd) < 0) error("serwer->t_close", fd, sd); if (t_close(sd) < 0) error("serwer->serwer_wrok->t_close", fd, sd); }
int main(int argc, char* argv[]) { int ret; // create server ret = create_server(); if(ret != 0) { printf("create server error\n"); return -1; } // send broad cast ret = send_broadcast(); if(ret != 0) { printf("send broadcast error\n"); return -2; } // accept client ret = accept_client(); if(ret != 0) { printf("accept_client error\n"); return -3; } return 0; }
static void server_accept_callback(int fd, uint32_t events, void *user_data) { struct server *server = user_data; struct client *client; enum btdev_type uninitialized_var(type); if (events & (EPOLLERR | EPOLLHUP)) { mainloop_remove_fd(server->fd); return; } client = malloc(sizeof(*client)); if (!client) return; memset(client, 0, sizeof(*client)); client->fd = accept_client(server->fd); if (client->fd < 0) { free(client); return; } switch (server->type) { case SERVER_TYPE_BREDRLE: type = BTDEV_TYPE_BREDRLE; break; case SERVER_TYPE_BREDR: type = BTDEV_TYPE_BREDR; break; case SERVER_TYPE_LE: type = BTDEV_TYPE_LE; break; case SERVER_TYPE_AMP: type = BTDEV_TYPE_AMP; break; case SERVER_TYPE_MONITOR: goto done; } client->btdev = btdev_create(type, server->id); if (!client->btdev) { close(client->fd); free(client); return; } btdev_set_send_handler(client->btdev, client_write_callback, client); done: if (mainloop_add_fd(client->fd, EPOLLIN, client_read_callback, client, client_destroy) < 0) { btdev_destroy(client->btdev); close(client->fd); free(client); } }
static gboolean handle_client(GIOChannel* source, GIOCondition condition, LXTermWindow* lxtermwin) { if (condition & G_IO_IN) { accept_client(source, lxtermwin); } if (condition & G_IO_HUP) { g_error("Server listening socket closed unexpectedly\n"); } return TRUE; }
int do_work(int servfd, char* fbDevice) { int* client_fd = NULL; int client_count = 0; int max_fd; fd_set readfs; int i, c; Log ("Starting main loop."); while (!end) { /* fill fd_set to check sockets for reading (client and server ones) */ Log ("<< select() on sockets... >>"); FD_ZERO(&readfs); max_fd = 0; FD_SET(servfd, &readfs); if (servfd > max_fd) max_fd = servfd; for (i = 0; i < client_count; ++i) { if (client_fd[i] < 0) continue; FD_SET (client_fd[i], &readfs); if (client_fd[i] > max_fd) max_fd = client_fd[i]; } if (select(max_fd + 1, &readfs, NULL, NULL, NULL) == -1) { if (errno == EINTR) { errno = 0; continue; } return -1; } /* check for input on client socket and handle it */ for (i = 0; i < client_count; ++i) { if (client_fd[i] < 0) continue; if (FD_ISSET(client_fd[i], &readfs)) { c = handle_client_input (client_fd[i], fbDevice); if (c < 0 && errno != EPIPE && errno!= ECONNRESET) return -1; /* connection finished */ close (client_fd[i]); client_fd[i] = -1; // no socket } } /* check whether we have incoming connection */ if (FD_ISSET(servfd, &readfs)) if (accept_client (servfd, &client_fd, &client_count) < 0) return -1; } Log ("- Caught SIGINT"); return cleanup(servfd, client_fd, client_count); }
int main(int argc, char **argv) { pid_t pid; int input[2], output[2], res; /* input output base on father process */ /* create pipe */ pipe(input); pipe(output); if ((pid = fork()) == -1) { perror("fork error"); } else if (pid == 0) { /* child */ close(input[0]); close(output[1]); accept_client(output[0], input[1]); } close(input[1]); close(output[0]); int fin = input[0]; int fout = output[1]; /* init thread pool */ init_pool(&thread_pool); /* init single list */ init_list(&head); /* init singal */ init_signal(); char addr_str[20]; unsigned long ip_addr; while (1) { res = read(fin, &ip_addr, sizeof(ip_addr)); if (res <= 0) { perror("read error"); } memset(addr_str, 0, sizeof(addr_str)); sprintf(addr_str, "%d.%d.%d.%d", ip_addr & 0xFF, (ip_addr >> 8) & 0xFF, (ip_addr >> 16) & 0xFF, (ip_addr >> 24) & 0xFF); //printf("%s\n", addr_str); build_rpc(addr_str); } }
void Server::on_accept(ClientConnection::ptr client, const boost::system::error_code & err) { if (err.value() == boost::asio::error::operation_aborted) { throw accept_aborted_exception(client); } if (err) { std::ostringstream oss; oss << "on_accept error: " << err; throw server_exception(oss.str(), client); } BOOST_LOG_TRIVIAL(info) << "Client accepted!"; client->start(); accept_client(); }
int main(void){ int server_socket = create_server(8080); if(server_socket == -1){ perror("Impossible de creer le serveur"); return -1; } while (1) { accept_client(server_socket); } return 0; }
int main(void) { int clientfd; setup_signals(); printf("*** Creating a server...\n"); if ((serverfd = new_server("server1.example.com")) < 0) { perror("Couldn't create server"); exit(EXIT_FAILURE); } printf("*** Created!\n" " This is an echo server.\n" " Anything received will be echoed back to the client.\n\n"); /* Accept a client */ if ((clientfd = accept_client(serverfd)) < 0) { perror("Couldn't accept client"); destroy_server(serverfd); exit(EXIT_FAILURE); } printf("*** Accepted incoming connection request.\n"); static char buf[512+1]; int n; while ((n = recv_info(clientfd, buf, sizeof(buf)-1)) > 0) { buf[n] = '\0'; if (!strcmp(buf, "__QUIT__NOW__\n")) { printf("*** Got disconnect request from client, leaving...\n"); disconnect_from(clientfd); break; } printf("*** Received: %s", buf); if (send_info(clientfd, buf, n) < 0) { perror("Error sending message to client"); } else { printf("*** Sent: %s", buf); } } printf("*** Goodbye\n"); destroy_server(serverfd); return 0; }
int main(int argc, const char *argv[]) { start_processes(5); create_rpc_listeners(); // 等待各 rpc 结点完成侦听 sleep(1); make_link_to_peers(); accept_client(); return 0; }
/** * 参数arg是监听端口 */ void* pthread_handler(void* arg) { int sockfd = (int) arg; char buf[BUF_SIZ + 1]; // 这里和blocked_server.c不同,将buf移入线程中 // 循环接受客户端请求,同一时刻只能处理一个用户 int cli_sockfd; // 当前客户端fd for (;;) { if ((cli_sockfd = accept_client(sockfd, 0, DEBUG)) < 0) { continue; } // 处理客户端请求,直到该函数返回才继续处理下一位用户 handle_client(cli_sockfd, buf); } return NULL; }
std::string& RadarServer::get_request(void) { transmit_buffer.clear(); if (sock_client > 0) finalize_client(); if ((sock_listen < 0) && !start_listening()) { DERR << "listener socket invalid, " << "restarting in some seconds..." << std::endl; sleep(5); } else if (accept_client()) { if (sock_getcmd()) shutdown(sock_client, SHUT_RD); else finalize_client(); } return transmit_buffer; }
int main(int argc, char *argv[]) { int retcode = EXIT_SUCCESS; prog_options_t my_opt; // read program options if (get_options(argc, argv, &my_opt) == 0) { print_usage(my_opt.progname); exit(EXIT_FAILURE); } /* end if */ // set the time zone (TZ) to GMT in order to // ignore any other local time zone that would // interfere with correct time string parsing setenv("TZ", "GMT", 1); tzset(); // do some checks and initialisations... open_logfile(&my_opt); check_root_dir(&my_opt); install_signal_handlers(); init_logging_semaphore(); // get root_dir to handle it later in child process char* root_dir = my_opt.root_dir; // start the server and create socket printf("[%d] Starting server '%s'...\n", getpid(), my_opt.progname); int accepting_socket = passive_tcp(my_opt.server_port, 5); struct sockaddr_in from_client; server_running = true; while(server_running) { socklen_t from_client_len = sizeof(from_client); // Accept new Client int listening_socket = accept(accepting_socket, (struct sockaddr *) &from_client, &from_client_len); accept_client(accepting_socket, listening_socket, root_dir); } /* end while */ printf("[%d] Good Bye...\n", getpid()); exit(retcode); } /* end of main */
void Server::start() { BOOST_LOG_TRIVIAL(info) << "Server start"; _started = true; _stopped = false; try { accept_client(); service_run_loop(); } catch (const std::exception &e) { BOOST_LOG_TRIVIAL(fatal) << "Unexpected std::exception: " << e.what(); stop_finish(); } catch (...) { BOOST_LOG_TRIVIAL(fatal) << "Unexpected unknown exception"; stop_finish(); } }
void NetTestThread(cyg_addrword_t arg) { int listen_fd = create_listen_socket(8181); while(listen_fd >= 0) { int fd = accept_client(listen_fd); diag_printf("accept fd=%d\n", fd); if(fd >= 0) { int i; for(i = 0; i < 100; ++i) write(fd, "abcd\r\n", 6); close(fd); } diag_printf("Retry accept\n"); } diag_printf("After while\n"); close(listen_fd); }
void FtpServer::accept_new_clients(){ for(int i=0; i < MAX_CLIENTS; i++) { // if open slot if( !(client[i].conn)) { // if accept client works if(accept_client(&client[i])) { client[i].runner = true; updateStatus(CLIENT_CONN); while(client[i].runner){ // Get this clients request frame getFrame(&client[i]); //Handle request frame client[i].runner = handleFrame(&client[i]); } } } } }
int main(void) { int sockfd; pthread_t udp_tid; struct sockaddr_in serv_addr; struct sockaddr_in udp_ser_addr; if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == ERR) pri_err("socket"); bzero(&serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; udp_ser_addr.sin_family = AF_INET; serv_addr.sin_port = htons(PORT); udp_ser_addr.sin_port = htons(5000); //inet_pton(AF_INET, IP, &serv_addr.sin_addr); serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); udp_ser_addr.sin_addr.s_addr = htonl(INADDR_ANY); int udp_sock = socket(AF_INET, SOCK_DGRAM, 0); if(udp_sock < 0) { perror("socket udp"); exit(1); } if(bind(udp_sock, (struct sockaddr *)&udp_ser_addr, (socklen_t)sizeof(struct sockaddr)) < 0) { perror("bind"); exit(1); } pthread_create(&udp_tid, NULL, pthread_udp,&udp_sock); //端口重用 int on = 1; if((setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) == ERR) pri_err("setsockopt"); if((bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))) == ERR) pri_err("bind"); if((listen(sockfd, MAX_USER)) == ERR) pri_err("listen"); printf("waitting client connection…………\n"); accept_client(sockfd); return 0; }
int accept_client (int sockfd) { int clientfd = -1; socklen_t addrlen; string_t client_addr = NULL; struct sockaddr_in cli_addr; string_t msg_print = NULL; assert (sockfd >= 0); addrlen = sizeof (cli_addr); if (listen (sockfd, MAX_CLIENTS) != 0) { fprintf (stderr,"listen"); return SERVER_ERR; /* because the error is handled by main */ } clientfd = accept (sockfd, (struct sockaddr *) &cli_addr, &addrlen); if (clientfd == -1) { fprintf (stderr, "accept"); } else{ client_addr = inet_ntoa (cli_addr.sin_addr); if (client_addr) { msg_print = (string_t) req_mem (strlen(client_addr) + 24, sizeof (char), MAXTRIES); sprintf (msg_print, "Conected with: ( '%s' )\n", client_addr); pflog (msg_print, STANDARD); free (msg_print); msg_print = NULL; } else { pflog ("The address of the client could not be obtained\n", STANDARD); msg_print = (string_t) req_mem (40, sizeof (char), MAXTRIES); sprintf (msg_print, "Closing the conection with the socket %i...\n", clientfd); pflog (msg_print, STANDARD); free (msg_print); msg_print = NULL; server_quit (clientfd); pflog ("Listening again...\n", STANDARD); clientfd = accept_client(sockfd); } } return clientfd; }
static bool tick(int timeout_ms) { int numready; bool raft_ready = false; fd_set readfds = server.all; struct timeval timeout = ms2tv(timeout_ms); numready = select(server.maxfd + 1, &readfds, NULL, NULL, &timeout); if (numready == -1) { fprintf(stderr, "failed to select: %s\n", strerror(errno)); return false; } if (FD_ISSET(server.listener, &readfds)) { numready--; accept_client(); } if (FD_ISSET(server.raftsock, &readfds)) { numready--; raft_ready = true; } Client *c = server.clients; while (numready > 0) { Assert(c - server.clients < MAX_CLIENTS); if ((c->sock >= 0) && (FD_ISSET(c->sock, &readfds))) { attend(c); numready--; } c++; } drop_bads(); return raft_ready; }
int main(int argc, char **argv) { int client, ret; char listen_path[PATH_MAX]; if (argc < 2) { printf("Usage: %s LISTEN_PORT\n", argv[0]); exit(EXIT_FAILURE); } signal(SIGCHLD, SIG_IGN); establish_server(argv[1], listen_path, sizeof listen_path); for (; ; close(client)) { client = accept_client(listen_path); ret = fork(); if (ret == 0) echo_server(client); else if (ret < 0) perror("fork"); } return EXIT_SUCCESS; }
// ACEPTAR UNA CONEXION Y CERRAR PUERTO DE ESCUCHA // RETORNA -1 SI ERROR, DESCRIPTOR DEL SOCKET SI NO int quick_accept(int port) { int aux; int fd=socket_bind("0.0.0.0", port); if(fd==-1) return -1; aux=listen(fd, 0); // ESCUCHAR if(aux==-1) // SI ERROR, A JODERSE { close(fd); return -1; } aux=accept_client(fd); // ACEPTAR 1ª PETICION Y CERRAR close(fd); // NO QUEREMOS ESCUCHAR MAS CONEXIONES ENTRANTES if(aux==-1) return -1; return aux; }
int main() { pid_t pid; pid = fork(); if (pid < 0) { perror("fork()"); } else if (pid > 0) { int lsn_fd, apt_fd; char buf[128]; int ret; printf("parent pid %d\n", getpid()); lsn_fd = init_listen_server(REQUEST); apt_fd = accept_client(lsn_fd); ret = read(apt_fd, buf, 124); //ret = recv(apt_fd, buf, 124, 0); if (ret > 0) { buf[ret] = '\0'; printf("%d, %s\n", getpid(), buf); } } else { int fd; printf("child pid %d\n", getpid()); fd = connect_server(REQUEST); if (fd > 0) { write(fd, "I am child", strlen("I am child.") + 1); //send(fd, "I am child", strlen("I am child.") + 1, 0); } } return 0; }
void server_start_listening_clients(ServerData *server, const int socket){ //Server shouldn't already be listening if(server->is_listening == TRUE){ fprintf(stdout, "Server is already listening.\n"); return; } server->is_listening = TRUE; fprintf(stdout, "Server start listening for new clients.\n"); //Listen for client, start thread for each new connected while(server->is_listening == TRUE){ fprintf(stdout, "Wait for client...\n"); int client_socket = accept_client(socket); //accept new client //Create thread args struct thread_info tinfo; pthread_t thread_id; memset(&tinfo, 0x00, sizeof(tinfo)); tinfo.server = server; tinfo.socket = client_socket; pthread_create(&thread_id, NULL, client_handler, (void*)&tinfo); pthread_detach(thread_id); } }
int connect_to_client() { // The socket on which the server receives // all incoming connections int server_socket; // The socket for unique communication with the client int client_socket; server_socket = create_server_socket(); client_socket = accept_client(server_socket); // Don't need the server socket anymore (only have one connection) // close(client_socket); if (fork() != (pid_t)0) { // Don't need the server socket anymore (only have one connection) close(client_socket); close(server_socket); exit(EXIT_SUCCESS); } return client_socket; }