echod.c

#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 <errno.h>

#define ECHO_PORT 7
#define MAX_BACKLOG 5
#define RCVBUFSIZE 256

#define die(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 echo(int sock) {
  debug("echo() bgein.\n");

  while (1) {
    char buf[RCVBUFSIZE + 1];
    size_t len;

    if ((len = recv(sock, buf, RCVBUFSIZE, 0)) < 0) {
      die("recv(2)");
    }

    if (len == 0) { break; }

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

    if (send(sock, buf, len, 0) < 0) {
      die("send(2)");
    }
  }

  close(sock);
  debug("echo() end.\n");
}

int main() {
  int sock;
  struct sockaddr_in addr;

  if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
    die("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("bind(2)");
  }

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

  while (1) {
    int s;
    struct sockaddr_in sa;
    socklen_t len = sizeof(sa);

    if ((s = accept(sock, (struct sockaddr*) &sa, &len)) < 0) {
      if (EINTR == errno) { continue; }
      die("accept(2)");
    }

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

  close(sock);
}