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
 
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, $@ tends to be more
informative in IO::Socket related errors.

Sinan
 
V

Vespasian

Error reported: unknown error

(e-mail address removed) wrote in

Take this with a grain of salt, but, IIRC correctly, $@ tends to be more
informative in IO::Socket related errors.

Sinan
 
X

xhoster

top posting fixed. Please don't top post.
Error reported: unknown error

Is that the value of "$!" or of "$@"? Whichever it is, try the other
one to see if it more informative.

Xho
 

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

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,052
Latest member
KetoBeez

Latest Threads

Top