Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Socket response inconsistent

by agoth (Chaplain)
on Jul 31, 2001 at 16:33 UTC ( [id://101136]=perlquestion: print w/replies, xml ) Need Help??

agoth has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to test out the success or failure of sending a UDP message to a set of servers which may be up or down.

On the port range I have chosen, the response seems to alternate and not be the same each time, I have waded back and forth through the perldoc and the cookbook for a clue, to no avail, so is it a problem with my use of the IO modules?

I am getting:

READ *3*
READ *0*
Read IO select timed out at ./socket.pl line 24.
READ *3*
READ *0*
Read IO select timed out at ./socket.pl line 24

when instead I want four dies in a row.... code below:

#!/usr/local/bin/perl -w use strict; use IO::Select; use IO::Socket; my $server = '127.0.0.1'; for (8721 .. 8724) { my $fd = new IO::Socket::INET( PeerAddr => $server, PeerPort => $_, Proto => 'udp'); my $sockin = new IO::Select( $fd ); eval { $fd->send( 'Test string' ); my $read = IO::Select->select( $sockin, undef, undef, 3); print "READ *$read*\n"; die "IO select timed out" unless $read; }; print "Read $@" if $@; }

Replies are listed 'Best First'.
Re: Socket response inconsistent
by Cubes (Pilgrim) on Jul 31, 2001 at 18:08 UTC
    I'm not sure what you're trying to do here. You say you're trying to test the success of sending a UDP message, but you don't check the return value of your send. Even if you do check that, it's not going to tell you anything about the server on the other end. UDP is an unreliable, connectionless protocol -- you'll find out whether or not your bits went out on the wire, nothing more.

    You're also doing strange things with the return value of select -- it returns an array of 3 array refs (read/write/error), or an empty array. In your code, the $read variable ends up with the length of the array in it, and you die if select returned an empty array. This works, but you'd probably be better off making $read an array, both for clarity and so you can do something meaningful with it if the select doesn't time out.

    I'm not sure why you're getting the varying behavior -- on my box it's very consistent (select returns me a handle ready to read every time if I throw packets at an IP address with a live box on it; timeouts every time if it's a dead address). You are sending your packets to localhost, which may have something to do with it. If what you're trying to do is check for a response from the server you're throwing these packets at, you'll want to add a recv in there after the select to see what, if anything, the server sent back.

    It's been a long time since I did anything with UDP, so I may be a little off here and there. You might find it enlightening to pick up a good TCP/IP networking book and read up on the guts of this stuff. Knowing what's supposed to happen behind the scenes makes this sort of debugging a lot easier.

      The sending of the UDP message has been consistently ok, if I print the return of send that's consistent and fine.

      The point of the script is to test the status of the server at the other end, when it receives a UDP message goes off and does stuff and returns a UDP response.

      I dont want to go ahead and check recv unless the IO::Select has a consistent and checkable response, but even if i turn $read into a proper array, it doesn't help much.

      Points taken on your box does it, I'll check versions, and have been looking out for a TCP/IP book for a while, time to bite the bullet, cheers :)

        When I did a recv after getting something back from select, I consistently got empty (i.e. zero byte) responses. Though I'm not entirely sure what is supposed to happen here, this does make some kind of sense to me -- select is saying "yeah, the socket's there, go ahead and read from it," even though there is no data presently waiting.

        As for books, I highly recommend UNIX Network Programming by W. Richard Stevens, and Douglas Comer's Internetworking with TCP/IP (Vol. I covers the basic, low-level stuff). Not exactly light bedtime reading, but they will give you a solid understanding of what lies beneath all these slick perl modules.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://101136]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-16 07:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found