//troy Rosin

#include <SDL2/SDL_net.h>
#include <SDL2/SDL.h>
#include <string.h>
#include <stdio.h>
#include <mydebug.h>
#include <net.h>



char buff[PACKSZ];
char pack[PACKSZ], pack2[PACKSZ];



int main(void)
{
  
  IPaddress *ip, _ip, *cip;
  TCPsocket sock, csock;
  ip = &_ip;

  Uint16 c_port;
  Uint32 c_ip;

  strcpy(pack, "testing Yolo fambro!");
  
  SDL_Init(0);

  SDLNet_Init();
 
  if(SDLNet_ResolveHost(ip, NULL, PORT) == -1)
  {
    printf("SDLNet_resolvehost error: %s\n", SDLNet_GetError());
    exit(1);
  }  

  sock = SDLNet_TCP_Open(ip);
  if(!sock)
  {
    printf("SDL_Net_TCP_Open error: %s\n", SDLNet_GetError());
    exit(1);
  }
  
  int flag = 1;
  int ret = 0;
  while(flag)
  {
    csock = SDLNet_TCP_Accept(sock);
    if(!csock)
    {
      //printf("TCP Accept error: %s\n", SDLNet_GetError());
    }
    else {
      flag = 0;
      cip = SDLNet_TCP_GetPeerAddress(csock);
      if(!cip)
      {
        printf("error getting client ip addr: %s\n", SDLNet_GetError());
      }
      else
      {
        puts("we got a new connection!");
        printf("sending: [%s]\n", pack);
        write_to_buff(buff, pack, PACKSZ);    
        ret = SDLNet_TCP_Send(csock, buff, PACKSZ);
        if(ret < PACKSZ)
        {
          printf("TCP Send error: %s\n", SDLNet_GetError());
        }
        read_to_buff(pack2, buff, PACKSZ);
        printf("sent: [%s]\n", pack2);
      }
    }
  }

  SDLNet_TCP_Close(sock);
  SDLNet_Quit();
  SDL_Quit();

  puts("donzo");


  return 0;
}




