user2896215
user2896215

Reputation: 527

set sockopt perl not working

I am using following code from perldoc website to set TCP nodelay to 1 (nagle algo disable)

#!/usr/bin/perl

use IO::Socket;
use IO::Socket::INET;
use Socket;
use Socket qw(IPPROTO_TCP TCP_NODELAY);
use IO::Socket qw(:DEFAULT :crlf);
use Socket qw(:all);

$/ = CRLF;

my $socket = IO::Socket::INET -> new (Proto => 'tcp', PeerAddr => 'www.example.com', PeerPort => 'http');
setsockopt($socket,IPPROTO_TCP,TCP_NODELAY,0);
my $packed = getsockopt($socket, $tcp, TCP_NODELAY);
my $nodelay = unpack("I",$packed);
print "Nagle's algorithm turned ",$nodelay ? "off\n":"on\n";

But in my case, everytime output is "Nagle algorithm is turned on" regardles of TCP nodelay value set to 1 or 0. How can i turn if off?

Upvotes: 0

Views: 246

Answers (1)

afenster
afenster

Reputation: 3608

That's because the line which you copied from the example contains the variable $tcp which is undefined in your code. Change the getsockopt line to

my $packed = getsockopt($socket, IPPROTO_TCP, TCP_NODELAY);

and it will work.

You should have used use strict; in your code, it would have caught this error.

Upvotes: 2

Related Questions