IO::Socket::INET Problem

H

Hobbit HK

I'm using WinXP (although I also have RH Linux and there's no
difference) and I want to write a script which uses sockets... Now, my
main problem is when there's nothing else to read from the socket...
That's where my script stucks... My guess is that because it keeps on
waiting for something (it can still get data)... Is there some way to
bypass this?
Will Blocking=>0 in IO::Socket::INET's new() help?

BTW
I'm using $line=<$sock> to read from the socket.
 
K

kz

Hobbit HK said:
I'm using WinXP (although I also have RH Linux and there's no
difference) and I want to write a script which uses sockets... Now, my
main problem is when there's nothing else to read from the socket...
That's where my script stucks... My guess is that because it keeps on
waiting for something (it can still get data)... Is there some way to
bypass this?
Will Blocking=>0 in IO::Socket::INET's new() help?

BTW
I'm using $line=<$sock> to read from the socket.

Your example seems to be blocking.
Snippet below does not exactly cover your case, worked in mine, though.
Calling the can_read method with no args was blocking. Specifying 500 ms
wasn't.

my $listen = IO::Socket::INET->new(Listen => 5, LocalAddr => 'localhost',
LocalPort => 12300, Proto => 'tcp') or die "listen: $!";
my $select = new IO::Select() or die "Can't setup main selector: $!";
$select->add($listen);
while (1) {
my @ready = $select->can_read(500); # magic
foreach my $endpt (@ready) {
# do whatever needs to be done
} }

HTH,

Zoltan
 
K

kz

Hobbit HK said:
I'm using WinXP (although I also have RH Linux and there's no
difference) and I want to write a script which uses sockets... Now, my
main problem is when there's nothing else to read from the socket...
That's where my script stucks... My guess is that because it keeps on
waiting for something (it can still get data)... Is there some way to
bypass this?
Will Blocking=>0 in IO::Socket::INET's new() help?

BTW
I'm using $line=<$sock> to read from the socket.

Your example seems to be blocking.
Snippet below does not exactly cover your case, worked in mine, though.
Calling the can_read method with no args was blocking. Specifying 500 ms
wasn't.

my $listen = IO::Socket::INET->new(Listen => 5, LocalAddr => 'localhost',
LocalPort => 12300, Proto => 'tcp') or die "listen: $!";
my $select = new IO::Select() or die "Can't setup main selector: $!";
$select->add($listen);
while (1) {
my @ready = $select->can_read(500); # magic
foreach my $endpt (@ready) {
# do whatever needs to be done
} }

HTH,

Zoltan
 
B

Ben Morrow

I'm using WinXP (although I also have RH Linux and there's no
difference) and I want to write a script which uses sockets... Now, my
main problem is when there's nothing else to read from the socket...
That's where my script stucks... My guess is that because it keeps on
waiting for something (it can still get data)... Is there some way to
bypass this?

What do you want it to do in that case? Stop? It is usual for socket
programs to either wait until the other end closes the socket (which
will come out as reading EOF) or to close it yourself on some
condition, ie. during the processing immediately after having read something.
Will Blocking=>0 in IO::Socket::INET's new() help?
I'm using $line=<$sock> to read from the socket.

Might do. It won't if you use buffered reads, though, so you either
want to switch to sysread or (if you have 5.8) push a :unix (NB. I
have never tried this and, though it ought to work, I don't really
know... :). You may also, as someone else said, want to use
IO::Select.

Ben
 
H

Hobbit HK

Ben Morrow said:
What do you want it to do in that case? Stop? It is usual for socket
programs to either wait until the other end closes the socket (which
will come out as reading EOF) or to close it yourself on some
condition, ie. during the processing immediately after having read something.

Yes, I want it to stop reading and move on... After a while when no
data has arrived to just stop reeading... It's kinda annoying with
IO::Select because it still waits the number of seconds I told it,
even if there's data...
Might do. It won't if you use buffered reads, though, so you either
want to switch to sysread or (if you have 5.8) push a :unix (NB. I
have never tried this and, though it ought to work, I don't really
know... :).

What do you mean by buffer reads? read($sock, $buffer, 1000)?
And "push a :unix"?
 
B

Ben Morrow

Yes, I want it to stop reading and move on... After a while when no
data has arrived to just stop reeading... It's kinda annoying with
IO::Select because it still waits the number of seconds I told it,
even if there's data...

No it doesn't. It should return when there's data, or when the timeout
has expired, whichever is the sooner.
What do you mean by buffer reads? read($sock, $buffer, 1000)?

Yup. Use sysread instead.
And "push a :unix"?

If you are using 5.8 and built your perl with PerlIO,

binmode $FH, ':unix';

*ought* to make all reads on that FH unbuffered, even those done with
<>... as I said, though, I've not tried it, and I wouldn't be
surprised if it didn't work. See perldoc PerlIO.

Ben
 
H

Hobbit HK

Ben Morrow said:
No it doesn't. It should return when there's data, or when the timeout
has expired, whichever is the sooner.

Right... I don't know why it did before (or why I thought it did)...
[snip]

binmode $FH, ':unix';

*ought* to make all reads on that FH unbuffered, even those done with
<>... as I said, though, I've not tried it, and I wouldn't be
surprised if it didn't work. See perldoc PerlIO.

Ben

binmode $sock, ':unix' didn't work... At least I saw no change...
I hope IO::Selcet will do the trick...
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top