develooper Front page | perl.perl5.porters | Postings from December 2011

[perl #8933] sockport() method of socket class returns 0 when we make a udp

Thread Previous
From:
James E Keenan via RT
Date:
December 31, 2011 06:26
Subject:
[perl #8933] sockport() method of socket class returns 0 when we make a udp
Message ID:
rt-3.6.HEAD-14510-1325341611-1789.8933-15-0@perl.org
On Fri Apr 12 01:33:25 2002, pturaga@bloomberg.com wrote:
> Hello, i was trying to run the following perl script using perl 5.6.1 
> 
> #!/usr/bin/perl -w
> 
> use IO::Socket;
> use IO::Select;
> 
> 
> #  v5.6.1 built for sun4-solaris using socket connection
> # program to test the working of sockport()
> # the socket port is randomly selected but the sockport() does not
> # display it
> my $socket   = new IO::Socket::INET(Proto => "udp", Type => SOCK_DGRAM);
> if ( $socket )
> {
>         my $procport = $socket->sockport();
>         print "$procport\n";
>         # this prints 0 after execution
>         $socket->close;
> }
> else
> {
>         print "failed\n";
> }
> 
> The same code runs well in version 5.005_03 built for sun4-solaris
> the function sockport() returns the udport number.
> 
> Can u please help me with this? thanks
> partha
> 

Have you determined whether a socket connection was actually established?

The documentation for IO::Socket describes the "connected" method:

#####
connected
If the socket is in a connected state the peer address is returned. 
If the socket is not in a connected state then undef will be 
returned.
#####

Adding that to your code (and deleting the import of IO::Select, which
seemed to have no effect), I tried this code:

#####
$ cat x8933.pl 
#!/usr/local/bin/perl
use strict;
use warnings;
use feature ':5.10';
use IO::Socket;

my $socket = new IO::Socket::INET(Proto => "udp", Type => SOCK_DGRAM);
defined($socket->connected()) ? say "connected" : say "not connected";
if ( $socket ) {
    say "sockaddr: <", $socket->sockaddr(), ">";
    say "sockport: <", $socket->sockport(), ">";
    say "sockhost: <", $socket->sockhost(), ">";
    $socket->close;
}
else {
    say "failed";
}
#####

Output:

#####
$ perl x8933.pl 
not connected
sockaddr: <>
sockport: <0>
sockhost: <0.0.0.0>
#####

I don't know much about sockets, but if no connection was actually made,
then getting a value of '0' for sockport is not surprising.

Thank you very much.
Jim Keenan

---
via perlbug:  queue: perl5 status: open
https://rt.perl.org:443/rt3/Ticket/Display.html?id=8933

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About