0

At this moment I'm working on my programming project and I need a server-client protocol, created using C, on which:

  1. The server is just listening to requests from its clients, it just reacts once it receives a request.
  2. The client places the request by using a web browser.
  3. In the URL field (in the browser) the user has to type "get" and then, the name of the desired file.
  4. The server sends the file as a http formatted string.

Thank you!

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


int main(int argc, char** argv) {

    struct servent* port;

    port = getservbyname("cpp_java","tcp");

    printf("Port: %i",port->s_port);

    int descriptor;

    descriptor = socket (AF_INET,SOCK_STREAM,0);

    if(descriptor == -1){
        printf("Error\n");
    }

    struct sockaddr_in direc;
    direc.sin_family = AF_INET;
    direc.sin_port = port->s_port;
    direc.sin_addr.s_addr = INADDR_ANY;

    if(bind(descriptor,(struct sockaddr *)&direc, sizeof(direc)) == -1){
        printf("Error\n");
    }

    if(listen(descriptor,1) == -1){
        printf("Error\n");
    }

    struct sockaddr client;
    int descriptor_client;
    int longi;

    descriptor_client = accept (descriptor, &client, &longi);

    if(descriptor_client == -1){
        printf("Error\n");
    }

    return (EXIT_SUCCESS);
}

This is the code I have done until now, I have read many questions and answers but I don't know how to adapt the HTTP header for binary files and download it using any browser. Thanks in advance.

Note: If you can tell me how can I request a resource using GET, I'd appreciate it, too.

1 Answer 1

0

It seems to me that you are trying to create a simple http server.

  • Start a server. Open a socket, bind it to an interface (say localhost), assign a port to it (say 8080) and start listening. The code you posted seems to do that, you need to implement a function to be executed when a client opens a connection. Here is an example.

    If you are using linux, you can start such a server on localhost (for testing) using netcat -l -p 8080

  • After that you will be able to send http requests using a browser by typing http://localhost:8080/. If i'm correct, this URL will send a http GET request for "/" resource.

  • You can send GET requests from browser for a file resource using http://localhost:8080/filename. The data sent to your server looks like this:

    GET /filename HTTP/1.1 Host: localhost:23000 Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36 Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8

    Notice the GET /filename

  • On accept connection, you will read the http request and parse it. I suggest using any http parser library for c, or, if you have a simple use-case, parse manually for the GET /filename text. Obtain the filename, then create a http response (with file content embedded in the page) and write it back to the socket. The browser will receive it and display the data. The response could look like this

    <!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN"> <HTML> <HEAD> <TITLE> File requested </TITLE> </HEAD> <BODY> <P>Raw file content goes here</P> </BODY> </HTML>

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much. Indeed, I am trying to make a server. Your answer is pretty much what I was looking for, say 99%. One last question about it, that little web page code I send back what will do if a request is a file the browser has no idea what to do with it? Will it prompt Download file?
You can embed the file content in the response as I did above. You can alter the http response to make it send a file, or prompt a download file etc, see stackoverflow.com/questions/5496200/…
Hey, could you help me on how I get the data sent to my server? I don know how to get the lines like those you wrote before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.