Simple TCP server can't output to web browser

廉价感情. 提交于 2019-12-24 00:55:09

问题


I wrote a simple TCP client and server program in C language.

It works fine among them but I have a doubt.

What if I wanted to access the TCP server from the web server?

I got the headers from web server and I can't write() back to the web browser. Should the response be in HTTP specifications compulsorily?

I get the following error:

Server Code:

// 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;
}

Output:

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

What is the problem? Is browser also expecting HTTP type response from the server? As the server is sending plain text, web browser can't display the contents. Is this the reason for the error displayed in web browser?


回答1:


What is the problem? Is browser also expecting HTTP type response from the server? As the server is sending plain text, web browser can't display the contents. Is this the reason for the error displayed in web browser?

Yes, the browser expects a HTTP response.

Here a simple HTTP response:

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

Hello world!

C Code snippet:

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

Parts explained:

HTTP/1.1 specifies the HTTP protocol version.

200 OK is the so called response status. 200 means no error.

List of HTTP status codes (wikipedia)

Content-Type and Content-Length are http headers:

  • Content-Type refers to the content type (who would've guessed?). text/plain means plaintext. (There is also text/html, image/png etc..)

  • Content-Length total size of the response in bytes.



来源:https://stackoverflow.com/questions/49152320/simple-tcp-server-cant-output-to-web-browser

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!