#include <stdio.h>
#include "serv.h"
#include "inet.h"


#define SBUFLEN 2000

static char     sbuf[2000];

int sock;
#define terminator(str) (!strncmp(str,EOM,EOM_LEN))

net_init()
{
  int ssock;
        ssock = inet_establish_connection(SERVER, PORT, 0);
        if (ssock == -1)
            return 0;
        sock = ssock;
  get_msg();
        printf("[%s]\n",sbuf );
if (!sock){
  printf("Could not connect to server. Call %s for help.\n",HELP_PHONE);
  sleep(10);
  exit(0);
}
        return ssock;}


get_msg()
{

        int     rc,len = 0;

        do {
                rc = read(sock, &sbuf[len], SBUFLEN - len);
          if (rc < 0 )
            perror("in read");
                len = rc + len;
		printf("red = %d\n",rc);
        } while (len <= SBUFLEN && !terminator(&sbuf[len - EOM_LEN]));
        len -= EOM_LEN;
        if (sbuf[len - 1] == LF)
                len--;
        if (sbuf[len - 1] == CR)
                len--;
        sbuf[len] = '\0';
        return;
}


main()
{
char buf[300];
net_init();

for (;;) {
   gets(buf);
   write(sock,buf,strlen(buf));
   write(sock,"\n",1);
    get_msg();
   printf("<%s>\n",sbuf);

 }
}
