Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

Using block - Perl...help!


Here is part of my script:
How do I modify the socket to make it nonblocking?  What happens is that it hangs(waits) for a period of time!
            
my $socket = new IO::Socket::INET (
                                  PeerAddr => '10.0.0.5',
                                  PeerPort => '2222',
                                  Proto => 'tcp',
                                 );


@requestBytes=( 0xD0, 0x00, 0x00, 0x00,
                0x28, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x65, 0x00, 0x00, 0x00,  
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00,
                0x07, 0x00, 0x00, 0x00 );
               
#print "@requestBytes\n";

$requestMsg = pack 'C*',@requestBytes;

  $socket->write($requestMsg);


while($socket->connected) {
  $buffer=0;
  #Retrieve only header
  $socket->read($buffer,40);
0
jewee
Asked:
jewee
1 Solution
 
ozoCommented:
If the socket does not have 40 bytes ready for you to read, what would you want to do other than wait for it to send you the data?

If the read is never returning, it may be because your write never flushed its buffer to send it to your peer.
If that's the problem you may want to set
  $socket->autoflush(1);
before writing
0
 
jhurstCommented:
If you want your script to go on then surely it needs the data.

If you can continue and accept the fact that the data is not there then why are you asking for it.

If you want to "time out" then I would sue an alarm() to set a timeout and go on even if the request does not finish in the time required.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now