Click here to get back home

sample client server socket issue

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
sample client server socket issue jm 03-06-2008
Posted by jm on March 6, 2008, 5:35 pm
Please log in for more thread options
I have written some perl client program and javascript server program.

Communication seams working in both direction, but the perl client
cannot read final data.

Perl use socket in blocking mode (default).
I open the connection with localhost, port number, and tcp.


When I read packets with a size of 512 or 4096, everything works fine
till the last packet. As the last packet is smaller than 4096, perl stay
blocked waiting for a complete packet.

I just want it to be blocked when no byte is available.

When I do not use a size of 512 or 4096, but a size of 1, everything
works fine, but I am wondering if it will not be too much slow.

my $buffer = '';
while ( not $buffer =~ /\n\n/ )
{
my $packet;
sysread $sock, $packet, 4096 ;
$buffer .= $packet ;
}



NB: I have tried different technics to read data:
$sock->read
$sock->recv
sysread $sock
and so on. This does not solve the problem.

Posted by Ben Morrow on March 6, 2008, 6:03 pm
Please log in for more thread options

> I have written some perl client program and javascript server program.
>
> Communication seams working in both direction, but the perl client
> cannot read final data.
>
> Perl use socket in blocking mode (default).
> I open the connection with localhost, port number, and tcp.
>
> When I read packets with a size of 512 or 4096, everything works fine
> till the last packet. As the last packet is smaller than 4096, perl stay
> blocked waiting for a complete packet.

When you say 'last packet', do you mean 'last packet before the
connection is closed' or 'last packet in this request'? If you mean the
former, then Perl will return an incomplete packet even in blocking
mode. If the latter, then you need to set the socket to non-blocking
mode, and be prepared to handle short reads all the way through. You
will also need to use IO::Select to wait for data after you get a short
read.

Ben


Posted by jm on March 6, 2008, 6:57 pm
Please log in for more thread options
Ben Morrow a écrit :
>> I have written some perl client program and javascript server program.
>>
>> Communication seams working in both direction, but the perl client
>> cannot read final data.
>>
>> Perl use socket in blocking mode (default).
>> I open the connection with localhost, port number, and tcp.
>>
>> When I read packets with a size of 512 or 4096, everything works fine
>> till the last packet. As the last packet is smaller than 4096, perl stay
>> blocked waiting for a complete packet.

> When you say 'last packet', do you mean 'last packet before the
> connection is closed' or 'last packet in this request'?

I mean the server send several lines of text, then an empty line.
Then the server wait for the client.

All theses lines of text are assembled by network stacks in buffers of
4096 bytes, but last characters wont.



> If you mean the
> former, then Perl will return an incomplete packet even in blocking
> mode.


> If the latter, then you need to set the socket to non-blocking
> mode, and be prepared to handle short reads all the way through. You
> will also need to use IO::Select to wait for data after you get a short
> read.
>
> Ben
>

I was believing blocking mode better than non blocking!

Posted by Abigail on March 6, 2008, 10:58 pm
Please log in for more thread options
_
jm (jm@nospam.fr) wrote on VCCCI September MCMXCIII in
!!
!! I was believing blocking mode better than non blocking!


If you think this is universal, why do you think there's non-blocking mode?



Abigail
--
A perl rose: perl -e '@}>-`-,-`-%-'

Posted by xhoster on March 6, 2008, 6:58 pm
Please log in for more thread options
...
> > When I read packets with a size of 512 or 4096, everything works fine
> > till the last packet. As the last packet is smaller than 4096, perl
> > stay blocked waiting for a complete packet.
>
> When you say 'last packet', do you mean 'last packet before the
> connection is closed' or 'last packet in this request'? If you mean the
> former, then Perl will return an incomplete packet even in blocking
> mode. If the latter, then you need to set the socket to non-blocking
> mode,

I have never seen this behavior with sysread. The only time I've seen
sysread block is when the alternative is to read 0 bytes (and of course
not even then when set to non-blocking). It happily does short non-zero
length reads when the alternative is blocking, even without setting it to
non-blocking mode.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Similar ThreadsPosted
Naive Unix Socket client August 7, 2005, 5:08 am
Client socket programming eating up CPU/memory April 12, 2005, 6:31 am
IO::Socket::SSL : $sock is not defined if client is not SSL (crash) December 18, 2005, 11:26 am
Server/client over a network September 19, 2006, 1:54 pm
gluing client and server March 13, 2007, 10:30 am
POP3 Mail Client in PERL using IO::Socket module only and regular expressions April 11, 2006, 5:46 pm
stalling server client program January 5, 2007, 8:37 pm
SOAP server & client problem November 8, 2007, 1:10 pm
SOAP server & client problem November 8, 2007, 3:05 pm
Perl server, SOAP and .NET client, how to communicate August 19, 2005, 3:26 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap