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

Dear Monks, Still playing with sockets here, and I have a working model within a script that generates a forked server and everything works swimmingly. Now, I'd like to break this up into some library files so I can share the code, I'm not sure about putting this as OO yet as what I intend to be able to reuse this for won't work as OO without a lot of directory and script redesign, which I can do later on as a larger project. My problem is being encountered in making the server and running IO::Socket::Net->new to generate the server; all the values are being sent correctly but somehow Protocol is being lost. Running in the debugger I get the following:
IO::Socket::socket(/usr/perl5/5.8.4/lib/sun4-solaris-64int/IO/Socket.p +m:78): 78: my($sock,$domain,$type,$protocol) = @_; DB<64> IO::Socket::socket(/usr/perl5/5.8.4/lib/sun4-solaris-64int/IO/Socket.p +m:80): 80: socket($sock,$domain,$type,$protocol) or 81: return undef; DB<64> x $sock 0 IO::Socket::INET=GLOB(0x4ea3bc) -> *Symbol::GEN0 DB<65> x $domain 0 2 DB<66> x $type 0 'SOCK_STREAM' DB<67> x $protocol 0 6 DB<68> IO::Socket::INET::_error(../Perl/IO/Socket/INET.pm:69): 69: my $sock = shift; DB<68> IO::Socket::INET::_error(../Perl/IO/Socket/INET.pm:70): 70: local($!); DB<68> x $sock 0 IO::Socket::INET=GLOB(0x4ea3bc) -> *Symbol::GEN0 DB<69> x $! 0 'Protocol not supported'
Checking the value of $protocol in the debugger shows its there when socket() is being called in socket.pm; but maybe its something I am not familiar with that could be generating the error? Any advice is welcome, I'd like to see what this is all about and get it working. Thanks a lot!

Replies are listed 'Best First'.
Re: Socket.pm returning Protocol not supported
by Somni (Friar) on Nov 13, 2007 at 02:43 UTC
    Given:

    DB<66> x $type 0 'SOCK_STREAM'

    You've apparently specified the socket type as a string, rather than the expected constant. Import the SOCK_STREAM constant from IO::Socket::INET, or better yet just don't specify it. IO::Socket::INET is smart enough to guess the socket type based on the protocol.

    However, this is largely a guess, given you haven't shown your code. In the future it'd be far more helpful to show the actual code having the problem, rather than a debug trace of a core module.

      Yeah, I had problems before with bare word issues using it as just the value that you would see in the cookbook. To me the problem seemed more apparent from the debug session, but I've been pretty intimate with the code I have been working on for the past couple days. I did not think of the string part, nor did I discern that from the documentation I read, but changing my code to the following worked well.
      sub make_server { my ($ipv6,$conn) = @_; print "Using protocol = $protocol, port = $socket_port.\n" if ($de +bug); if ($ipv6) { $server = IO::Socket::INET->new(Listen => $conn, LocalAddr => "localhost:$socket_port", Proto => $protocol, Reuse => 1) or die "Couldn't make IPv6 server " . $server{'LocalAddr'} ." + on port $socket_port: $!\n"; print "Running IPv6 enabled on $socket_port.\n" if ($debug); } else { $server = IO::Socket::INET->new(LocalAddr => "localhost:$sock +et_port", Proto => $protocol, Reuse => 1, Listen => $conn) or die "Can't make $protocol server " . $server{'LocalAddr'} +. " on port $socket_port: $!\n"; print "Running on $socket_port.\n" if ($debug); } }
      Hope that helps out anyone who may encounter this in the future.
        What is $server{'LocalAddr'}? There is no %server declared in your code; are you trying to access $server->{'LocalAddr'}, i.e. a hash key in the returned object? If so, I seriously doubt it's going to work. IO::Socket modules return blessed filehandles, not hashrefs; not to mention accessing it as a hashref is not documented, and so shouldn't be relied upon even if it does work.

        Also, did you use strict? Accessing %server like you are should cause a fatal error, given there's no declaration for it.