IO::Socket help


V

Vespasian

Why does the following code correctly sets up a server

my $port = 9850;
my $proto = getprotobyname('tcp');
socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket:
$!";
setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die
"setsock: $!";
my $paddr = sockaddr_in($port, INADDR_ANY);
bind(SERVER, $paddr) or die "bind: $!";
listen(SERVER, SOMAXCONN) or die "listen: $!";
print "SERVER started on port $port\n";

# accepting a connection
my $client_addr;
while ($client_addr = accept(CLIENT, SERVER))


while the following does not i.e. it dies at the die statement,

my $port = 9850;
my $proto = getprotobyname('tcp');


my $sock = new IO::Socket::INET(
LocalHost => 'localhost',
LocalPort => 9850,
Proto => 'tcp',
Listen => SOMAXCONN,
Type => SOCK_STREAM,
Reuse => 1);

$sock or die "no socket :$!";


print "SERVER started on port $port\n";

# accepting a connection
my $client_sock = $sock->accept();


TIA,

Ves
 
Ad

Advertisements

S

Sisyphus

"Vespasian" <[email protected]>
..
..
while the following does not i.e. it dies at the die statement,

use warnings;
use strict;
use IO::Socket;
my $port = 9850;
my $proto = getprotobyname('tcp');


my $sock = new IO::Socket::INET(
LocalHost => 'localhost',
LocalPort => 9850,
Proto => 'tcp',
Listen => SOMAXCONN,
Type => SOCK_STREAM,
Reuse => 1);

$sock or die "no socket :$!";


print "SERVER started on port $port\n";

# accepting a connection
my $client_sock = $sock->accept();

Don't know what the problem is. Works fine for me - prints "SERVER started
on port 9850" and just sits there waiting for a connection.
Don't forget to 'use warnings;'.

Cheers,
Rob
 
X

xhoster

Vespasian said:
Why does the following code correctly sets up a server
....

while the following does not i.e. it dies at the die statement,

I'm guessing that the reason that it dies at the die statement would
have be reflected in the contents of $! at the die statement. Why
have you concealed this from us?

Xho
 
A

A. Sinan Unur

I'm guessing that the reason that it dies at the die statement would
have be reflected in the contents of $! at the die statement. Why
have you concealed this from us?

Take this with a grain of salt, but, IIRC correctly, [email protected] tends to be more
informative in IO::Socket related errors.

Sinan
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top