int main(int argc, char *argv[])
{
     int sockfd, newsockfd, portno;
     socklen_t clilen;
     char buffer[256];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);
     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
     listen(sockfd,5);
     clilen = sizeof(cli_addr);
     while (1) {
         newsockfd = accept(sockfd, 
                     (struct sockaddr *) &cli_addr, 
                     &clilen);
         if (newsockfd < 0) 
              printf("ERROR on accept");
         connection_handler(newsockfd);
     };
     close(sockfd);
     return 0; 
}
Exemplo n.º 2
0
int main(void)
{
    struct sockaddr_un address;
    int socket_fd, connection_fd;
    socklen_t address_length;
    pid_t child;

    /* Create a socket of local interprocess communication, CO stream type */
    socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
    if(socket_fd < 0)
    {
        printf("socket() failed\n");
        return 1;
    }

    /* delete any left over socket */
    unlink("./demo_socket");

    /* start with a clean address structure */
    memset(&address, 0, sizeof(struct sockaddr_un));

    /*Setup the path in the data structure */
    address.sun_family = AF_UNIX;
    snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");

    if(bind(socket_fd,
            (struct sockaddr *) &address,
            sizeof(struct sockaddr_un)) != 0)
    {
        printf("bind() failed\n");
        return 1;
    }

    if(listen(socket_fd, 5) != 0)
    {
        printf("listen() failed\n");
        return 1;
    }

    while((connection_fd = accept(socket_fd,
                                  (struct sockaddr *) &address,
                                  &address_length)) > -1)
    {
        child = fork();
        if(child == 0)
        {
            /* now inside newly created connection handling process */
            return connection_handler(connection_fd);
        }

        /* still inside server process */
        close(connection_fd);
    }

    close(socket_fd);
    unlink("./demo_socket");
    return 0;
}
Exemplo n.º 3
0
/* Main loop for thread in thread pool
 */
static void thread_wait_loop(t_thread_pool *self) {
	t_session *session;
	t_thread_pool *prev;

	do {
		if (self->session == NULL) {
			pthread_mutex_lock(&thread_pool_mutex);

			waiting_workers++;
			if (pthread_cond_wait(&thread_pool_cond, &thread_pool_mutex) != 0) {
				waiting_workers--;
				session = NULL;
			} else {
				session = last_session(session_list);
			}

			pthread_mutex_unlock(&thread_pool_mutex);
		} else {
			session = self->session;
			self->session = NULL;
		}

		if (session != NULL) {
			if (add_client(session) == 0) {
				connection_handler(session);
			} else {
				close(session->client_socket);
				free(session);
			}
		}
	} while (self->quit == false);

	/* Remove thread record from pool
	 */
	pthread_mutex_lock(&thread_pool_mutex);

	if (thread_pool == self) {
		thread_pool = thread_pool->next;
	} else {
		prev = thread_pool;
		while (prev != NULL) {
			if (prev->next == self) {
				prev->next = self->next;
				break;
			}
			prev = prev->next;
		}
	}
	thread_pool_size--;

	pthread_mutex_unlock(&thread_pool_mutex);

	free(self);

	pthread_exit(NULL);
}
Exemplo n.º 4
0
void listener_dispatch(pn_handler_t *h, pn_event_t *event, pn_event_type_t type)
{
  global_context_t *gc = global_context(h);
  if (type == PN_REACTOR_QUIESCED)
    gc->quiesce_count++;
  else
    gc->quiesce_count = 0;

  switch (type) {
  case PN_CONNECTION_INIT:
    {
      pn_connection_t *connection = pn_event_connection(event);

      // New incoming connection on listener socket.  Give each a separate handler.
      pn_handler_t *ch = connection_handler(gc);
      pn_handshaker_t *handshaker = pn_handshaker();
      pn_handler_add(ch, handshaker);
      pn_decref(handshaker);
      pn_record_t *record = pn_connection_attachments(connection);
      pn_record_set_handler(record, ch);
      pn_decref(ch);
    }
    break;
  case PN_REACTOR_QUIESCED:
    {
      // Two quiesce in a row means we have been idle for a timout period
      if (gc->opts->timeout != -1 && gc->quiesce_count > 1)
        global_shutdown(gc);
    }
    break;
  case PN_REACTOR_INIT:
    {
      pn_reactor_t *reactor = pn_event_reactor(event);
      start_listener(gc, reactor);

      // hack to let test scripts know when the receivers are ready (so
      // that the senders may be started)
      if (gc->opts->ready_text) {
        fprintf(stdout, "%s\n", gc->opts->ready_text);
        fflush(stdout);
      }
      if (gc->opts->timeout != -1)
        pn_reactor_set_timeout(pn_event_reactor(event), gc->opts->timeout);
    }
    break;
  case PN_REACTOR_FINAL:
    {
      if (gc->received == 0) statistics_start(gc->stats);
      statistics_report(gc->stats, gc->sent, gc->received);
    }
    break;
  default:
    break;
  }
}
Exemplo n.º 5
0
void 
listener_dispatch ( pn_handler_t *h, pn_event_t * event, pn_event_type_t type )
{
  global_context_t * gc = global_context ( h );
  if ( type == PN_REACTOR_QUIESCED )
    gc->quiesce_count++;
  else
    gc->quiesce_count = 0;

  switch (type) 
  {
    case PN_CONNECTION_INIT:
      {
        pn_connection_t * connection = pn_event_connection ( event );

        // New incoming connection on listener socket.  Give each a separate handler.
        pn_handler_t *ch = connection_handler(gc);
        pn_handshaker_t *handshaker = pn_handshaker();
        pn_handler_add(ch, handshaker);
        pn_decref(handshaker);
        pn_record_t *record = pn_connection_attachments(connection);
        pn_record_set_handler(record, ch);
        pn_decref(ch);
      }
      break;

    case PN_REACTOR_INIT:
      {
        pn_reactor_t *reactor = pn_event_reactor(event);
        start_listener(gc, reactor);
      }
      break;

    case PN_REACTOR_FINAL:
      {
        if (gc->received == 0) 
          statistics_start(gc->stats);

        //statistics_report(gc->stats, gc->sent, gc->received);
        fclose ( gc->report_fp );

        if ( gc->received > 0 )
          fprintf ( stderr, "reactor-recv received %d messages.\n", gc->received );
      }
      break;

    default:
      break;
  }
}
int main(void)
{
	struct sockaddr_un address;
	int socket_fd, connection_fd;
	socklen_t address_length;
	pid_t child; 
	socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
	if(socket_fd < 0)
	{
		printf("socket() failed\n");
		return 1;
	} 
	unlink("./demo_socket");
	/* start with a clean address structure */
	memset(&address, 0, sizeof(struct sockaddr_un));
	address.sun_family = AF_UNIX;
	snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
	if(bind(socket_fd, 
				(struct sockaddr *) &address, 
				sizeof(struct sockaddr_un)) != 0)
	{
		printf("bind() failed\n");
		return 1;
	}
	if(listen(socket_fd, 5) != 0)
	{
		printf("listen() failed\n");
		return 1;
	}
	while(1)
	{
		connection_fd = accept(socket_fd,(struct sockaddr *) &address,&address_length);
		if (connection_fd< 0) 
			error("ERROR on accept");
		child = fork();
		if (child < 0)
			error("ERROR on fork");
		if (child == 0)  {
			/* now inside newly created connection handling process */
		printf("inside child closing socket\n");
			return connection_handler(connection_fd);
			exit(0);
		}
		else 
		{
			close(connection_fd);
		}
	} /* end of while */
}
Exemplo n.º 7
0
int main(int argc, char ** argv) {
	if (argc != 2) {
		printf("Usage: server pathname\n");
		return 0;
	}
	int listenfd = server_listen(argv[1]);
	
    int connection_fd;
	struct sockaddr_un un;
	socklen_t len = sizeof(un);
	while (1) {
		connection_fd = accept(listenfd, (struct sockaddr*)&un, &len);
		if (fork() == 0) {
			return connection_handler(connection_fd);
		}
		close(connection_fd);
	}

	close(listenfd);
	unlink(argv[1]);
	return 0;
}
Exemplo n.º 8
0
int main(void) {	//this code emulates the RSI process...

	puts("RSI-BLE POC 1...");
	led_setValue(GREEN, 0);
	led_setValue(RED, 0);
	led_setValue(BLUE, 0);

	int rsi_fd = rsiOpen();
	int reqLength, respLength, count, status;
	int running = 1;


	char rsiRequestFrame[RSI_MAX_FRAME_SIZE];
	char rsiResponseFrame[RSI_MAX_FRAME_SIZE];

	// ======================= SET UP SOCKET ==========================
	// socket will be ./demo_socket
	// socket vars
	struct sockaddr_un address;
	int socket_fd, connection_fd, writeToLeet;
	socklen_t address_length;
	pid_t child;
	address_length = 0;
	writeToLeet = 1;
	int counter = 0;
	char ctrString[ 4 ];

	// ignore the SIGCHLD signal to prevent zombie processes
	signal( SIGCHLD, SIG_IGN );

	printf( "Starting socket...\n" );

	socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
	if(socket_fd < 0)
	{
	  printf( "socket() failed\n" );
	  return 1;
	}

	unlink("./demo_socket");

	/* start with a clean address structure */
	memset(&address, 0, sizeof(struct sockaddr_un));

	address.sun_family = AF_UNIX;
	snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");

	if(bind(socket_fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)) != 0)
	{
		printf( "bind() failed\n" );
		return 1;
	}

	if(listen(socket_fd, 5) != 0)
	{
		printf( "listen() failed\n" );
		return 1;
	}

	// ======================= MAIN LOOP ==========================
	while (running) {	//emulate RSI process

		//---- listen for RS485 command from ACP (master)
		memset(rsiRequestFrame,  0, sizeof(rsiRequestFrame));
		memset(rsiResponseFrame, 0, sizeof(rsiResponseFrame));
		reqLength = rsiRead(rsi_fd, rsiRequestFrame);

		//process the command, generate immediate response
		status = rsiHandler(rsiRequestFrame, rsiResponseFrame, &reqLength);

		//send immediate response back to ACP
		if (status == STATUS_SUCCESS) {
			//printf("need to send %d bytes back...\n", reqLength);
			rsiWrite(rsi_fd, rsiResponseFrame, reqLength);

			// =================== SOCKET HANDLING ========================
			// send/receive socket messages
			// NOTE: accept() is blocking in this usage
			connection_fd = accept( socket_fd, (struct sockaddr *) &address, &address_length );

			// accept() returns a nonnegative integer that is a descriptor for the accepted socket
			if( connection_fd != -1)
			{
				child = fork();
				if(child == 0)
				{
					// always send the 0x01 command for now
					return connection_handler(connection_fd, 1, writeToLeet);

				} else {
					close(connection_fd);
				} // if child
			} else {
				printf( "Error accepting message.\n" );
				perror( "accept" );
			} // if connection_fd

			/*
			if( writeToLeet == 1) {
				writeToLeet = 0;
			} else {
				writeToLeet = 1;
			} // if writeToLeet
			*/
		} // if status == STATUS_SUCCESS

		//are we done? set the flag...
		//running = 0;




	}	//while running
	printf( "Closing socket...\n" );
	close(socket_fd);
	unlink("./demo_socket");
	printf("done...\n");

	//----- close the UART, release the GPIOs, etc -----
	rsiClose(rsi_fd);
	return 0;
}
Exemplo n.º 9
0
int main()
{
	int sockfd, pid;
	//struct sockaddr_in dest;
  struct sockaddr_in6 dest;
  //
  char client_addr_ipv6[100];
	char start[20] = "Server start.\n";

	printf("%s", start);

	/* create socket */
	//sockfd = socket(PF_INET, SOCK_STREAM, 0);
  sockfd = socket(AF_INET6, SOCK_STREAM, 0);

	/* initialize structure dest */
	bzero(&dest, sizeof(dest));
  //
  dest.sin6_flowinfo = 0;
	//dest.sin_family = AF_INET;
  dest.sin6_family = AF_INET6;
	//dest.sin_port = htons(8888);
  dest.sin6_port = htons(8888);
	/* this line is different from client */
	//dest.sin_addr.s_addr = INADDR_ANY;
  dest.sin6_addr = in6addr_any;

	/* Assign a port number to socket */
	bind(sockfd, (struct sockaddr*)&dest, sizeof(dest));

	/* make it listen to socket with max 10 connections */
	listen(sockfd, 20);

	/* infinity loop -- accepting connection from client forever */
	while(1)
	{
    int clientfd;
		//struct sockaddr_in client_addr;
    struct sockaddr_in6 client_addr;
		int addrlen = sizeof(client_addr);

    clientfd = accept(sockfd, (struct sockaddr *) &client_addr, &addrlen);

    //
    inet_ntop(AF_INET6, &(client_addr.sin6_addr), client_addr_ipv6, 100);
    printf("Incoming connection from client having IPv6 address: %s\n", client_addr_ipv6);

    if (clientfd < 0) {
      perror("ERROR on accept");
      exit(1);
    }

    /* Create child process */
    pid = fork();

    if (pid < 0) {
      perror("ERROR on fork");
      exit(1);
    }

    if (pid == 0) {
      /* This is the client process */
      close(sockfd);
      connection_handler(clientfd,pid);
      printf("%d",pid);
      printf("%s","\n");
      exit(0);
    } else {
      close(clientfd);
    }
	}

	/* close(server) , but never get here because of the loop */
	close(sockfd);
	return 0;
}