// (c) 2007 Abdulla Abdurakhmanov

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once


#define WIN32_LEAN_AND_MEAN        // Exclude rarely-used stuff from Windows headers
#include <stdio.h>
#include <time.h>
#include <string>

#ifndef WIN32
#include <sys/types.h>
#include <sys/socket.h>    
#include <signal.h>
#define SOCKET int
#define WORD unsigned int
#define INADDR_NONE (in_addr_t)(-1)
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define WSAECONNRESET -1
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>     
#include <sys/time.h>

#define GetTickCount gethrtime
#else
#include <tchar.h>
#include <windows.h>
#include <winsock2.h>
#endif


// TODO: reference additional headers your program requires here


// EchoServer.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

void DieWithError(const char* err)  
{
#ifdef WIN32
    printf("Err: %s/%d\n",err, WSAGetLastError());
#else
    printf("Err: %s\n",err);
#endif
    
    exit(-1);
}

#define MAXPENDING 5
SOCKET serverSocket;

void createSocketServer()
{
    /* Create socket for incoming connections */
    if ((serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
        DieWithError("socket() failed");

    struct sockaddr_in echoServAddr; /* Local address */
    /* Construct local address structure */
    memset(&echoServAddr, 0, sizeof(echoServAddr));   /* Zero out structure */
    echoServAddr.sin_family = AF_INET;                /* Internet address family */
    echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
    echoServAddr.sin_port = htons( 5555 );                /* Local port */

    /* Bind to the local address */
    if (bind(serverSocket, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
        DieWithError("bind() failed");

    /* Mark the socket so it will listen for incoming connections */
    if (listen(serverSocket, MAXPENDING) < 0)
        DieWithError("listen() failed");
}

void stopSocketServer()
{
#ifdef WIN32
    closesocket(serverSocket);
#else
    close(serverSocket);
#endif
}


void HandleTCPClient(SOCKET socket)
{
    int totalBytesRcv = 0;
    int totalBytesSnt = 0;
    char buf[1024] = {0};
    int bytesRecv = 0;
    int bytesSent = 0;
    while( bytesRecv != SOCKET_ERROR ) {
        bytesRecv = recv( socket, buf, 1024, 0 );
        if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
          printf( "Connection Closed.\n");
          break;
        }
        if(bytesRecv > 0)
            totalBytesRcv += bytesRecv;

        bytesSent = send(socket, buf, bytesRecv, 0);
        if ( bytesSent == 0 || bytesSent == WSAECONNRESET ) {
          printf( "Connection Closed.\n");
          break;
        }
        if(bytesSent > 0)
            totalBytesSnt += bytesSent;
    }
#ifndef WIN32
    close(socket);
#endif

    printf("Total bytes. Sent %d/Rcvd: %d\n", totalBytesSnt, totalBytesRcv);
}

int main(int argc, char* argv[])
{
#ifdef WIN32
    WORD wVersionRequested;
    WSADATA wsaData;
    int err;
    
    wVersionRequested = MAKEWORD( 2, 2 );
    
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) {
        /* Tell the user that we could not find a usable */
        /* WinSock DLL.                                  */
        return - 1;
    }
#else
    signal(SIGPIPE, SIG_IGN);
#endif

    createSocketServer();
    SOCKET clntSock;
    struct sockaddr_in echoClntAddr; /* Local address */
#ifndef WIN32
    socklen_t clntLen = 0;
#else
    int clntLen = 0;
#endif
    for (;;) /* Run forever */
    {
        /* Set the size of the in-out parameter */
        clntLen = sizeof(echoClntAddr);

        /* Wait for a client to connect */
    if ((clntSock = accept(serverSocket, (struct sockaddr *) &echoClntAddr, &clntLen)) < 0)
            DieWithError("accept() failed");

        /* clntSock is connected to a client! */

        printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));

        HandleTCPClient(clntSock);
    }
    return 0;
}