简单的 TCP 服务器无法输出到 Web 浏览器 [英] Simple TCP server can't output to web browser
问题描述
我用 C 语言编写了一个简单的 TCP 客户端和服务器程序.
I wrote a simple TCP client and server program in C language.
在它们之间运行良好,但我有疑问.
It works fine among them but I have a doubt.
如果我想从 Web 服务器访问 TCP 服务器怎么办?
What if I wanted to access the TCP server from the web server?
我从 Web 服务器获取了标头,但我无法 write()
返回到 Web 浏览器.响应是否应该强制使用 HTTP
规范?
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?
我收到以下错误:
服务器代码:
// 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
有什么问题?浏览器是否也期待来自服务器的 HTTP
类型响应?由于服务器正在发送纯文本,因此 Web 浏览器无法显示内容.这是网页浏览器显示错误的原因吗?
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?
推荐答案
有什么问题?浏览器是否也期待来自服务器的 HTTP 类型响应?由于服务器正在发送纯文本,因此 Web 浏览器无法显示内容.这是网页浏览器显示错误的原因吗?
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?
是,浏览器需要 HTTP 响应.
Yes, the browser expects a HTTP response.
这里是一个简单的 HTTP 响应:
Here a simple HTTP response:
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 协议版本.
HTTP/1.1
specifies the HTTP protocol version.
200 OK
就是所谓的响应状态.200 表示没有错误.
200 OK
is the so called response status. 200 means no error.
Content-Type
和 Content-Length
是 http 头:
Content-Type
and Content-Length
are http headers:
Content-Type
指的是内容类型(谁会猜到?).text/plain
表示纯文本.(还有text/html
、image/png
等.)
Content-Type
refers to the content type (who would've guessed?).text/plain
means plaintext. (There is alsotext/html
,image/png
etc..)
Content-Length
响应的总大小(以字节为单位).
Content-Length
total size of the response in bytes.
这篇关于简单的 TCP 服务器无法输出到 Web 浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!