echod_async.c

Cygwinでは動かず。非同期だからといってIOが多重化されるわけではない…と。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/file.h>
#include <signal.h>
#include <errno.h>

#define ECHO_PORT 7
#define MAX_BACKLOG 5
#define RCVBUFSIZE 256
#define MAX_EVENTS 1024
#define EPOLL_TIMEOUT 1000

#define die(s) do { fprintf(stderr, "%s\n", (s)); exit(1); } while(0)
#define die_with_err(s) do { perror((s)); exit(1); } while(0)

#ifdef DEBUG
#define debug(...) do { fprintf(stderr,  __VA_ARGS__); } while(0)
#else
#define debug(...) do {} while(0)
#endif

int sock;

void echo(int s) {
  char buf[RCVBUFSIZE + 1];
  size_t len;

  while(1) {
    if ((len = recv(s, buf, RCVBUFSIZE, 0)) < 0) {
      die_with_err("recv(2)");
    }

    if (len == 0) { break; }

    buf[len] = '\0';
    debug("recv: %s", buf);

    if (send(s, buf, len, 0) != len) {
      die_with_err("send(2)");
    }
  }

  close(s);
}

void sigio_handler(int signal) {
  struct sockaddr_in caddr;
  int s, len = sizeof(caddr);

  if ((s = accept(sock, (struct sockaddr*) &caddr, &len)) < 0) {
    die_with_err("accept(2)");
  }

  debug("accepted.\n");
  echo(s);
}

int main() {
  struct sockaddr_in addr;
  struct sigaction handler;

  if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    die_with_err("socket(2)");
  }

  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  addr.sin_port = htons(ECHO_PORT);

  if (bind(sock, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
    die_with_err("bind(2)");
  }

  if (listen(sock, MAX_BACKLOG) < 0) {
    die_with_err("listen(2)");
  }

  memset(&handler, 0, sizeof(handler));
  handler.sa_handler = sigio_handler;
  if (sigfillset(&handler.sa_mask) < 0) {
    die_with_err("sigfillset(3)");
  }
  handler.sa_flags = 0;

  if (sigaction(SIGIO, &handler, 0) < 0) {
    die_with_err("sigaction(3)");
  }

  if (fcntl(sock, F_SETOWN, getpid()) < 0) {
    die_with_err("fcntl(2)");
  }

  if (fcntl(sock, F_SETFL, FASYNC) < 0) {
    die_with_err("fcntl(2)");
  }

  while(1) {
    puts("Zzz...");
    sleep(3);
  }

  close(sock);
}