goldsborough / ipc-bench

:racehorse: Benchmarks for Inter-Process-Communication Techniques
MIT License
696 stars 108 forks source link

chore: simplify server.c #33

Open matu3ba opened 7 months ago

matu3ba commented 7 months ago

No need to use getaddrinfo dance. This would be enough (feel free to reuse https://github.com/matu3ba/dotfiles/blob/2374f5ab126e8d47f4cc3ae86eb576197c31e8a3/templates/server.c):

  int addr_family;
  //int ip_protocol;
  struct sockaddr_in6 source_addr = {}; // can contain Ipv4 and Ipv6
  memset(&source_addr, 0, sizeof(source_addr));
  //struct in_addr source_addr;
  struct sockaddr_in6 dest_addr = {};
  memset(&dest_addr, 0, sizeof(dest_addr));

  dest_addr.sin6_family = AF_INET6;
  dest_addr.sin6_port = htons(port);
  addr_family = AF_INET6;

  *listenfd = socket(addr_family, SOCK_STREAM, 0);
  if (listenfd < 0) {
    perror("unable to create socket");
    exit(1);
  }
   // 1 - on, 0 - off
  int opt_REUSEADDR = 1;
  int opt_NODELAY = 1;
  int st = setsockopt(*listenfd, SOL_SOCKET, SO_REUSEADDR, &opt_REUSEADDR, sizeof(opt_REUSEADDR));
  if (st < 0) {
    perror("set socket option SO_REUSEADDR failed");
    close(*listenfd);
    exit(1);
  }
  st = setsockopt(*listenfd, IPPROTO_TCP, TCP_NODELAY, &opt_NODELAY, sizeof(opt_NODELAY));
  if (st < 0) {
    perror("set socket option TCP_NODELAY failed");
    close(*listenfd);
    exit(1);
  }
  st = bind(*listenfd, (struct sockaddr*) &dest_addr, sizeof(dest_addr));
  if (st != 0) {
    perror("unable to bind socket");
    close(*listenfd);
    exit(1);
  }
alexandervanrenen commented 7 months ago

Thanks! We will incorperate this in the next iteration 🕺