我在Perl中创建了一个HTTP服务器来接受来自客户端的请求。
目前只有一个客户端发送请求。
这就是我的设置:
客户 - >服务器(这是代理服务器以及连接到互联网),在Ubuntu上运行的Apache 2。
这是我服务器的Perl代码:
#!/usr/bin/perl
use IO::Socket::INET;
use strict;
use warnings;
use LWP::Simple;
# auto-flush on socket
$| = 1;
my $port = 7890;
# Create a listening port
my $socket = new IO::Socket::INET(
LocalHost => '127.0.0.1',
LocalPort => shift || $port,
Proto => 'tcp',
Listen => SOMAXCONN,
Reuse => 1
) or die "cannot create socket $!\n";
# open a file and write client requests to the file
$| = 1;
open(FH, '>>', '/home/suresh/clientrequest.txt')
or die "could not open the /home/suresh/clientrequest : $!\n";
print FH "server waiting for client on port\n"
or die "could not write to file : $!\n";
while (my $client_socket = $socket->accept()) {
$client_socket->autoflush(1);
#print FH "Welcome to $0 \n";
my $client_address = $socket->peerhost();
my $client_port = $client_socket->peerport();
print FH "connection from $client_address:$client_port\n";
# read from connected client
my $data = "";
$client_socket->recv($data, 1024);
print FH "Data received from $client_address:$client_port: $data\n";
# write response data to the client
$data = "Sucessfully processed your request";
$client_socket->send($data);
shutdown($client_socket, 1);
}
close(FH);
$socket->close();
当我启动此服务器并尝试从客户端发送请求时,请求将写入该文件,因此看起来服务器会捕获这些请求。
任何人都可以告诉我在服务器端和客户端需要做的其他配置吗?
答案 0 :(得分:1)
如果你写
$| = 1;
然后仅为默认输出文件句柄激活刷新。这是STDOUT
,除非使用内置select()
进行更改。所以FH
没有在这里刷新 - 我想这是你的意图。相反,你必须写
FH->autoflush(1);