Пример #1
0
/**
 * This function will initialize the connection and stuff
 */
void init_process() {

	signal(SIGCHLD, SIG_IGN);
	memset(client_udp_ports, 0, sizeof(client_udp_ports));
	//4. Create shared memory area with the child processes.
	// REUSED CODE :- http://www.tldp.org/LDP/lpg/node81.html
	//--START
	key_t key;

	/* Create unique key via call to ftok() */
	key = ftok("./test", 'S');

	if ((shmid = shmget(key, SEGSIZE, IPC_CREAT | 0666)) < 0) {
		perror("shmget");
		exit(1);
	}

	/* Attach (map) the shared memory segment into the current process */
	if ((segptr = (char *) shmat(shmid, NULL, 0)) == (char *) -1) {
		perror("shmat");
		exit(1);
	}

	//--END

	//3. set up TCP server at manager
	tcp_serv_sock_fd = create_tcp_socket();
	populate_sockaddr_in(&client_tcp_server, "localhost", 0);
	if (bind_address(tcp_serv_sock_fd, client_tcp_server) < 0) {
		perror("Error biding the address to socket. Exiting!!");
		exit(0);
	}

	//get the port number information
	socklen_t size = sizeof(tmp);
	if (getsockname(tcp_serv_sock_fd, (struct sockaddr *) &tmp, &size) < 0) {
		perror("Error getting port number information!!");
		exit(0);
	}

	//listen for incomming connections
	listen(tcp_serv_sock_fd, BACKLOG_QUEUE);

	//5. Put manager's port # in shared memory so that child processes and use it to connect the manager (server) socket
	//writeshm(segptr, temp);
	sprintf(segptr, "%u", ntohs(tmp.sin_port));
	printf("init_process: data set in shared memory is: %s\n", segptr);
}
/**
 * Handle HELLO-message.
 *
 * @param cls closure
 * @param client identification of the client
 * @param message the actual message
 */
static void
handle_hello (void *cls, struct GNUNET_SERVER_Client *client,
              const struct GNUNET_MessageHeader *message)
{
  const struct GNUNET_HELLO_Message *hello;
  struct GNUNET_PeerIdentity pid;

  hello = (const struct GNUNET_HELLO_Message *) message;
  if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid))
  {
    GNUNET_break (0);
    GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
    return;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' message received for peer `%4s'\n",
              "HELLO", GNUNET_i2s (&pid));
  bind_address (&pid, hello);
  GNUNET_SERVER_receive_done (client, GNUNET_OK);
}
Пример #3
0
void listen_server_init(char *ip_address, int port)
{
    int sockfd;
    struct sockaddr_in address;

    sockfd = create_socket();

    print_message("Socket created\n");

    set_socket_options(sockfd);
    bind_address(sockfd, ip_address, port);

    print_message("Socket bind to address\n");

    listen(sockfd, MAX_PENDING_CONNECTIONS);

    print_message("Listening...\n");

    handle_clients(sockfd);

    shutdown(sockfd, 2);
}