PHP包在perl中发送

时间:2014-01-10 21:06:36

标签: php perl sockets

我有这个代码用PHP发送数据包,我想知道如何在perl中执行它,我尝试通过十六进制数据发送,但它无法正常工作。这是PHP代码:

$sIPAddr = "37.221.175.211";                                                         
$iPort = 7777;                                                               
$sPacket = "";                                                                 
$aIPAddr = explode('.', $sIPAddr);                                              

$sPacket .= "SAMP";                                                           

$sPacket .= chr($aIPAddr[0]);                                                   
$sPacket .= chr($aIPAddr[1]);                                                   
$sPacket .= chr($aIPAddr[2]);                                                   
$sPacket .= chr($aIPAddr[3]);                                                   

$sPacket .= chr($iPort & 0xFF);                                               
$sPacket .= chr($iPort >> 8 & 0xFF);                                          

$sPacket .= 'c';                                                              


$rSocket = fsockopen('udp://'.$sIPAddr, $iPort, $iError, $sError, 2);           
fwrite($rSocket, $sPacket);

fclose($rSocket); 

我如何在Perl中执行此操作?我也想使用原始套接字发送它。

这是我尝试过的,但是服务器没有回复它,这让我觉得数据在某处被破坏了:

$packet = Net::RawIP->new({
                        ip => {
                              saddr => $saddr,
                              daddr => $dest,
                              },

                        udp => {
                              source => $rsport,
                              dest => $port,
                              data => "\x53\x41\x4d\x50\x25\xdd\xaf\xd3\x61\x1e\x63", # this is the data from the PHP file in HEX
                              },
                        });
  $packet->send;

1 个答案:

答案 0 :(得分:1)

不了解Net::RawIP,但这里是使用IO::Socket::INET模块发送与PHP代码完全相同的数据包的Perl变体。有关它的文档,请参阅https://metacpan.org/pod/IO::Socket::INET

use strict;
use warnings;
use IO::Socket;

my $sIPAddr = '37.221.175.211';
my $iPort = 7777;
my $sPacket = 'SAMP' . join( '', map chr,
    split(/\./, $sIPAddr),
    $iPort & 0xFF,
    $iPort >> 8 & 0xFF,
) . 'c';

my $sock = IO::Socket::INET->new(
    Proto    => 'udp',
    PeerPort => $iPort,
    PeerAddr => $sIPAddr,
) or die "Could not create socket: $!\n";

$sock->send( $sPacket );
相关问题