Perl:使用简单的HTTP服务器重置连接 [英] Perl: Connection reset with simple HTTP server

查看:148
本文介绍了Perl:使用简单的HTTP服务器重置连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Perl编写一个非常简单的HTTP服务器来提供一个单独的html文件。但是,几乎每次我访问该网站时,我都会收到连接重置错误(但它确实有5%的时间)。我已阅读这篇文章,但我无法做任何事情。

我开始使用的服务器可以在这里,但我试图修改它为



a)在文件中读取而不是使用硬编码的html和



b)用每一个新的HTTP请求读取这个文件,这样就可以看到刷新了。



这是我的代码: p>

 #!/ usr / bin / env perl 

use strict;
使用警告;
使用Socket;
my $ port = 8080;
my $ protocol = getprotobyname(tcp);
套接字(SOCK,PF_INET,SOCK_STREAM,$协议)或死无法打开套接字:$!;
## PF_INET表示这个套接字将连接到互联网域
## SOCK_STREAM表示TCP流,SOCK_DGRAM表示UDP通信
setsockopt(SOCK,SOL_SOCKET,SO_REUSEADDR,1)或死无法设置套接字选项:$!;
## SOL_SOCKET表示我们在套接字上设置了一个选项而不是协议
##标记套接字可重用
bind(SOCK,sockaddr_in($ port,INADDR_ANY))或死无法将套接字绑定到端口$ port:$!;
##将我们的套接字绑定到$ port,允许任何IP连接
listen(SOCK,SOMAXCONN)或者无法监听端口$ port:$!;
##开始监听传入连接



while(accept(CLIENT,SOCK)){
open(FILE,<, index.html)或死无法打开\index.html\:$!; $(< FILE>){
print CLIENT $ _;
};
关闭FILE;
关闭CLIENT;

$ / code>

感谢任何帮助。


<您不读取来自客户端的HTTP请求,即在有数据可读的时候关闭连接。这可能会导致RST。


I am attempting to write a very simple HTTP server with Perl to serve up a single html file. Almost every time I go to the site, however, I get a "connection reset" error (it does however work something like 5% of the time). I have read this post, but I can't make anything of it.

The server I started with can be found here, but I am attempting to modify it to

a) read in a file instead of using hard-coded html and

b) read this file with every new HTTP request so changes can be seen with a refresh.

Here is my code:

#!/usr/bin/env perl

use strict;
use warnings;
use Socket;
my $port = 8080;
my $protocol = getprotobyname( "tcp" );
socket( SOCK, PF_INET, SOCK_STREAM, $protocol ) or die "couldn't open a socket: $!";
## PF_INET to indicate that this socket will connect to the internet domain
## SOCK_STREAM indicates a TCP stream, SOCK_DGRAM would indicate UDP communication
setsockopt( SOCK, SOL_SOCKET, SO_REUSEADDR, 1 ) or die "couldn't set socket options: $!";
## SOL_SOCKET to indicate that we are setting an option on the socket instead of the protocol
## mark the socket reusable
bind( SOCK, sockaddr_in($port, INADDR_ANY) ) or die "couldn't bind socket to port $port: $!";
## bind our socket to $port, allowing any IP to connect
listen( SOCK, SOMAXCONN ) or die "couldn't listen to port $port: $!";
## start listening for incoming connections



while( accept(CLIENT, SOCK) ){
    open(FILE, "<", "index.html") or die "couldn't open \"index.html\": $!";
    while(<FILE>) {
        print CLIENT $_;
    };
    close FILE;
    close CLIENT;
}

Any help is appreciated, thanks.

解决方案

You don't read the HTTP request from the client, i.e. you close the connection while there are still data to read. This will probably cause the RST.

这篇关于Perl:使用简单的HTTP服务器重置连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆