예제 #1
0
파일: net.c 프로젝트: faf0/sws
/**
 * Waits for client request. When a client connects a new process is forked
 * to handle the request. Calls function to communicate with it.
 *
 * @param flag user-provided flags
 * @param server_sock the server socket file descriptor
 */
static void
accept_client(struct flags *flag, int server_sock)
{
  struct sockaddr_storage client;
  socklen_t client_length;
  int client_sock;

  client_length = sizeof(client);
  client_sock = accept(server_sock, (struct sockaddr *) &client,
      &client_length);
  if (client_sock < 0) {
    perror("accept");
  } else {
    switch (fork()) {
    case -1:
      err(EXIT_FAILURE, "cannot fork child to handle client");
      /* NOTREACHED */
      /* existing childs become adopted by init. no need to reap them. */
      break;
    case 0:
      handle_client(client_sock, &client, client_length, flag);
      exit(EXIT_SUCCESS);
      /* NOTREACHED */
      break;
    default:
      /* parent cleans duplicate client socket */
      (void) close(client_sock);
      break;
    }
  }
}
예제 #2
0
파일: client.c 프로젝트: AllardJ/Tomato
int main(int argc, char *argv[])
{
   int sockfd, port;
   char host[200];
   char msg[200], *p, *cmd;

   strcpy(host, SERV_HOST_ADDR);
   port = SERV_TCP_PORT;

   if (argc > 1) {
      strcpy(host, argv[1]); /* get host from command line */
      p = strchr(host, ':');
      if (p) {
	 *p++ = 0;
	 port = atoi(p);
      }
   }

   if (argc > 2) {
      cmd = argv[2];
   } else {
      cmd = NULL;
   }

   if ((sockfd = net_open(host, NULL, port)) < 0) {
      sprintf(msg, "client: tcp_open for host %s on %d failed\n", host, port);
      error_abort(msg);
   }

   handle_client(stdin, sockfd, cmd);	    /* do it all */
   net_close(sockfd);
   exit(0);
}
예제 #3
0
파일: main.cpp 프로젝트: hpsMouse/yasocks
 void exec()
 {
     acceptor.async_accept(peer, peer_endpoint, error_handler("async_accept", [this]{
         handle_client(std::move(peer), std::move(peer_endpoint));
         exec();
     }));
 }
예제 #4
0
void accept_connection(int sock)
{
        struct sockaddr_in client_addr;
        int clientfd, pid, addrlen = sizeof(client_addr);
        /*Loop forever*/
        while(1)
        {
                if((clientfd = accept(sock, (struct sockaddr *)&client_addr, &addrlen)) < 0)
                {
                        edie("accept");
                }

                if((pid = fork()) < 0)
                {
                        edie("fork");
                }

                if(pid == 0)
                {
                        /*child process, handle client*/
                        close(sock);
			handle_client(clientfd);
			return;
                }

                else
                {
                        /*parent process, return to loop*/
                        close(clientfd);
                }


        }
	/*parent never returns*/
}
예제 #5
0
void do_accept(int listen_sockfd,std::string& cmds) {
	int peer_sockfd;
	struct sockaddr_in6 src;
	socklen_t srclen;
	pid_t child;

	/* accept the connection */
	srclen = sizeof(src);
	peer_sockfd = accept(
		listen_sockfd, 
		(struct sockaddr *)&src, 
		&srclen
	);

	if (peer_sockfd < 0) {
		perror("accept");
		return;
	}

	child = fork();
	if (child == -1) {
		perror("fork");
	} else if (child == 0) {
        //child return 0 means it is in the child process
		handle_client(peer_sockfd, &src, srclen,cmds);
		exit(0);
	}

	/* 
	 * if we get here, either the fork failed or 
	 * we are in the parent. Either way, we need to
	 * close the socket.
	 */
	close(peer_sockfd);
}
예제 #6
0
파일: server.c 프로젝트: Meldanor/Gnuddels
void
serverLoop(void) {

    while (true) {
        // res stores the numbers of file descriptors throwed an event
        int res = poll(pollList->elements, pollList->size, INFINITE_TIMEOUT);
        // Poll returns without any events
        if (res == 0) {
            continue;
        }
        if (res < 0) {
            perror("poll failed!");
            break;
        }
        int i;
        struct pollfd *pollfd = NULL;
        for (i = 0 ; i < pollList->size; ++i) {
            pollfd = pollVector_get(pollList, i);
            // This fd didn't fired an event
            if (pollfd->revents == 0) {
                continue;
            }
            // The fd want to send something
            if ((pollfd->revents & POLLIN) == POLLIN) {
                // New client want to connect
                if (pollfd->fd == serverSocket) {
                    // try to accept new client
                    if (accept_newClient() == EXIT_FAILURE) {
                        return;
                    }
                }
                // Connected client want to send something
                else {
                    // Read it's input and handle it
                    int cRes = handle_client(pollfd->fd);
                    // sconnected
                    if (cRes == CLIENT_DISCONNECTED) {
                        printf("Client %d disconnected\n", pollfd->fd);
                        remove_client(pollfd->fd);
                        --i;
                    }
                    // An error occurred
                    else if (cRes == EXIT_FAILURE) {
                        printf("Client %d crashed!\n", pollfd->fd);
                        remove_client(pollfd->fd);
                        --i;
                    }
                    else {
                        // Do Nothing
                    }
                }
            }
            // Unregistered poll event was thrown
            else {
                fprintf(stderr, "Unkown poll event %d! Server is stopping\n", pollfd->revents);
                return;
            }
        }
    }
}
예제 #7
0
int		handle_clients(int fd, char **pwds)
{
  pid_t		pid;
  int		fd_client;

  pid = 1;
  if (pwds && !get_conf(pwds[ROOT], 0))
    return (my_perror(EALLOC, FAILURE, 0, fd));
  while (pid != CHILD)
    {
      if ((fd_client = accept(fd, NULL, NULL)) == -1)
	return (my_perror(EACCEPT, EXIT_FAILURE, fd, 0));
      if ((pid = fork()) == -1)
	return (my_perror(EFORK, FAILURE, fd, fd_client));
      if (pid == CHILD)
	{
	  if (close(fd) == -1)
	    return (my_perror(ESCLOSE, FAILURE, 0, fd_client));
	  if (!handle_client(fd_client, pwds))
	    return (FAILURE);
	}
      else
	if (close(fd_client) == -1)
	  return (my_perror(ECCLOSE, FAILURE, fd, 0));
    }
  return (SUCCESS);
}
예제 #8
0
파일: sgreen.c 프로젝트: ruhler/focus
// wrapper around handle_client for pthreads.
// ud should be a pointer to a malloced client.
void* handle_client_thread(void* ud)
{
    CNSL_Client* client = (CNSL_Client*)ud;
    handle_client(*client);
    free(client);
    return NULL;
}
예제 #9
0
// Currently this main will setup the socket and accept a single client.
// After handling the client, it will exit.
// You will need to extend this to handle multiple concurrent clients
// and remain running until it receives a shut-down command.
int main()
{
    int server_socket = setup_server();
    if (server_socket < 0) {
        exit(1);
    }

    status s = grab_persisted_data();
    if (s.code != OK) {
        log_err(s.error_message);
        return 0;
    }

    // Populate the global dsl commands and catalogs
    dsl_commands = dsl_commands_init();
    catalogs = init_catalogs();

    log_info("Waiting for a connection %d ...\n", server_socket);

    struct sockaddr_un remote;
    socklen_t t = sizeof(remote);
    int client_socket = 0;

    if ((client_socket = accept(server_socket, (struct sockaddr *)&remote, &t)) == -1) {
        log_err("L%d: Failed to accept a new connection.\n", __LINE__);
        exit(1);
    }
    handle_client(client_socket);

    return 0;
}
예제 #10
0
파일: main.c 프로젝트: yy-nm/irc-client
int main(int argc, void **args)
{
	short port = IRC_SERVER_PORT;
	char *ip = IRC_SERVER_URL;
	if (argc >= 2)
		ip = (char *)args[1];
	if (argc >= 3) {
		// port = (short)*(int *)args[2];
		port = (short) atoi((char *)args[2]);
	}
	int socket_fd, client_fd;
	struct sockaddr_un local_addr, client_addr;
	if ((socket_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
		perror("socket error");
		perror(strerror(errno));
		return -1;
	}

	local_addr.sun_family = AF_UNIX;
	strcpy(local_addr.sun_path, SOCK_PATH);
	unlink(local_addr.sun_path);
	int len = strlen(local_addr.sun_path) 
		+ sizeof(local_addr.sun_family);
	if (-1 == bind(socket_fd, (struct sockaddr *)&local_addr, len)) {
		perror("bind error");
		perror(strerror(errno));
		return -2;
	}
	
	if (-1 == listen(socket_fd, SOCK_MAX_CLIENT)) {
		perror("listen error");
		perror(strerror(errno));
		return -3;
	}

	// printf("unix domain socket start");
	for(;;) {
		memset(client_addr.sun_path, 0
				, sizeof(client_addr.sun_path));
		len = sizeof(client_addr);
		if (-1 == (client_fd = accept(socket_fd
					, (struct sockaddr *)&client_addr
					, &len))) {
			perror("accept error");
			perror(strerror(errno));
			exit(-4);
		}
		// printf("client info: path: %s\n", client_addr.sun_path);
		if (0 == fork()) {
			close(socket_fd);
			handle_client(client_fd, ip, port);
			exit(0);
		}
		close(client_fd);
	}
	close(socket_fd);

	return 0;
}
예제 #11
0
파일: birdsv.c 프로젝트: kczajkow/pcbird
int handle_requests()
{
    struct sockaddr_in remote;
    struct pollfd pfds[MAX_CLIENTS];
    int cli_fd, i, n;
    
    size_t len = sizeof(remote);

// akceptujemy przychodzace polaczenia i dodajemy nowych klientow do tablicy
    while ((cli_fd = accept(sock_fd, (struct sockaddr *)&remote, &len))>0)
	if( add_client(cli_fd) < 0) 
	{
	    close(cli_fd);
	    return 0;
	}


// przygotowujemy liste deskryptorow dla poll()-a
    for(n=i=0;i < MAX_CLIENTS;i++)
	if(client_tab[i].fd)
	{    
	    pfds[n].fd = client_tab[i].fd;
	    pfds[n].events = POLLIN | POLLHUP; // reagujemy na przychodzace dane i zerwanie sesji
	    pfds[n].revents = 0;

	    n++;
	}    

    int ret;

// pollujemy klientow    
    if(( ret = poll(pfds, n, 10)) < 0) die("poll error: %s", strerror(errno));

// brak ruchu - koniec pracy funkcji
    if(!ret)  return 0; 

// sprawdzamy, co sie wydarzylo:
    for(i=0; i<n ;i++)
    {
	if(pfds[i].revents & POLLHUP)					// klient zerwal polaczenie?
	{
	    fprintf(stderr, "[lsrv] client %d closed connection.\n", pfds[i].fd);
	    close(pfds[i].fd);
	    remove_client(pfds[i].fd);
	} else if(pfds[i].revents & POLLIN)				// nadeszly dane od klienta?
	{
	    if(handle_client(find_client(pfds[i].fd)) < 0)
	    {
	        close(pfds[i].fd);
		remove_client(pfds[i].fd);
    	    }
	}  	
    }
    
    return 0;    

}
예제 #12
0
int 
umain(void)
{
	int serversock, clientsock;
	struct sockaddr_in echoserver, echoclient;
	char buffer[BUFFSIZE];
	unsigned int echolen;
	int received = 0;
	
	// Create the TCP socket
	if ((serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
		die("Failed to create socket");
	
	cprintf("opened socket\n");
	
	// Construct the server sockaddr_in structure
	memset(&echoserver, 0, sizeof(echoserver));       // Clear struct
	echoserver.sin_family = AF_INET;                  // Internet/IP
	echoserver.sin_addr.s_addr = htonl(INADDR_ANY);   // IP address
	echoserver.sin_port = htons(PORT);		  // server port
	
	cprintf("trying to bind\n");
	
	// Bind the server socket
	if (bind(serversock, (struct sockaddr *) &echoserver,
		 sizeof(echoserver)) < 0) 
	{
		die("Failed to bind the server socket");
	}

	// Listen on the server socket
	if (listen(serversock, MAXPENDING) < 0)
		die("Failed to listen on server socket");
	
	cprintf("bound\n");
	
	// Run until cancelled
	while (1) {
		unsigned int clientlen = sizeof(echoclient);
		// Wait for client connection
		if ((clientsock =
		     accept(serversock, (struct sockaddr *) &echoclient,
			    &clientlen)) < 0) 
		{
			die("Failed to accept client connection");
		}
		cprintf("Client connected: %s\n", inet_ntoa(echoclient.sin_addr));
		handle_client(clientsock);
	}
	
	closesocket(serversock);
	
	return 0;
}
예제 #13
0
int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  const ACE_TCHAR *rendezvous = argc > 1 ? argv[1] : ACE_DEFAULT_RENDEZVOUS;
  // Create a server.
  ACE_OS::unlink (rendezvous);
  ACE_UNIX_Addr addr (rendezvous);
  ACE_LSOCK_Acceptor peer_acceptor (addr);
  ACE_LSOCK_Stream stream;

  // Performs the concurrent server activities.

  for (;;)
    {
      // Create a new ACE_SOCK_Stream endpoint.
      if (peer_acceptor.accept (stream) == -1)
        ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) %p\n", "accept"), -1);

      ACE_DEBUG ((LM_DEBUG, "(%P|%t) accepted new connection\n"));

#if defined (VXWORKS)
      handle_client (stream);
#else
      switch (ACE_OS::fork (argv[0]))
        {
        case -1:
          ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) %p\n", "fork"), -1);
          /* NOTREACHED */
        case 0:
          ACE_LOG_MSG->sync (argv[0]);
          handle_client (stream);
          ACE_OS::exit (0);
          /* NOTREACHED */
        default:
          stream.close ();
        }
#endif /* VXWORKS */
    }

  ACE_NOTREACHED (return 0;)
}
예제 #14
0
int main(int argc, char** argv){
    dpid = fork();
    if (dpid != 0) {
        waitpid(dpid, NULL, 0);
        //int status;
        //wait3(&status, WNOHANG, NULL);
        exit(0);
    }
    int sid = setsid();
    init_net();
    if (sid < 0) {
        perror("session creation error");
        exit(1);
    }
    std::vector<pollfd> pollfds(fds.size());    
    for (int i = 0; i < pollfds.size(); i++) {
        pollfds[i].fd = fds[i];
        pollfds[i].events = POLLIN | POLLERR | POLLHUP | POLLRDHUP | POLLNVAL;
        pollfds[i].revents = 0;
    }
    for(;;){
        int ret = poll(pollfds.data(), pollfds.size(), -1);
        int status;
        wait3(&status, WNOHANG, NULL);
        if(ret == 0) continue;
        for (int i = 0; i < pollfds.size(); i++) {
            if (pollfds[i].revents & (POLLERR | POLLHUP | POLLRDHUP | POLLNVAL)) {
                pollfds[i].events = 0;
                close(fds[i]);
                perror("socket has been disconnected"); //this shoud not be reachable
                _exit(1);
            }
            if (pollfds[i].revents & POLLIN) {
                int cfd = accept(fds[i], NULL, NULL);
                if (cfd < 0) {
                    perror("fd < 0");
                    continue;
                }
                printf("Accepting %d\n", cfd);
                int pid = fork();
                if (pid == 0) {
                    for (int i = 0; i < fds.size(); i++) {
                        close(fds[i]);
                    }
                    handle_client(cfd);
                    return 0;
                }
                close(cfd);
            }
        }
        
    }
}
예제 #15
0
static void serv(struct sstate *ss, char *dev, int port, int chan)
{
	int max;
	fd_set fds;
	struct client *c;
	int card_fd;

	open_card_and_sock(ss, dev, port, chan);
	card_fd = wi_fd(ss->ss_wi);

	while (1) {
		/* server */
		max = ss->ss_s;
		FD_ZERO(&fds);
		FD_SET(max, &fds);

		/* clients */
		c = ss->ss_clients.c_next;
		while (c != &ss->ss_clients) {
			FD_SET(c->c_s, &fds);
			if (c->c_s > max)
				max = c->c_s;

			c = c->c_next;
		}

		/* card */
		FD_SET(card_fd, &fds);
		if (card_fd > max)
			max = card_fd;

		if (select(max+1, &fds, NULL, NULL, NULL) == -1)
			err(1, "select()");

		/* handle clients */
		c = ss->ss_clients.c_next;
		while (c != &ss->ss_clients) {
			if (FD_ISSET(c->c_s, &fds))
				handle_client(ss, c);

			c = c->c_next;
		}

		/* handle server */
		if (FD_ISSET(ss->ss_s, &fds))
			handle_server(ss);

		if (FD_ISSET(card_fd, &fds))
			handle_card(ss);
	}
}
예제 #16
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;
}
예제 #17
0
파일: server.c 프로젝트: tfc/madmonkey
void* thread_main(void *arg)
{
  int client_sock;

  // Guarantee to deallocate thread resources upon return
  pthread_detach(pthread_self());

  // Extract socket file descriptor from argument
  client_sock = ((struct thread_args_t *)arg)->client_sock;
  free(arg);

  handle_client(client_sock);

  return 0;
}
예제 #18
0
파일: httpd.c 프로젝트: pombredanne/jos
void
umain(int argc, char **argv)
{
	int serversock, clientsock;
	struct sockaddr_in server, client;

	binaryname = "jhttpd";
	
	// Set page fault hanlder
	set_pgfault_handler(handler);

	// Create the TCP socket
	if ((serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
		die("Failed to create socket");

	// Construct the server sockaddr_in structure
	memset(&server, 0, sizeof(server));		// Clear struct
	server.sin_family = AF_INET;			// Internet/IP
	server.sin_addr.s_addr = htonl(INADDR_ANY);	// IP address
	server.sin_port = htons(PORT);			// server port

	// Bind the server socket
	if (bind(serversock, (struct sockaddr *) &server,
		 sizeof(server)) < 0)
	{
		die("Failed to bind the server socket");
	}

	// Listen on the server socket
	if (listen(serversock, MAXPENDING) < 0)
		die("Failed to listen on server socket");

	cprintf("Waiting for http connections...\n");

	while (1) {
		unsigned int clientlen = sizeof(client);
		// Wait for client connection
		if ((clientsock = accept(serversock,
					 (struct sockaddr *) &client,
					 &clientlen)) < 0)
		{
			die("Failed to accept client connection");
		}
		handle_client(clientsock);
	}

	close(serversock);
}
예제 #19
0
/* Main server loop */
void server_loop()
{
    struct sockaddr_in client_addr;
    int addrlen = sizeof(client_addr);

    while (TRUE) {
        client_sock = accept(server_sock, (struct sockaddr*)&client_addr, (socklen_t *)&addrlen);
        if (fork() == 0) { // handle client connection in a separate process
            close(server_sock);
            handle_client(client_sock, client_addr);
            exit(0);
        }
        close(client_sock);
    }

}
예제 #20
0
파일: mproxy.c 프로젝트: CCbird/mproxy
void server_loop() {
    struct sockaddr_in client_addr;
    socklen_t addrlen = sizeof(client_addr);

    while (1) {
        client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &addrlen);
        
        if (fork() == 0) { // 创建子进程处理客户端连接请求
            close(server_sock);
            handle_client(client_sock, client_addr);
            exit(0);
        }
        close(client_sock);
    }

}
예제 #21
0
파일: main.c 프로젝트: sgiurgiu/smlaunch
void start_listening()
{
	int clisockfd;
	socklen_t client_socket_len;
	struct sockaddr_in cli_addr;

	client_socket_len= sizeof(cli_addr);
	while(TRUE)
	{
		sm_log(LOG_DEBUG,"started to listen\n");
		clisockfd = accept(sockfd,(struct sockaddr*)&cli_addr,&client_socket_len);
		if(clisockfd>=0)
		{
			handle_client(clisockfd);
		}
	}
}
예제 #22
0
파일: main_server.c 프로젝트: nsierra-/ftp
static void				accept_connection(void)
{
	struct sockaddr_in6 client_addr;
	unsigned int		client_addr_len;
	int					new_sock_fd;
	int					fd;
	int					pid;

	fd = g_server_data.socket_descriptor;
	client_addr_len = sizeof(client_addr);
	new_sock_fd = accept(fd, (struct sockaddr*)&client_addr, &client_addr_len);
	ft_putendl("New connection established");
	if ((pid = fork()) == 0)
		handle_client(new_sock_fd);
	else
		close(new_sock_fd);
}
예제 #23
0
int
main(void)
{
	int listen_socket, client_socket;

	init_unicorn_hat();
	listen_socket = setup_listen_socket();

	while (true) {
		client_socket = do_accept(listen_socket);

		if (client_socket != -1) {
			handle_client(client_socket);
		}
	}

	return 0;
}
예제 #24
0
파일: main.c 프로젝트: dmateos/scratch
int listener() {
    int lsock, csock, csockinfosize, pid;
    struct sockaddr_in lsockaddr, csockinfo;

    /* Clear/init some data. */
    memset(&lsockaddr, 0, sizeof(lsockaddr));
    memset(&csockinfo, 0, sizeof(csockinfo));
    csockinfosize = sizeof(csockinfo);

    /* Setup our server socket to a listening state. */
    lsockaddr.sin_family = AF_INET;
    lsockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    lsockaddr.sin_port = htons(2100);

    if((lsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        exit(1);
    }
    if(bind(lsock, (struct sockaddr*)&lsockaddr, sizeof(lsockaddr)) < 0) {
        exit(1);
    }
    if(listen(lsock, 1) < 0) {
        exit(1);
    }

    ftp_state_init();

    /* Accept/child fork loop. */
    for(;;) {
        csock = accept(lsock, (struct sockaddr*)&csockinfo, &csockinfosize);
        pid = fork();

        /* Child */
        if(pid == 0) {
            handle_client(csock, &csockinfo);
            close(csock);
            exit(0);
        }
        /* Parent. */
        else {
            close(csock);
        }
    } 
    return 0;
}
예제 #25
0
파일: reverse_tcp.c 프로젝트: hammackj/saru
void *handler_reverse_tcp_start(struct module_t *module)
{
	int listen_fd = 0;
	int client_fd = 0;
	struct sockaddr_in serv_addr;
	struct module_t *self;

	self = module;

	(void) self;

#ifdef SIGPIPE
	signal (SIGPIPE, SIG_IGN);
#endif

	listen_fd = socket(AF_INET, SOCK_STREAM, 0);
	memset(&serv_addr, '0', sizeof(serv_addr));
	
	serv_addr.sin_family = AF_INET;
	serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
	serv_addr.sin_port = htons(4444); 

	bind(listen_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); 
	listen(listen_fd, 10);

	print_status("Handler listening on port 4444");

	client_fd = accept(listen_fd, (struct sockaddr*)NULL, NULL);

	print_status("Client accepted");

	if (client_fd == -1)
	{
		perror("client error");
		print_error("Exploit failed.");
	}

	handle_client(client_fd);

	close(client_fd);

	return 0;
}
예제 #26
0
void run_server(int port){
	// Socket Identifier	
	int server_sock, client_sock;
	// Socket Address
	struct sockaddr *server_addr, *client_addr;
	// * Avoid Undefined Behavior
	server_addr = NULL;
	client_addr = NULL;
	// Attempt To Bind Server
	server_sock = sl_tcp_server(sl_sockaddr_server(port));	
	// Allocate a sockaddr for the client
	client_addr = sl_sockaddr();

	signed char running = 1;
	
	if(server_sock < 0){
		util_err("(sl_tcp_server) binding socket");
		running = 0;
	}

	if(OPTION_VERBOSE==1)
		util_startup(port);
	
	while(running){
		client_sock = sl_accept(server_sock,client_addr);
		
		if(client_sock < 0){
			util_err("(sl_accept) accepting connection.");
			running = 0;
		}else{
			util_say("Accepted Connection.");
		}
		
		handle_client(client_sock);
		close(client_sock);	
		util_say("Closed Connection.");
	}
	
	// We check != NULL here incase our addr's failed to be allocated -
	// which happens for server_addr if sl_tcp_server fails to bind.
	if(server_addr!=NULL) free(server_addr);
	if(client_addr!=NULL) free(client_addr);
}
예제 #27
0
파일: player.c 프로젝트: atrinik/dwc
/**
 * This is similar to handle_player(), but is only used by the new
 * client/server stuff.
 *
 * This is sort of special, in that the new client/server actually uses
 * the new speed values for commands.
 * @param pl Player to handle.
 * @retval -1 Player is invalid.
 * @retval 0 No more actions to do.
 * @retval 1 There are more actions we can do. */
int handle_newcs_player(player *pl)
{
	object *op = pl->ob;

	if (!op || !OBJECT_ACTIVE(op))
	{
		return -1;
	}

	handle_client(&pl->socket, pl);

	if (!op || !OBJECT_ACTIVE(op) || pl->socket.status == Ns_Dead)
	{
		return -1;
	}

	/* Check speed. */
	if (op->speed_left < 0.0f)
	{
		return 0;
	}

	/* If we are here, we're never paralyzed anymore. */
	CLEAR_FLAG(op, FLAG_PARALYZED);

	if (op->direction && (CONTR(op)->run_on || CONTR(op)->fire_on))
	{
		/* All move commands take 1 tick, at least for now. */
		op->speed_left--;
		move_player(op, op->direction);

		if (op->speed_left > 0)
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}

	return 0;
}
예제 #28
0
void* slaveWork() {
	CLIENT client;
	sigset_t pending_signals;

	pthread_mutex_lock(&p_mutex);
	while(1) {
		/* Always stays in a loop getting FDs from the list and processing */
		if( clients_desc.count > 0 ) { /* Is there any FD in the list waiting to be processed? */
			client = get_client();
			pthread_mutex_unlock(&p_mutex);
			/* Ok, now others threads can access the list and process other requests */

			handle_client(client);

			/* KILLING ZONE *******************************/
			/* it has to be outside the RC !!! */
			/* is it time to die already? ;( */
			if( sigpending(&pending_signals) < 0 ) {
				fprintf(stdout,":: ERROR!\n:: A slave failed to check pending signals.\n:: Killing the slave. Another one shall be issued if necessary.\n");
				break;
			} else if( sigismember(&pending_signals, SIGINT) ) {
				break;
			}
			/***********************************************/

			pthread_mutex_lock(&p_mutex);
		} else {
			pthread_cond_wait(&p_cond_var, &p_mutex);
			/*There is no FD in the FDs' list.
			So, we use condition variables to lock this specific thread and force it to wait until a new FD arrives
			It's important to note that when we use pthread_cond_wait the thread running at this point will
			wait until a new FD is available in the list. Also, in this moment the lock is released and it gives
			access to protected resources to other threads. The return from pthread_cond_wait locks the mutex again. So, we don't
			need to take care of this*/
		}
	}

	if( VRB & 1 ) {
		fprintf(stdout,":: -1 slave.\n");
	}
	pthread_exit(NULL);
}
예제 #29
0
void run_daemon (libusb_device_handle* handle)
{
    int sock = socket (PF_INET, SOCK_STREAM, 0);
    int client, yes = 1;
    struct sockaddr_in saddr, caddr;
    socklen_t len;

    if (sock == -1) {
        printf ("Socket creation failed: %d\n", errno);
        return;
    }

    setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));

    memset (&saddr, 0, sizeof (saddr));
    saddr.sin_family = AF_INET;
    saddr.sin_addr.s_addr = INADDR_ANY;
    saddr.sin_port = htons (DAEMON_PORT);

    if (bind (sock, (struct sockaddr*)&saddr, sizeof (saddr)) == -1) {
        printf ("Bind error %d\n", errno);
        close (sock);
        return;
    }

    if (listen (sock, 10) == -1) {
        printf ("Listen error %d\n", errno);
        close (sock);
        return;
    }

    len = sizeof (saddr);
    while ((client = accept (sock, (struct sockaddr*)&caddr, &len)) >= 0) {
        printf ("New connection arrived, fd = %d\n", client);
        handle_client (client, handle);
        close (client);
        len = sizeof (saddr);
        printf ("Connection closed\n");
    }

    close (sock);
}
예제 #30
0
void socket_server()
{
	int32 server_sock = 0, conn_sock = 0;

	if( (server_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	{
		cout << "create socket error." << strerror(server_sock) << endl;
		return;
	}

	struct sockaddr_in server_addr, client_addr;

	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(SERVER_PORT);
	server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

	if(bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0)
	{
		cout << "bind socket error" << endl;
	}

	if(listen(server_sock, MAX_LISTEN) < 0)
	{
		cout << "listen sock error" << endl;
	}

	while(1)
	{
		uint32 client_len = sizeof(client_addr);

		if( (conn_sock = accept(server_sock, (struct sockaddr*)&client_addr, &client_len)) < 0)
		{
			cout << "failed to accept client connection" << endl;
		}

		cout << "Client connected:" << inet_ntoa(client_addr.sin_addr) << "   port:" << client_addr.sin_port  << endl;

		handle_client(conn_sock);
	}

	close(server_sock);
}