This works on both OSs....
my $linger = getsockopt($sock,SOL_SOCKET,SO_LINGER);
if(defined($linger)){
        if($^O eq "MSWin32"){
                print("Length of linger is ");
                print(length($linger)."\n");
        my @jim = unpack("SS", $linger);
        print("The length of linger unpacked is ".scalar(@jim)."\n");
        print("The SO_LINGER return value is $jim[0] and $jim[1]\n");
        }else {
        print("Length of linger is ");
        print(length($linger)."\n");
        my @jim = unpack("II", $linger);
        print("The length of linger unpacked is ".scalar(@jim)."\n");
        print("The SO_LINGER return value is $jim[0] and $jim[1]\n");
        }
} else {
    print("Return value fo SO_LINGER is undefined\n");
}

> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On 
> Behalf Of John Serink
> Sent: Friday, January 07, 2005 2:09 PM
> To: $Bill Luebkert; perl-win32-users@listserv.ActiveState.com
> Subject: RE: getsockopt--Solution on Win32
> 
> 
> Hi Bill:
> 
> Thanx for that! Got Stein's "Network programming With Perl" 
> and he alos have the unit struc in it as does allthe online 
> docs. Just one of those things.
> 
> Also, in Windows SO-LINGER appears to be off, or set to zero 
> by default.
> 
> Will do an OS check before I setup the socket to make the 
> appropriate settings
> 
> Cheers,
> john
> 
> > -----Original Message-----
> > From: [EMAIL PROTECTED]
> > [mailto:[EMAIL PROTECTED] On 
> > Behalf Of $Bill Luebkert
> > Sent: Friday, January 07, 2005 1:57 PM
> > To: perl-win32-users@listserv.ActiveState.com
> > Subject: Re: getsockopt--Solution on Win32
> > 
> > 
> > John Serink wrote:
> > 
> > > Ok, from:
> > > 
> > 
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winso
> > > ck
> > > /winsock/linger_2.asp
> > > 
> > > You'll note that using Winsock the linger structure is:
> > typedef struct
> > > linger {
> > >   u_short l_onoff;
> > >   u_short l_linger;
> > > } linger;
> > 
> > Too bad the orig post didn't include that.  ;)
> > 
> > > While from:
> > > http://www.rt.com/man/getsockopt.2.html
> > > The linus linger structure is:
> > > struct linger {
> > >   int  l_onoff;   /* Linger active */
> > >       int  l_linger;  /* How long to linger for */
> > > };
> > > As such, a correct way to do this in Win32:
> > > my $setsock =
> > setsockopt($sock,SOL_SOCKET,SO_LINGER,pack("SS",1,120));
> > > if(defined($setsock)){
> > >     $linger = getsockopt($sock,SOL_SOCKET,SO_LINGER);
> > >     if(defined($linger)){
> > >         print("setcockopt was successful to on and 120 
> seconds\n");
> > >         print("Linger is $linger\n");
> > >         print("Length of linger is ");
> > >         print(length($linger)."\n");
> > >         my $firstbyte = substr($linger,0,2,"");
> > >         print("Length of linger after substr is ");
> > >         print(length($linger)."\n");
> > >         print("Length of firstbyte after substr is ");
> > >         print(length($firstbyte)."\n");
> > >         $linger = unpack("S*", $linger);
> > >         print("The length of linger unpacked is
> > ".length($linger)."\n");
> > >         print("The SO_LINGER secondbyte return value is 
> $linger\n");
> > >         $firstbyte = unpack("S*", $firstbyte);
> > >         print("The length of firstbyte unpacked is
> > > ".length($firstbyte)."\n");
> > >         print("The SO_LINGER firstbyte return value is 
> > $firstbyte\n");
> > >     } else {
> > >         print("Return value fo SO_LINGER is undefined\n");
> > >     }
> > > }
> > 
> > I hadn't looked at the linger structure since he described it
> > as in int and getsockopt returns an int (needless to say it 
> > has two fields encoded into the int apparently).
> > 
> > A little simpler snippet and it includes the creation of
> > socket and pre-check (for Win32 only - you could add a check) :
> > 
> > use strict;
> > use IO::Socket;
> > 
> > my $S = IO::Socket::INET->new(LocalPort => 6666, Listen => 5,
> > Reuse => 1) or
> >   die "new socket: $! ($^E)";
> > 
> > check_linger ();
> > my $ret = setsockopt $S, SOL_SOCKET, SO_LINGER, pack 'SS', 1,
> > 120; die "setsockopt: $! {$^E)" if not $ret; check_linger (); exit;
> > 
> > sub check_linger {
> > 
> > my $ret = getsockopt $S, SOL_SOCKET, SO_LINGER;
> > die "getsockopt: $! ($^E)\n" if not $ret;
> > my ($onoff, $ltime) = unpack 'SS', $ret;
> > print "onoff = $onoff\n";
> > print "ltime = $ltime\n";
> > 
> > }
> > 
> > __END__
> > 
> > -- 
> >   ,-/-  __      _  _         $Bill Luebkert    
> > Mailto:[EMAIL PROTECTED]
> >  (_/   /  )    // //       DBE 
> > Collectibles    Mailto:[EMAIL PROTECTED]
> >   / ) /--<  o // //      Castle of Medieval Myth & Magic 
> http://www.todbe.com/
> -/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My 
> Perl/Lakers stuff)
> _______________________________________________
> Perl-Win32-Users mailing list 
> Perl-Win32-Users@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> 
> _______________________________________________
> Perl-Win32-Users mailing list 
> Perl-Win32-Users@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
> 

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to