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 Ben Morrow on March 6, 2008, 9:22 pm
Please log in for more thread options

Quoth xhoster@gmail.com:
> ...
> > > 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.

Yes, you would seem to be correct, at least here (FreeBSD). I don't know
what's going on for the OP, then.

Ben


Posted by Jim Gibson on March 7, 2008, 12:56 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 ;
> }

How do you know the problem is not with your server? We can't tell,
since you have not shown us your code. You should 1) capture the return
value of sysread and check it for a) the number of bytes read, b) a 0
if the socket has been closed, or c) undef if an error has occurred,
and 2) print the return value and the bytes read, if any.

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Posted by jm on March 7, 2008, 6:05 pm
Please log in for more thread options
Jim Gibson 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.
>>
>> 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 ;
>> }
>
> How do you know the problem is not with your server? We can't tell,
> since you have not shown us your code. You should 1) capture the return
> value of sysread and check it for a) the number of bytes read, b) a 0
> if the socket has been closed, or c) undef if an error has occurred,
> and 2) print the return value and the bytes read, if any.


When debugging the code, I had replaced sysread, by recv and read methods.

I moved back to the sysread, and it looks like working fine.

However, I am not sure documentation clearly explain why $sock->send for
sending, and why sysread for reading...

thanks.


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

>
> When debugging the code, I had replaced sysread, by recv and read methods.
>
> I moved back to the sysread, and it looks like working fine.
>
> However, I am not sure documentation clearly explain why $sock->send for
> sending, and why sysread for reading...

$sock->send with at most two arguments (plus $sock itself) ends up
calling send(2), which (under Unix, at any rate) is exactly equivalent
to write(2), which is what syswrite calls. $sock->send with three
arguments calls sendto(2), which is quite different; also, I believe
there are operating systems where send(2) and write(2) are different,
though I think perl deals with that for you if it needs to.

The same applies to recv/sysread. Perl's read, however, calls fread(3),
(or rather PerlIO's equivalent) which is buffered. Buffered reads do
behave exactly as you described: they will wait for ever (or until EOF,
or error) for a full buffer if the underlying file descriptor is in
blocking mode. So don't do that: stick to sysread or recv.

Alternatively, if you're using 5.8 you can push a :unix layer

binmode $sock, ':unix';

which will let you use <> and so on in unbuffered mode. It probably
isn't as efficient as doing your own buffering, but it's a lot more
convenient.

Ben


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