8000 chore: simplify server.c · Issue #33 · goldsborough/ipc-bench · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

chore: simplify server.c #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
matu3ba opened this issue Apr 3, 2024 · 1 comment
Open

chore: simplify server.c #33

matu3ba opened this issue Apr 3, 2024 · 1 comment

Comments

@matu3ba
Copy link
matu3ba commented Apr 3, 2024

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
Copy link
Collaborator

Thanks! We will incorperate this in the next iteration 🕺

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0