代码之家  ›  专栏  ›  技术社区  ›  ssh

简单TCP服务器无法输出到web浏览器

  •  2
  • ssh  · 技术社区  · 7 年前

    我用C语言编写了一个简单的TCP客户端和服务器程序。

    在他们中间效果很好,但我有一个疑问。

    如果我想从web服务器访问TCP服务器,该怎么办?

    我从web服务器获取了头文件,但我无法 write() 返回web浏览器。响应是否应在 HTTP 强制规范?

    我得到以下错误 :

    enter image description here

    服务器代码:

    // Server side C program to demonstrate Socket programming
    #include <stdio.h>
    #include <sys/socket.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <netinet/in.h>
    #include <string.h>
    #define PORT 8080
    int main(int argc, char const *argv[])
    {
        int server_fd, new_socket; long valread;
        struct sockaddr_in address;
        int addrlen = sizeof(address);
        char buffer[1024] = {0};
        char *hello = "Hello from server";
    
        // Creating socket file descriptor
        if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
        {
            perror("In socket");
            exit(EXIT_FAILURE);
        }
    
    
        address.sin_family = AF_INET;
        address.sin_addr.s_addr = INADDR_ANY;
        address.sin_port = htons( PORT );
    
        // Forcefully attaching socket to the port 8080
        if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0)
        {
            perror("In bind");
            exit(EXIT_FAILURE);
        }
        if (listen(server_fd, 3) < 0)
        {
            perror("In listen");
            exit(EXIT_FAILURE);
        }
        while(1)
        {
            if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0)
            {
                perror("In accept");
                exit(EXIT_FAILURE);
            }
            valread = read( new_socket , buffer, 1024);
            printf("%s\n",buffer );
            write(new_socket , hello , strlen(hello));
            printf("Hello message sent\n");
        }
        return 0;
    }
    

    输出:

    GET / HTTP/1.1
    Host: localhost:8080
    Connection: keep-alive
    Cache-Control: max-age=0
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
    DNT: 1
    Accept-Encoding: gzip, deflate, br
    Accept-Language: en-US,en;q=0.9
    
    
    
    Hello message sent
    

    有什么问题?浏览器是否也需要 超文本传输协议 是否从服务器键入响应?由于服务器正在发送纯文本,web浏览器无法显示内容。这是web浏览器中显示错误的原因吗?

    1 回复  |  直到 7 年前
        1
  •  5
  •   Marco    7 年前

    有什么问题?浏览器是否也需要来自服务器的HTTP类型响应?由于服务器正在发送纯文本,web浏览器无法显示内容。这是web浏览器中显示错误的原因吗?

    ,浏览器需要HTTP响应。

    下面是一个简单的HTTP响应:

    HTTP/1.1 200 OK
    Content-Type: text/plain
    Content-Length: 12
    
    Hello world!
    

    C代码段:

    const char *hello = "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 12\r\n\r\nHello world!";
    

    说明部分:

    HTTP/1.1 指定HTTP协议版本。

    200 OK 就是所谓的 响应状态 . 200表示无错误。

    List of HTTP status codes (wikipedia)

    Content-Type Content-Length 是http标头:

    • 内容类型 指的是内容类型(谁会猜到?)。 text/plain 表示纯文本。(还有 text/html , image/png 等等……)

    • 内容长度 响应的总大小(字节)。