// SNL Server Demo (echo server)

#include <snl.h>
#include <stdio.h>

SNL_READ_CALLBACK(read_callback) {
   // just send the buffer content back
   snl_write(fd, buf, len);
}

SNL_LISTEN_CALLBACK(listen_callback) {
   // start a new receiving thread
   snl_read(fd, read_callback, NULL);
}

int
main(int argc, char **argv) {
   if (snl_listen(NULL, 3000, listen_callback, NULL)) {
      printf("can't connect to socket\n"); exit(1);
   }
   while (1) sleep(1);
   return (0);
}

// SNL Client Demo (echo client)

#include <snl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

static int quit = 0;

SNL_READ_CALLBACK(read_callback) {
   int i;

   for (i=0; i<len; i++) printf("%c", ((char *)buf)[i]); printf("\n");
   quit = 1;
}

int
main(int argc, char **argv) {
   int server;

   if (argc < 2) { printf("USAGE: client <text>\n"); exit(1); }
   if ((server = snl_connect("localhost", 3000)) > 0) {
      snl_read(server, read_callback, NULL);
      snl_write(server, argv[1], strlen(argv[1]));
      while (!quit) usleep(1); // wait for server response
   } else {
      printf("can't connect to server\n"); exit(1);
   }
   return (0);
}