FredrichP
FredrichP

Reputation: 21

Web server problem in C

I've written a web server in c, and it can get a connection by enter http://localhost:8080/ in the web browser, but how can i go forward in the project so I can get the homepage in the browser?

The only thing which is happening now is that a connection is accepted, and nothing more! If you want to see the code, then say so!

main(){

        WORD wVersionRequested;
        WSADATA wsaData;
        int err, ok, clientAddrLen, serverSocket, clientSocket;
        struct addrinfo *info;
        struct sockaddr clientAddr;


        wVersionRequested = MAKEWORD(2, 2);
        err = WSAStartup(wVersionRequested, &wsaData);


        //Creates a socket with 3 pre-defined values found in the libraries.
        serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        ok = getaddrinfo("127.0.0.1", "8080", NULL, &info);
        if(ok!=0) {
                WCHAR * error = gai_strerror(ok);
                printf("%s\n",error);
        }


        printf("Waiting for connection...\n");


        //Binds the port to the socket.
        ok = bind(serverSocket, info->ai_addr, info->ai_addrlen); 
        if(ok == SOCKET_ERROR) {
                err = WSAGetLastError();
                printf("%d\n",err);
        }
        ok = listen(serverSocket, SOMAXCONN);
        if(ok == SOCKET_ERROR) {
                err = WSAGetLastError();
                printf("%d\n",err);
        }


        //Creates new socket after another IP connects.
        clientAddrLen = sizeof(clientAddr);
        clientSocket = accept(serverSocket, &clientAddr, &clientAddrLen);


        printf("Connected!\n");

        //Sends a message on the new socket.
        sendMessage(clientSocket);
        printf("Message sent.\n");


        //Writes a log for the server.
        writeServerLog("127.0.0.1", " GET /HTTP1.1 C:/Server/index.html", "index.html", 100);


        getchar(); //Waiting for input, Used to stop the server before terminating.


        //Cleanup.
        closesocket(serverSocket);
        closesocket(clientSocket);
        WSACleanup();
}


void sendMessage(int socket){

        string message;


        message = "<html><body>Welcome to the server!</body></html>";
        send(socket, message, strlen(message), 0);
}


void writeServerLog(string ip, string recieved, string response, int sizeOfResponse){

        FILE *log;
        SYSTEMTIME st;


        GetSystemTime(&st);
        log = fopen("serverlog.txt", "a");
        fprintf(log, "IP: %s DaTi: %d-%d-%d %d:%d:%d Recv: %s Resp: %s Size: %d\n", ip, st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, recieved, response, sizeOfResponse);
        fclose(log);
}

Upvotes: 0

Views: 933

Answers (3)

Nelu Cozac
Nelu Cozac

Reputation: 11

When you get request from browser, this request may be

GET /filename.ext HTTP/1.0
Host: ...
User-agent: ...
...

You should search for your filename.ext

If .ext is .htm or .html

Function sendMessage should send

"HTTP/1.0 200 Ok\nConnection: close\nContent-type: text/html\nContent-length: 999999\n\n"

replace 999999 with the actual length of your file

send the content of your file

If .ext is .gif

Function sendMessage should send

"HTTP/1.0 200 Ok\nConnection: close\nContent-type: image/gif\nContent-length: 999999\n\n"

replace 999999 with the actual length of your file

send the content of your file

And so on

Upvotes: 1

BlackBear
BlackBear

Reputation: 22979

Once you have the connection, you simply have to check what file the browser is asking for and send it. For example, when you first connect to a web server your browser is asking something like GET HTTP/1.1 index so the server has to send the main page. It just opens it, reads from it and send()s it back. Then the browser will look into that page and asks again if it finds images/videos or whatever, finally it shows you the page. Give a look here, it will surely help you. :)

Upvotes: 0

rlibby
rlibby

Reputation: 6021

Speak HTTP, or at least fake it.

Upvotes: 3

Related Questions