简体   繁体   中英

PHP packet send in perl

I have this code to send a packet in PHP, and I wanted to know how to do it in perl, I have tried sending via hex data, but it is not working. Here is the PHP code:

$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); 

How would I go about doing this in Perl? I want to use a raw socket as well to send it.

This is what I tried, but the server is not replying to it, which makes me think that the data is corrupted somewhere:

$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;

Don't know about Net::RawIP , but here's the Perl variant that sends the exact same packet as your PHP code, using IO::Socket::INET module. For docs for it, see 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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM