I do c language "httpd" application works without problems at first.

  1. meet user requests from nginx ago
  2. Looking to requests from. Directs incoming requests according to the address 127.0.0.1:8080

But how can I solve the query is loaded when the crash occurred.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <time.h>

#define PORTNO          8080

#define BACKLOG         8
#define HOSTNAME        100
#define BUFSIZE         10000
#define SOCKET_ERROR    -1

int write_socket(int s, const void *pBuf, int n)
{
    int result;
    int index = 0;
    int left = n;

    while (left > 0)
    {
        result = send(s, (const char *) pBuf + index, left, 0);

        if (result == 0)
            return index;

        if (result == SOCKET_ERROR)
            return SOCKET_ERROR;

        index += result;
        left -= result;
    }

    return index;
}


int read_socket(int s, void *pBuf, int n)
{
    int result;
    int index = 0;
    int left = n;

    while (left > 0) {
        result = recv(s, (char *) pBuf + index , left, 0);

        if (result == 0)
            return index;

        if (result == SOCKET_ERROR)
            return SOCKET_ERROR;

        index += result;
        left -= result;

    }
    return index;
}

static void getIpAddress(char *addrp)
{
    struct ifaddrs *iflist, *iface;

        if (getifaddrs(&iflist) < 0) {
            perror("getifaddrs");
            exit(EXIT_FAILURE);
        }

        for (iface = iflist; iface; iface = iface->ifa_next) {
            int af = iface->ifa_addr->sa_family;
            const void *addr;

            switch (af) {
                case AF_INET:
                    addr = &((struct sockaddr_in *)iface->ifa_addr)->sin_addr;
                    break;
                case AF_INET6:
                    addr = &((struct sockaddr_in6 *)iface->ifa_addr)->sin6_addr;
                    break;
                default:
                    addr = NULL;
            }

            if (addr) {
                inet_ntop(af, addr, addrp, 80);
                if (strstr(iface->ifa_name, "wlan") || strstr(iface->ifa_name, "eth"))
                    if (strchr(addrp, '.'))
                        break;
                continue;
            }
        }
        freeifaddrs(iflist);

        if (!strchr(addrp, '.'))
            puts("No Internet Connection");
}

void errorMessage(char *message) 
{
    printf("%s...\n", message);
    getchar();
    exit(EXIT_FAILURE);
}

int main(void) {

    char servername[25];
    int portno = PORTNO;
    struct sockaddr_in servaddr, clientaddr;
    int servfd, clientfd;
    int addr_len;
    int yes = 1;
    char hostname[100];
    char buf[BUFSIZE];
    char request[250];
    int count = 0;

    getIpAddress(servername);

    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(portno);
    memset(&servaddr.sin_zero, '\0', 8);
    servaddr.sin_addr.s_addr = inet_addr(servername);

    gethostname(hostname, 100);

    printf("%s -> %s:%d\n",hostname,servername,PORTNO);


    if ((servfd = socket(AF_INET, SOCK_STREAM, 0)) == SOCKET_ERROR) {
        perror("socket");
        exit(EXIT_FAILURE);
    }

    if (setsockopt(servfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof (int)) == SOCKET_ERROR) {
        perror("setsockopt");
        exit(EXIT_FAILURE);
    }

    if (bind(servfd, (struct sockaddr *) &servaddr, sizeof (servaddr)) == SOCKET_ERROR) {
        perror("bind");
        exit(EXIT_FAILURE);
    }

    if (listen(servfd, BACKLOG) == SOCKET_ERROR) {
        perror("listen");
        exit(EXIT_FAILURE);
    }

    while(1)
    {
        addr_len = sizeof(clientaddr);

        if ((clientfd = accept(servfd, (struct sockaddr *) &clientaddr, &addr_len)) == SOCKET_ERROR)
        {
            perror("accept");
            exit(EXIT_FAILURE);
        }

        sprintf(request,"HTTP/1.0 200 OK\nContent-Type: text/html; charset=utf-8\n\n");

        if ((write_socket(clientfd,request,strlen(request)) == SOCKET_ERROR))
        {
                puts("Gonderilemedi...");
                exit(EXIT_FAILURE);
        }

        char buf[] = "<center><br><br>Hello World Wide Web</center>";

        if ((write_socket(clientfd, buf, strlen(buf)) == SOCKET_ERROR)) {
                puts("Gonderilemedi...");           
                exit(EXIT_FAILURE);
        }

        printf("The number of requests from: %d", ++count);
        fflush(stdout);
        shutdown(clientfd,2);
    }

    close(servfd);
    return 0;
}

Recommended Answers

All 2 Replies

Please prove additional information regarding your question or issues/errors you encountered.

And in this way the final version of my code. When trying to compile and run it in the house. However, now it says there is no connection to the internet

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <time.h>

#define PORTNO          8080

#define BACKLOG         8
#define HOSTNAME        100
#define BUFSIZE         10000
#define SOCKET_ERROR    -1


int write_socket(int s, const void *pBuf, int n);
int read_socket(int s, void *pBuf, int n);
static void getIpAddress(char *addrp);
void errorMessage(char *message);

int main(void) {

    char servername[25];
    int portno = PORTNO;
    struct sockaddr_in servaddr, clientaddr;
    int servfd, clientfd;
    int addr_len;
    int yes = 1;
    char hostname[100];
    char buf[BUFSIZE];
    char request[250];
    int count = 0;

    getIpAddress(servername);

    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(portno);
    memset(&servaddr.sin_zero, '\0', 8);
    servaddr.sin_addr.s_addr = inet_addr(servername);

    gethostname(hostname, 100);

    printf("%s (%s:%d)", hostname, servername, PORTNO);


    if ((servfd = socket(AF_INET, SOCK_STREAM, 0)) == SOCKET_ERROR) {
        perror("socket");
        exit(EXIT_FAILURE);
    }

    if (setsockopt(servfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof (int)) == SOCKET_ERROR) {
        perror("setsockopt");
        exit(EXIT_FAILURE);
    }

    if (bind(servfd, (struct sockaddr *) &servaddr, sizeof (servaddr)) == SOCKET_ERROR) {
        perror("bind");
        exit(EXIT_FAILURE);
    }

    if (listen(servfd, BACKLOG) == SOCKET_ERROR) {
        perror("listen");
        exit(EXIT_FAILURE);
    }

    while (1) {
        addr_len = sizeof (clientaddr);
        if ((clientfd = accept(servfd, (struct sockaddr *) &clientaddr, &addr_len)) == SOCKET_ERROR) {
            perror("accept");
            exit(EXIT_FAILURE);
        }

        sprintf(request, "HTTP/1.0 200 OK\nContent-Type: text/html; charset=utf-8\n\n");

        if ((write_socket(clientfd, request, strlen(request)) == SOCKET_ERROR)) {
                puts("Gonderilemedi...");
                exit(EXIT_FAILURE);
        }

        char buf[] = "<center><br><br>Hello World Wide Web</center>";

        if ((write_socket(clientfd, buf, strlen(buf)) == SOCKET_ERROR)) {
                puts("Gonderilemedi...");           
                exit(EXIT_FAILURE);
        }

        printf("%d OK...\r", ++count);
        fflush(stdout);
        shutdown(clientfd, 2);
    }

    close(servfd);
    return 0;
}

int write_socket(int s, const void *pBuf, int n) {
    int result;
    int index = 0;
    int left = n;

    while (left > 0) {
        result = send(s, (const char *) pBuf + index, left, 0);

        if (result == 0)
            return index;

        if (result == SOCKET_ERROR)
            return SOCKET_ERROR;

        index += result;
        left -= result;
    }

    return index;
}


int read_socket(int s, void *pBuf, int n)
{
    int result;
    int index = 0;
    int left = n;

    while (left > 0) {
        result = recv(s, (char *) pBuf + index , left, 0);

        if (result == 0)
            return index;

        if (result == SOCKET_ERROR)
            return SOCKET_ERROR;

        index += result;
        left -= result;

    }
    return index;
}

static void getIpAddress(char *addrp)
{
    struct ifaddrs *iflist, *iface;

        if (getifaddrs(&iflist) < 0) {
            perror("getifaddrs");
            exit(EXIT_FAILURE);
        }

        for (iface = iflist; iface; iface = iface->ifa_next) {
            int af = iface->ifa_addr->sa_family;
            const void *addr;

            switch (af) {
                case AF_INET:
                    addr = &((struct sockaddr_in *)iface->ifa_addr)->sin_addr;
                    break;
                case AF_INET6:
                    addr = &((struct sockaddr_in6 *)iface->ifa_addr)->sin6_addr;
                    break;
                default:
                    addr = NULL;
            }

            if (addr) {
                inet_ntop(af, addr, addrp, 80);
                if (strstr(iface->ifa_name, "wlan") || strstr(iface->ifa_name, "eth"))
                    if (strchr(addrp, '.'))
                        break;
                continue;
            }
        }
        freeifaddrs(iflist);

        if (!strchr(addrp, '.'))
            puts("No connection to the internet");
}

void errorMessage(char *message) 
{
    printf("%s...\n", message);
    getchar();
    exit(EXIT_FAILURE);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.