At this moment I'm working on my programming project and I need a server-client protocol, created using C, on which:
- The server is just listening to requests from its clients, it just reacts once it receives a request.
- The client places the request by using a web browser.
- In the URL field (in the browser) the user has to type "get" and then, the name of the desired file.
- 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.