Re: Lost data on socket - Can we start over politely?

From: Thomas Kratz (ThomasKratz_at_REMOVEwebCAPS.de)
Date: 03/30/04


Date: Tue, 30 Mar 2004 18:27:41 +0200

Vorxion wrote:

> In light of some uh...disagreements, I've bent a bit. Hopefully it's taken
> as a token of goodwill. I could use the help, I'm willing to bend a bit to
> do it. SO...
>
> I've reworked this to both make it shorter, and so that my extra curly
> brackets are not present. I also implemented strict and warnings.
>
> In short, connect with anything and toss, say... 4000 thousand lines of
> input at the server. It should give up the ghost far early--somewhere
> around 1120-2200, depending on the phase of the moon. You'll note that if
> you're still connected, you can even send more data. It's purely a
> buffering issue.
>
> I've switched to using only buffered reads and writes on the socket fd's,
> so that's no longer the issue. I'm sure it was contributing, but nothing
> I'm doing here should be affecting it, to the best of my knowledge.
>
> Now that I've cleaned up my code (at least the majority) and am trying to
> appease some people, could I please ask for assistance? (Yes, there are a
> lot of variables in the our() statements that aren't used here...I trimmed
> about 1000 lines of code out this time instead of ~800.)
>
> If you just run it and toss input at it that's newline-separated, you'll
> see what I mean. I'm tossing 3900+ lines at it. It gives up at 1123
> somtimes, 2316 others, etc. Entirely arbitrary. It's not buffering
> corretly, but I know not why at this point.

[ code snipped ]

Sorry, but the code is still too cluttered to debug it. I have tried,
really, for about 45 minutes, then I gave up.

Instead please try this server ( some quickly reduced code from a bigger
server ) and mini client. If this works for you, you could extend it.
I don't loose any data with this setup. And I don't even bother to set
unbuffered mode (tested on Win32).

If you look at the server code, you'll get an idea what is meant by "a
small but complete sample to reproduce the problem". The base
functionality you need is in there. And if I were able to understand, what
your code tries to do, I would be able to shorten it to that level too.

Regards
Thomas

++++ server ++++

use strict;
use warnings;

use IO::File;
use IO::Select;
use IO::Socket;

my $max_buf = 10240;

my $sel = IO::Select->new();

my $srv = IO::Socket::INET->new(
      Proto => 'tcp',
      LocalPort => 4016,
      Listen => SOMAXCONN,
      ReuseAddr => 1,
);

$sel->add($srv);

die "couldn't create socket" unless $srv;

my $log = IO::File->new('server.log', '>') or die $!;
$log->autoflush(1);

while ( 1 ) {

    foreach my $sock ( $sel->can_read(0.05) ) {

       if ( $sock == $srv ) {

          my $new = $srv->accept();
          $new->autoflush(1);
          print $log "new connection from ", $new->peerhost,
                ':', $new->peerport, "\n";
          $sel->add($new);

          next;
       }

       my $buf = '';
       my $clsel = IO::Select->new($sock);
       while ( $clsel->can_read(0.05) ) {
          last unless $sock->sysread($buf, 1024, length($buf))
             and (length($buf) <= $max_buf);
       }

       unless ( $buf ) {
          print $log "error or close on socket\n";
          $sock->close();
          $sel->remove($sock);
          next;
       }

       print $log "\n", length($buf), " bytes read\n";
       print $log $buf;
    }
}

++++ client ++++

use strict;
use warnings;

use IO::Socket;

my $sock = IO::Socket::INET->new(
    PeerAddr => '235p008',
    PeerPort => 4016,
    Proto => 'tcp',
) or die "couldn't create socket";

my $i = 0;
while ( $sock->connected() ) {
    last unless print $sock $i, 'x' x (100-length($i++)), "\n";
}

-- 
open STDIN,"<&DATA";$=+=14;$%=50;while($_=(seek( #J~.> a>n~>>e~.......>r.
STDIN,$:*$=+$,+$%,0),getc)){/\./&&last;/\w| /&&( #.u.t.^..oP..r.>h>a~.e..
print,$_=$~);/~/&&++$:;/\^/&&--$:;/>/&&++$,;/</  #.>s^~h<t< ..~. ...c.^..
&&--$,;$:%=4;$,%=23;$~=$_;++$i==1?++$,:_;}__END__#....>>e>r^..>l^...>k^..


Relevant Pages

  • Lost data on socket - Can we start over politely?
    ... input at the server. ... I've switched to using only buffered reads and writes on the socket fd's, ... It's not buffering ... sub get_packet { ...
    (comp.lang.perl.misc)
  • Sockets, Performance, Messaging, NetworkStream
    ... I have implemented async socket server & client, ... line of buffering and then actually write the data into the socket. ... Current performance is about 8 messages per second where server sends to ...
    (microsoft.public.dotnet.framework.performance)
  • using pthread to receive data on a socket and simultaneously
    ... I am writing a client for a real time audio streaming server. ... I have one thread receiving data on a socket from ... After buffering some data, other thread wakes up (checking how ...
    (comp.unix.programmer)
  • Re: How to terminate a socket in CLOSE_WAIT state
    ... FTP Server fixed for certain FTP clients who use both passive ... This was causing a PASSIVE opened socket to be left ... but instead expect the "half close" from the receiver. ... this sends a TCP/IP FIN packet to the ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Socket switch delay
    ... both at the client and at the server (and why ... would you set the send buffer size to zero on a non-overlapped ... One glaring error is your client does ... So when you use a single socket, ...
    (microsoft.public.win32.programmer.networks)