Snorkel Embedded Webserver with C Language

Snorkel Embedded Web Compile on x64 Linux

Snorkel Library는 32bit 이므로, 필수 라이브러리 설치

yum install glibc.i686 glibc-devel.i686 libzip.i686 libzip-devel.i686

gcc -m32 -lsnorkel32 -lrt -lm -lnsl -lpthread src.c 로 컴파일!!

Snorkel Example

#include <stdio.h>
#include <stdlib.h>
#include <snorkel.h>

void syntax(char *pszProg)
{
    fprintf(stderr, "Syntax Error:\n");
    fprintf(stderr, "%s [-i <index_file_directory>] [-p <port>]\n", pszProg);
    exit(1);
}



void main(int argc, char *argv[])
{
    int i = 1;
    int port = 80;
    char *pszIndex = 0;
    char szExit[10];
    snorkel_obj_t  http = 0;

    for(; i < argc; i++)
    {
        if( argv[i][0] == '-' || argv[i][0] == '/' )
        {
            char carg = argv[i][1];

            switch(carg)
            {
                case 'p':
                    port = atoi(argv[i + 1]);
                    i++;
                    break;
                case 'i':
                    pszIndex = argv[i + 1];
                    i++;
                    break;
                default:
                    syntax(argv[0]);
                    break;
            }
        }
    }

    if( !pszIndex )
        syntax(argv[0]);


    if( snorkel_init() != SNORKEL_SUCCESS )
    {
        perror("Could not Initialize Snorkel\n");
        exit(1);
    }


    http = snorkel_obj_create(snorkel_obj_server, 2, pszIndex);
    if( !http )
    {
        perror("Could not Create HTTP Server\n");
        exit(1);
    }

    if( snorkel_obj_set(http, snorkel_attrib_listener, port, 0) != SNORKEL_SUCCESS )
    {
        fprintf(stderr, "Could not Create Listener\n");
        snorkel_obj_destroy(http);
        exit(1);
    }

    if( snorkel_obj_set(http, snorkel_attrib_ipvers, IPVERS_IPV4, SOCK_SET) != SNORKEL_SUCCESS )
    {
        fprintf(stderr, "Error Could not Set IP Version\n");
        exit(1);
    }


    fprintf(stderr, "\n\n[ H T T P ] Starting Embedded Server\n");
    if( snorkel_obj_start(http) != SNORKEL_SUCCESS )
    {
        perror("Could not Start Server\n");
        snorkel_obj_destroy(http);
        exit(1);
    }

    fprintf(stderr, "\n[ H T T P ] Started!!\n\n" "--Hit Enter to Terminate --\n");
    fgets(szExit, sizeof(szExit), stdin);
    fprintf(stderr, "[ H T T P ] Bye!!\n");


    snorkel_obj_destroy(http);
    exit(0);
}