http在Linux(C)中使用套接字发送图像文件(png,jpg)

时间:2019-06-15 19:02:28

标签: c linux image sockets http

我正在尝试创建一个网络服务器,以将png或jpg或html文件发送到浏览器(Firefox)。因此,我执行了已编译的文件,打开浏览器,然后输入要发送的文件,例如“ http://localhost:9191/a.html”,然后该文件显示在浏览器中。

但是当我尝试发送png或jpg文件时,它不起作用! 只是说'图像“ http://localhost:9191/dog.png”因为包含错误而无法显示。”

我不知道为什么它不起作用 所以我认为我应该在send_data函数中做些什么

有人可以给我一些建议吗?

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <pthread.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/stat.h>


#define BUF_SIZE 1024
#define SMALL_BUF 100
#define DATA_SIZE 100000

void* request_handler(void* arg);
void send_data(FILE* fp, char* ct, char* file_name);
char* content_type(char* file);
void send_error(FILE* fp);
void error_handling(char* message);
void read_childproc(int sig);

int main(int argc, char *argv[])
{
    int serv_sock, clnt_sock;
    struct sockaddr_in serv_adr, clnt_adr;
    int clnt_adr_size;
    char buf[BUF_SIZE];
    int state;

    pid_t pid;
    struct sigaction act;

    if (argc != 2) {
        printf("Usage : %s <port>\n", argv[0]);
        exit(1);
    }

    act.sa_handler = read_childproc;
    sigemptyset(&act.sa_mask);
    act.sa_flags = 0;
    state = sigaction(SIGCHLD, &act, 0);

    serv_sock = socket(PF_INET, SOCK_STREAM, 0);
    memset(&serv_adr, 0, sizeof(serv_adr));
    serv_adr.sin_family = AF_INET;
    serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_adr.sin_port = htons(atoi(argv[1]));
    if (bind(serv_sock, (struct sockaddr*)&serv_adr, sizeof(serv_adr)) == -1)
        error_handling("bind() error");
    if (listen(serv_sock, 20) == -1)
        error_handling("listen() error");

    while (1)
    {
        clnt_adr_size = sizeof(clnt_adr);
        clnt_sock = accept(serv_sock, (struct sockaddr*)&clnt_adr, &clnt_adr_size);
        if (clnt_sock == -1)
            continue;
        else {
            printf("Connection Request : %s:%d\n", inet_ntoa(clnt_adr.sin_addr), ntohs(clnt_adr.sin_port));
        }

        pid = fork();
        if (pid == -1)
        {
            close(clnt_sock);
            continue;
        }
        if (pid == 0)
        {
            close(serv_sock);
            request_handler(&clnt_sock);
        }
        else
            close(clnt_sock);
    }
    close(serv_sock);
    return 0;
}

void* request_handler(void *arg)
{
    int clnt_sock = *((int*)arg);
    char req_line[SMALL_BUF];
    FILE* clnt_read;
    FILE* clnt_write;

    char method[10];
    char ct[15];
    char file_name[30];

    clnt_read = fdopen(clnt_sock, "r");
    clnt_write = fdopen(dup(clnt_sock), "w"); // duplicate clnt_sock

    fgets(req_line, SMALL_BUF, clnt_read);// clnt_read -> req_line 

    if (strstr(req_line, "HTTP/") == NULL) // find "HTTP/" in req_line
    {
        send_error(clnt_write);
        fclose(clnt_read);
        fclose(clnt_write);
        return;
    }

    strcpy(method, strtok(req_line, " /")); // req_line token 
    strcpy(file_name, strtok(NULL, " /")); //
    strcpy(ct, content_type(file_name));
    if (strcmp(method, "GET") != 0) // compare method with "GET"
    {
        send_error(clnt_write);
        fclose(clnt_read);
        fclose(clnt_write);
        return;
    }

    fclose(clnt_read);
    send_data(clnt_write, ct, file_name);
}

void send_data(FILE* fp, char* ct, char* file_name)
{

    int size;

    char protocol[] = "HTTP/1.1 200 OK\r\n";
    char server[] = "Server:Linux Web Server \r\n";
    char cnt_len[SMALL_BUF];
    char cnt_type[SMALL_BUF];
    char buf[DATA_SIZE];
    char keep_alive[] = "Keep-Alive: timeout=10, max=15\r\n";
    char connection[] = "Connection: Keep-Alive\r\n";
    FILE* send_file;

    sprintf(cnt_type, "Content-type:%s\r\n\r\n", ct);

    send_file = fopen(file_name, "rb");

    if (send_file == NULL)
    {
        send_file = fopen("error404.html", "r");
        send_404_error(fp, send_file);

    }
    else {
        fseek(send_file, 0, SEEK_END);
        size = ftell(send_file);
        fseek(send_file, 0, SEEK_SET);

        sprintf(cnt_len, "Content-length:%d\r\n", size);

        /* send header data*/
        fputs(protocol, fp);
        fputs(server, fp);
        fputs(cnt_len, fp);
        fputs(keep_alive, fp);
        fputs(connection, fp);
        fputs(cnt_type, fp);

        /* send requested data */
        while (fgets(buf, size, send_file) != NULL)
        {
            fputs(buf, fp);
            fflush(fp);
        }
    }
    fflush(fp);
    fclose(fp);
}

char* content_type(char* file)
{
    char extension[SMALL_BUF];
    char file_name[SMALL_BUF];
    strcpy(file_name, file);
    strtok(file_name, ".");
    strcpy(extension, strtok(NULL, "."));

    if (!strcmp(extension, "html") || !strcmp(extension, "htm"))
        return "text/html";
    else if (!strcmp(extension, "jpg") || !strcmp(extension, "jpeg"))
        return "image/jpg";
    else if (!strcmp(extension, "png"))
        return "image/png";
    else
        return "text/plain";
}

void send_404_error(FILE* fp, FILE* fp2)
{
    char buf[BUF_SIZE];
    char protocol[] = "HTTP/1.1 404 Not Found\r\n";
    char cnt_len[] = "Content-length:137\r\n";
    char cnt_type[] = "Content-type:text/html\r\n\r\n";
    char content[] = "<html><head><title>NETWORK</title></head>"
        "<body><font size=+5><br> 404 ERROR </br>"
        "</font></body></html>";

    fputs(protocol, fp);
    fputs(cnt_len, fp);
    fputs(cnt_type, fp);

    while (fgets(buf, BUF_SIZE, fp2) != NULL)
    {
        fputs(buf, fp);
        fflush(fp);
    }
    fflush(fp2);
    fclose(fp2);
}


void send_error(FILE* fp)
{
    char protocol[] = "HTTP/1.0 400 Bad Request\r\n";
    char server[] = "Server:Linux Web Server \r\n";
    char cnt_len[] = "Content-length:2048\r\n";
    char cnt_type[] = "Content-type:text/html\r\n\r\n";
    char content[] = "<html><head><title>NETWORK</title></head>"
        "<body><font size=+5> <br> 400 ERROR </br>"
        "</font></body></html>";

    fputs(protocol, fp);
    fputs(server, fp);
    fputs(cnt_len, fp);
    fputs(cnt_type, fp);
    fflush(fp);
}

void error_handling(char* message)
{
    fputs(message, stderr);
    fputc('\n', stderr);
    exit(1);
}

void read_childproc(int sig)
{
    pid_t pid;
    int status;
    pid = waitpid(-1, &status, WNOHANG);
    printf("removed proc id: %d \n", pid);
}

0 个答案:

没有答案