Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

IO::Socket Listen

by Secalles (Initiate)
on Dec 02, 2011 at 12:03 UTC ( #941305=perlquestion: print w/replies, xml ) Need Help??
Secalles has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks ,

I wrote this simple code because I just want to send some information to a device , the program sends the query (I used wireshark and saw the package leaving corrertly my machine and the device responded correctly too ) but I can't recieve this info in my program and i don't know what to do , here is the code

use IO::Socket; #Sender my $send = new IO::Socket::INET ( PeerAddr => '', PeerPort => '6008', Proto => 'tcp', ); die "Couldn't Send: $!\n" unless $send; #Reciever my $recieve = new IO::Socket::INET ( LocalHost => '', LocalPort => '6008', Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Couldn't Recieve: $!\n" unless $recieve; $data = pack("CccccccCcCC",02,65,63,80,81,76,86,03,100,13,10); print $send $data ; #close($send); my $new_sock = $recieve->accept(); while(<$new_sock>) { print $_; } close($recieve);

I appreciate any help!

Thanks in advanced!

Replies are listed 'Best First'.
Re: IO::Socket Listen
by Eliya (Vicar) on Dec 02, 2011 at 12:15 UTC

    I'm a bit surprised that you've set up two separate sockets for sending and receiving.  Is the device actually supposed to initiate a backwards connection to your machine for its reply, instead of simply replying via the socket you've already established when connecting to the device?

Re: IO::Socket Listen
by Marshall (Abbot) on Dec 02, 2011 at 15:41 UTC
    As Eliya pointed out, the socket is a 2-way connection and normally the server uses it to talk back to you.
    use IO::Socket; my $sock = new IO::Socket::INET ( PeerAddr => '', PeerPort => '6008', Proto => 'tcp', ) or die "Couldn't connect!! $!"; $data = pack("CccccccCcCC",02,65,63,80,81,76,86,03,100,13,10); print $sock $data ; while(<$sock>) { print $_; }
      Thanks i did this , yeah i did it the first time but i got no response , so i tryed 2 sockets ( thinking that the device could responde faster than i could listen ) , but this program takes 5 minutes to respond , what can i do to make it faster?
        but this program takes 5 minutes to respond

        The problem probably is that the device isn't sending a newline, so the <$sock> (i.e. readline) keeps waiting for it until the socket is finally closed — which is likely what is happening due to a timeout after those 5 minutes...

        Try sysread instead (with a length value larger than the response you expect).

        while ( sysread $sock, $_, 1000 ) { print $_; }
        I think you need to read the spec on the I/F to this server. You are sending <CR><LF> at the end of your packet, but many of these things work with fixed length packets instead of line oriented packets.

        It could be that you need to send perhaps 128 bytes to it instead of a buffer ending in CRLF? Note: that CRLF is the network line ending, not just <LF>, so that part is right as far as it goes...However it could be that this thing is waiting for more bytes from you... Maybe..Send minimum 128 bytes? Read their spec..

        Usually the smallest packet that will be sent is 128 bytes. Sometimes a 256 byte message will take a couple of packets even on the local machine.

        Try just printing whatever the server returns to you on the first sysread() attempt. That will give you a clue as to what it is doing. If all you are expecting is less than 128 bytes, then you are probably done!

        FYI, A Perl client read routine to get a 512 byte fixed packet might look like this:
        my $buf = readn ($socket, 512);

        sub readn { my ($socket, $bytes ) = @_; my $offset = 0; my $buf = ""; while ($offset < $bytes) { my $nread; my $nleft = $bytes-$offset; $nread = sysread($socket,$buf,$nleft,$offset); kill 'USR1',$$ unless (defined $nread); ## undef is like -1 uni +x return last if ($nread ==0); ## EOF $offset += $nread; } # print "length of received buff=",length $buf,"\n"; # print $buf; return $buf; }
        Update: Perl will work with line oriented packets GREAT! But if that is not what this thing is sending back, then this is for naught. print the results of the first sysread() and see where that leads.
        ***Also perhaps try a flush() on your end! Normal socket IO is buffered. $socket->flush().

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://941305]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2018-02-23 03:23 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (300 votes). Check out past polls.