Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > IO::Socket::INET Problem

Reply
Thread Tools

IO::Socket::INET Problem

 
 
Hobbit HK
Guest
Posts: n/a
 
      02-09-2004
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.
 
Reply With Quote
 
 
 
 
kz
Guest
Posts: n/a
 
      02-09-2004
"Hobbit HK" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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



 
Reply With Quote
 
 
 
 
kz
Guest
Posts: n/a
 
      02-09-2004
"Hobbit HK" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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




 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      02-09-2004

http://www.velocityreviews.com/forums/(E-Mail Removed) (Hobbit HK) wrote:
> 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. 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

--
'Deserve [death]? I daresay he did. Many live that deserve death. And some die
that deserve life. Can you give it to them? Then do not be too eager to deal
out death in judgement. For even the very wise cannot see all ends.'
:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-:-: (E-Mail Removed)
 
Reply With Quote
 
Hobbit HK
Guest
Posts: n/a
 
      02-10-2004
Ben Morrow <(E-Mail Removed)> wrote in message news:<c085k2$h2n$(E-Mail Removed)>...
> (E-Mail Removed) (Hobbit HK) wrote:
> > 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.
>


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...

> > 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. 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"?

>You may also, as someone else said, want to use
> IO::Select.


> Ben

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      02-10-2004

(E-Mail Removed) (Hobbit HK) wrote:
> Ben Morrow <(E-Mail Removed)> wrote in message
> news:<c085k2$h2n$(E-Mail Removed)>...
> > 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...


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

> > > 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. 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)?


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

--
perl -e'print map {/.(.)/s} sort unpack "a2"x26, pack "N"x13,
qw/1632265075 1651865445 1685354798 1696626283 1752131169 1769237618
1801808488 1830841936 1886550130 1914728293 1936225377 1969451372
2047502190/' # (E-Mail Removed)
 
Reply With Quote
 
Hobbit HK
Guest
Posts: n/a
 
      02-12-2004
Ben Morrow <(E-Mail Removed)> wrote in message news:<c0am1u$727$(E-Mail Removed)>...
> > 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.
>


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

> > > > 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. push a :unix (NB. I
> > > have never tried this and, though it ought to work, I don't really
> > > know... .

[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...
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments