1

我在 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();

当我启动此服务器并尝试从客户端发送请求时,请求被写入文件,因此看起来请求被服务器捕获。

谁能告诉我在服务器端和客户端我需要做哪些其他配置?

4

1 回答 1

1

如果你写

$| = 1;

然后仅对默认输出文件句柄激活刷新。除非使用内置函数进行STDOUT更改。select()So FHis not flushed here - 我想这是你的意图。相反,你必须写

FH->autoflush(1);
于 2013-09-17T09:55:32.623 回答