/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <netdb.h>
#endif

#include <errno.h>

#define PORT 12345
#define BACKLOG 100

void recv_line(int sd, char *buf, int buflen)
{
    int ix;
    ix = 0;
    buf[ix] = '\0';
    while(ix < buflen && strstr(buf, "\r\n") == NULL)
    {
        ix = ix + recv(sd, buf + ix, buflen - ix, 0);
        buf[ix] = '\0';
    }
}

int main()
{
    int sd, sd2, status, buflen, slen;
    long int a, b, c;
    char *buf, *v, *v2;
    struct sockaddr_in local, remote;
#ifdef WIN32
    WSADATA WSAData;
    if(WSAStartup(0x0101, &WSAData) != 0)
    {
        printf("Error initializing\n");
        exit(0);
    }
#endif
    /* create socket */
    sd = socket(AF_INET, SOCK_STREAM, 0);
    if(sd < 0)
    {
        printf("Error creating socket: %s\n", strerror(errno));
        exit(0);
    }
    /* bind */
    local.sin_family = AF_INET;
    local.sin_port = htons(PORT);
    local.sin_addr.s_addr = INADDR_ANY;
    status = bind(sd, (struct sockaddr *)&local ,sizeof(local));
    if(status < 0)
    {
        printf("Error binding socket: %s\n", strerror(errno));
        exit(0);
    }
    /* listen */
    status = listen(sd, BACKLOG);
    if(status < 0)
    {
        printf("Error listening socket: %s\n", strerror(errno));
        exit(0);
    }
    for(;;)
    {
        /* accept */
        sd2 = accept(sd, 0, 0);
        if(sd2 < 0)
        {
            printf("Error accepting socket: %s\n", strerror(errno));
            exit(0);
        }
        slen = sizeof(remote);
        getpeername(sd2, (struct sockaddr *)&remote, &slen);
        printf("(connection from %s)\n", inet_ntoa(remote.sin_addr));
        /* read a and b */
        buflen = 24;
        buf = malloc(buflen);
        recv_line(sd2, buf, buflen);
        sscanf(buf, "%ld %ld\r\n", &a, &b);
        free(buf);
        /* calculate c and send */
        c = a + b;
        buflen = 13;
        buf = malloc(buflen);
        sprintf(buf, "%ld\r\n", c);
        status = send(sd2, buf, strlen(buf), 0);
        if(status < 0)
        {
            printf("Error sending c: %s\n", strerror(errno));
            exit(0);
        }
        free(buf);
        /* read v */
        buflen = 258;
        buf = malloc(buflen);
        recv_line(sd2, buf, buflen);
        v = malloc(strlen(buf) + 1);
        sscanf(buf, "%s\r\n", v);
        free(buf);
        /* calculate v2 and send */
        v2 = malloc(2 * strlen(v) + 1);
        strcpy(v2, v);
        strcat(v2, v);
        buflen = strlen(v2) + 3;
        buf = malloc(buflen);
        sprintf(buf, "%s\r\n", v2);
        status = send(sd2, buf, strlen(buf), 0);
        if(status < 0)
        {
            printf("Error sending v2: %s\n", strerror(errno));
            exit(0);
        }
        free(buf);
        free(v);
        free(v2);
        /* close socket */
#ifdef WIN32
        closesocket(sd2);
#else
        close(sd2);
#endif
    }
    return 0;
}
