help convert perl script to purebasic [solved]

Just starting out? Need help? Post your questions and find answers here.
szerda
User
User
Posts: 12
Joined: Wed Sep 14, 2005 1:57 pm

help convert perl script to purebasic [solved]

Post by szerda »

I have a perl script, which read info from our NAS server, i try convert it to purebasic but not work, please help if somebody can.
Thank you.

Code: Select all

#!/usr/bin/perl -w

use Socket;

my $data='';

# NAS UDP port number
my $nas_port = 22081;

# NAS IP address/hostname
my $nas_host = '192.168.1.1';

my $nas_paddr = sockaddr_in($nas_port, inet_aton($nas_host));

# craft request packet
my $msg = chr(0x00).chr(0x00).chr(0x05).chr(0xd3).chr(0x00).chr(0x00). 
       chr(0x00).chr(0x01).chr(0x00).chr(0x00).chr(0x00).chr(0x00). 
       chr(0x80).chr(0xc9).chr(0x6c).chr(0x05).chr(0xff).chr(0xff). 
       chr(0xff).chr(0xff).chr(0x00).chr(0x00).chr(0x00).chr(0x1c). 
       chr(0x00).chr(0x00).chr(0x00).chr(0x00);

socket(SOCKET, PF_INET, SOCK_DGRAM, getprotobyname('udp'))   || die "socket: $!";

defined(send(SOCKET, $msg, 0, $nas_paddr))    || die "send $nas_host: $!";

$nas_paddr = recv(SOCKET, $data, 1458, 0)        || die "recv: $!";
print substr($data, 28)."\n";

exit 0;
Last edited by szerda on Mon Nov 07, 2011 11:13 am, edited 1 time in total.
Sorry my bad English
szerda
User
User
Posts: 12
Joined: Wed Sep 14, 2005 1:57 pm

Re: help convert perl script to purebasic

Post by szerda »

here is my code that i try, but it is not work, no receive data and i don't known how can convert msg variable (Protocol request, must be 28 bytes long )

Code: Select all

;Protocol request, this must be 28 bytes long
msg.s="000007050000000100000000A4A1EFB8FFFFFFFF0000001C00000000"
len=Len(msg)
*memory=AllocateMemory(len/2)
For k=0 To (len/2)-1
  PokeB(*memory+k,Val("$"+PeekS(@msg+(k*2),2)))
Next

*Buffer = AllocateMemory(1000)

If InitNetwork()

  ConnectionID = OpenNetworkConnection("192.168.1.1", 22081, #PB_Network_UDP)
  
  If ConnectionID

    Result = SendNetworkData(ConnectionID,@memory,len/2)

    Result = ReceiveNetworkData(ConnectionID, *Buffer, 1000)
    
    MessageRequester("Info", "String: "+PeekS(*Buffer), 0)
    
  EndIf
  
  CloseNetworkConnection(ConnectionID)
  
EndIf
Sorry my bad English
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: help convert perl script to purebasic

Post by infratec »

Hi,

maybe this gives better results

Code: Select all

#nas_port = 22081
#nas_host = "192.168.1.1"


If InitNetwork()
 
  *Buffer = AllocateMemory(1500)
  If *Buffer
    ConnectionID = OpenNetworkConnection(#nas_host, #nas_port, #PB_Network_UDP)
    If ConnectionID
      SendNetworkData(ConnectionID, ?MSG, 28) 
     
      Result = 0
      Starttime = ElapsedMilliseconds()
      While ElapsedMilliseconds() - Starttime < 3000
        If NetworkClientEvent(ConnectionID) = #PB_NetworkEvent_Data
          Result = ReceiveNetworkData(ConnectionID, *Buffer, MemorySize(*Buffer))
          Break
        EndIf
      Wend
      If Result > 28
        MessageRequester("Info", "String: "+ PeekS(*Buffer + 28))
      EndIf
      CloseNetworkConnection(ConnectionID)
    EndIf
    FreeMemory(*Buffer)
  EndIf
EndIf

End 0

DataSection
  MSG:
  Data.a $00, $00, $05, $d3, $00, $00
  Data.a $00, $01, $00, $00, $00, $00
  Data.a $80, $c9, $6c, $05, $ff, $ff
  Data.a $ff, $ff, $00 ,$00, $00, $1c
  Data.a $00, $00, $00, $00
EndDataSection
Not tested :wink:

Not fully true: Sending works, checked with wireshark.

Bernd
Last edited by infratec on Mon Nov 07, 2011 10:49 pm, edited 3 times in total.
szerda
User
User
Posts: 12
Joined: Wed Sep 14, 2005 1:57 pm

Re: help convert perl script to purebasic

Post by szerda »

Thank you your help infratec
i think it is work because the receive result size 442, so there are some data in *Buffer, but PeekS read nothing.
Why not, or how can i reed *Buffer data?
Sorry my bad English
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: help convert perl script to purebasic

Post by infratec »

Hi szerda,

you can replace

Code: Select all

MessageRequester("Info", "String: "+ PeekS(*Buffer, 28))
with

Code: Select all

For i = 0 To 27
  Byte.a = PeekA(*Buffer + i)
  Debug "Byte " + Str(i + 1) + " = " + Hex(Byte) + " -> " + Chr(Byte)
Next i
Than you can see definately the first 28 received bytes.

Maybe the 'string' contains a $00 at the beginning.
Than Peeks() stops, because the string is finished.

Bernd
szerda
User
User
Posts: 12
Joined: Wed Sep 14, 2005 1:57 pm

Re: help convert perl script to purebasic

Post by szerda »

Thank you infratec again

this code working, *Buffer need shift 28 and i can read received string.

Code: Select all

MessageRequester("Info", PeekS(*Buffer+28))
Thanx
Szerda
Sorry my bad English
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: help convert perl script to purebasic [solved]

Post by infratec »

Sorry,
my fault.

I missinterpreted the substr() command.

Bernd

P.S.: I changed my code above.
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: help convert perl script to purebasic [solved]

Post by infratec »

Hi,

I found an optimization:
It was not neccessary to copy the MSG Data to the *Buffer.
We can directly send the MSG.

I modified my listing above.

Bernd
szerda
User
User
Posts: 12
Joined: Wed Sep 14, 2005 1:57 pm

Re: help convert perl script to purebasic [solved]

Post by szerda »

Nice thank you
Sorry my bad English
Post Reply