douxiegan6468 2016-02-25 02:30
浏览 63
已采纳

将数据从Web服务器发送到perl套接字侦听器

I try to send data from php web script to local perl socket listener. Here is perl server code:

#!/usr/bin/perl

use IO::Socket::INET;
use strict;

$| = 1;

my $web = new IO::Socket::INET (
    LocalHost => '127.0.0.1',
    LocalPort => '9001',
    Proto => 'tcp',
    Listen => 5,
    Reuse => 1
) or die "ERROR in Socket Creation : $!
";

while ($web->accept()) {
    my $web_address = $web->peerhost();
    my $web_port = $web->peerport();

    print "Accepted New Web Client Connection From : $web_address, $web_port
";

    my $data = <$web>;
    chomp ($data);
    print "Data from web: $data
";
}

And here is php code:

if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Couldn't create socket: [$errorcode] $errormsg 
");
}

echo "Socket created 
";

if(!socket_connect($sock , '127.0.0.1' , 9001))
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not connect: [$errorcode] $errormsg 
");
}

echo "Connection established 
";

$message = "GET / HTTP/1.1

";

if( !socket_send ($sock , $message , strlen($message) , 0))
{
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not send data: [$errorcode] $errormsg 
");
}

echo "Message send successfully 
";

Here is output of perl script:

Accepted New Web Client Connection From : , 
Data from web:

And here is output of php script:

Socket created 
Connection established 
Message send successfully

So, why it actually don't send the data?

  • 写回答

1条回答 默认 最新

  • duanhe4267 2016-02-25 02:53
    关注

    The $web->accept() only creates and returns an object, which can be used to read the handle (for the socket). You are attempting to read with $web itself, which it cannot do. In other words, <$web> does not read from the socket. The PHP client has sent its message which is sitting in the buffer waiting to be read.

    Further, as the PHP client prints it is filling up the buffer, since nothing is emptying it (reading) on the other end. If it prints enough at one point the buffer will get full and the next print will hang, just waiting. This is a common source of errors, when the buffer is not being read from orderly.

    Your code needs small changes

    if ( my $listen = $web->accept() ) {
        my ($buff, $recd);
        while (defined($buff = <$listen> ) { 
            chomp($recd = $buff);
            # process the line in $recd
        }
    } else { warn "Error with the socket  -- $!"; }
    

    The while around $buff = <$listen> is needed to read more than one line as they are sent. For multiple connections that you allow all of this should be in a while loop, which you have. In a nutshell, for the one-line-message in your posted example

    my $listen = $web->accept();
    my $recd = <$listen>;
    chomp($recd);
    print "$recd
    ";
    

    This is as far as Perl code goes. I don't know how PHP routines work (they seem to be OK to me).

    While IO::Socket provides basic information, there is a lot more scattered around Perl documentation. For example, there is a server example using IO::Socket in Perl IPC, by the very end of that page.

    If I may suggest, please please add use warnings; at the beginning. It is actually extremely useful. I suspect that it would have warned you of this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)