/*
 * Have you ever been to electric ladyland ?
 */
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>

int main(int argc,char *argv[]) {
  int s;
  struct sockaddr_in a;

  s = socket(PF_INET,SOCK_STREAM,0);
  if (s==-1) {
    fprintf(stderr,"socket problem\n");
    exit(EXIT_FAILURE);
  }
  bzero(&a,sizeof(a));
  a.sin_family = AF_INET;
  a.sin_port = htons(61234);
  a.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(s,(struct sockaddr *)&a,sizeof(a))==-1) {
    fprintf(stderr,"bind problem");
    close(s);
    exit(EXIT_FAILURE);    
  }
  if (listen(s,0)==-1) {
    fprintf(stderr,"listen problem");
    close(s);
    exit(EXIT_FAILURE);
  }
  int d, l, i;
  struct sockaddr_in c;
  char buffer[256];
  int lus;
  do {
    printf("Ready to accept... Please call me!\n");
    if ((d=accept(s,(struct sockaddr *)&c,&l))==-1) {
      fprintf(stderr,"accept problem");
      close(s);
      exit(EXIT_FAILURE);
    }
    shutdown(s,SHUT_WR);
    while ((lus=read(d,buffer,256))>0) {
      for (i=0; i<lus; i++) printf("%2x(%c) ",buffer[i],buffer[i]);
      printf("\n");
    }
    sleep(10); // I'm tired, rest! now!
    close(d);
  } while(lus==0);
  close(s);
  exit(EXIT_SUCCESS);
}