Blame src/components/appio/tests/appio_test_recv.c

Packit Service a1973e
#include <stdio.h>
Packit Service a1973e
#include <string.h>
Packit Service a1973e
#include <stdlib.h> /* exit() */
Packit Service a1973e
#include <errno.h> /* herror() */
Packit Service a1973e
#include <netdb.h> /* gethostbyname() */
Packit Service a1973e
#include <sys/types.h> /* bind() accept() */
Packit Service a1973e
#include <sys/socket.h> /* bind() accept() */
Packit Service a1973e
#include <unistd.h>
Packit Service a1973e
Packit Service a1973e
#include "papi.h"
Packit Service a1973e
#include "papi_test.h"
Packit Service a1973e
Packit Service a1973e
#define PORT 3490
Packit Service a1973e
#define NUM_EVENTS 6
Packit Service a1973e
Packit Service a1973e
main(int argc, char *argv[]) {
Packit Service a1973e
  int Events[NUM_EVENTS]; 
Packit Service a1973e
  const char* names[NUM_EVENTS] = {"RECV_CALLS", "RECV_BYTES", "RECV_USEC", "RECV_ERR", "RECV_INTERRUPTED", "RECV_WOULD_BLOCK"};
Packit Service a1973e
  long long values[NUM_EVENTS];
Packit Service a1973e
Packit Service a1973e
  /* Set TESTS_QUIET variable */
Packit Service a1973e
  tests_quiet( argc, argv );
Packit Service a1973e
Packit Service a1973e
  int version = PAPI_library_init (PAPI_VER_CURRENT);
Packit Service a1973e
  if (version != PAPI_VER_CURRENT) {
Packit Service a1973e
    fprintf(stderr, "PAPI_library_init version mismatch\n");
Packit Service a1973e
    exit(1);
Packit Service a1973e
  }
Packit Service a1973e
Packit Service a1973e
  if (!TESTS_QUIET) printf("This program will listen on port 3490, and write data received to standard output\n");
Packit Service a1973e
  int retval;
Packit Service a1973e
  int e;
Packit Service a1973e
  for (e=0; e
Packit Service a1973e
    retval = PAPI_event_name_to_code((char*)names[e], &Events[e]);
Packit Service a1973e
    if (retval != PAPI_OK) {
Packit Service a1973e
      fprintf(stderr, "Error getting code for %s\n", names[e]);
Packit Service a1973e
      exit(2);
Packit Service a1973e
    } 
Packit Service a1973e
  }
Packit Service a1973e
Packit Service a1973e
  int bytes = 0;
Packit Service a1973e
  char buf[1024];
Packit Service a1973e
Packit Service a1973e
  int sockfd, n_sockfd, sin_size, len;
Packit Service a1973e
  char *host_addr, *recv_msg;
Packit Service a1973e
  struct sockaddr_in my_addr;
Packit Service a1973e
  struct sockaddr_in their_addr;
Packit Service a1973e
  my_addr.sin_family = AF_INET;
Packit Service a1973e
  my_addr.sin_port = htons(PORT);
Packit Service a1973e
  my_addr.sin_addr.s_addr = INADDR_ANY;
Packit Service a1973e
Packit Service a1973e
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
Packit Service a1973e
  if (sockfd < 0) {
Packit Service a1973e
    perror("socket");
Packit Service a1973e
    exit(1);
Packit Service a1973e
  }
Packit Service a1973e
  if ((bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr))) == -1) {
Packit Service a1973e
    perror("bind");
Packit Service a1973e
    exit(1);
Packit Service a1973e
  }
Packit Service a1973e
  listen(sockfd, 10);
Packit Service a1973e
  if ((n_sockfd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
Packit Service a1973e
    perror("accept");
Packit Service a1973e
    exit(1);
Packit Service a1973e
  }
Packit Service a1973e
  close(sockfd);
Packit Service a1973e
Packit Service a1973e
  /* Start counting events */
Packit Service a1973e
  if (PAPI_start_counters(Events, NUM_EVENTS) != PAPI_OK) {
Packit Service a1973e
    fprintf(stderr, "Error in PAPI_start_counters\n");
Packit Service a1973e
    exit(1);
Packit Service a1973e
  }
Packit Service a1973e
Packit Service a1973e
  while ((bytes = recv(n_sockfd, buf, 1024, 0)) > 0) {
Packit Service a1973e
    write(1, buf, bytes);
Packit Service a1973e
  }
Packit Service a1973e
Packit Service a1973e
  close(n_sockfd);
Packit Service a1973e
Packit Service a1973e
  /* Stop counting events */
Packit Service a1973e
  if (PAPI_stop_counters(values, NUM_EVENTS) != PAPI_OK) {
Packit Service a1973e
    fprintf(stderr, "Error in PAPI_stop_counters\n");
Packit Service a1973e
  }
Packit Service a1973e
 
Packit Service a1973e
  if (!TESTS_QUIET) { 
Packit Service a1973e
    printf("----\n");
Packit Service a1973e
    for (e=0; e
Packit Service a1973e
      printf("%s: %lld\n", names[e], values[e]);
Packit Service a1973e
  }
Packit Service a1973e
  test_pass( __FILE__ );
Packit Service a1973e
  return 0;
Packit Service a1973e
}