/****************************************************************************
 *  simpleClient.c
 *  Copyright (C) 2006-2007
 *  Junxing Zhang
 *
 ****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> 
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h> 
#include <time.h>
#include <sys/time.h>
#include <fcntl.h>
#include <netdb.h>
#include <math.h>
#include <pcap.h>
#include <netinet/if_ether.h> 
#include <net/ethernet.h>
#include <netinet/ether.h> 
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>

#define MAX_PAYLOAD_SIZE 1448

int main(int argc, char * argv[])
{
  char buffer[MAX_PAYLOAD_SIZE] = {0};
  int connection = -1;
  int error = 0;
  struct sockaddr_in address;
  int client = -1, count;
  int address_size = sizeof(address);

  if (argc != 2)
  {
    printf("Usage: %s <port-number>\n", argv[0]);
    return 1;
  }

  connection = socket(AF_INET, SOCK_STREAM, 0);
  if (connection == -1)
  {
    printf("Failed socket()\n");
    return 1;
  }

  address.sin_family = AF_INET;
  address.sin_port = htons(atoi(argv[1]));
  address.sin_addr.s_addr = htonl(INADDR_ANY);
  memset(address.sin_zero, '\0', 8);

  error = bind(connection, (struct sockaddr *)&address, sizeof(address));
  if (error == -1)
  {
    printf("Failed bind()\n");
    return 1;
  }

  error = listen(connection, 1);
  if (error == -1)
  {
    printf("Failed listen()\n");
    return 1;
  }

  client = accept(connection, (struct sockaddr *)&address, &address_size);
  if (client == -1)
  {
    printf("Failed accept()\n");
    return 1;
  }
  while (1)
  {
    count = recv(client, buffer, MAX_PAYLOAD_SIZE, 0);
    if (count>0) printf("Received %d bytes\n", count);
  }

  return 0;
}

