Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 14 Nov 1996 11:54:48 -0800
From:      John Polstra <jdp@polstra.com>
To:        "Marc G. Fournier" <scrappy@ki.net>
Cc:        hackers@freebsd.org
Subject:   Re: Sockets question... 
Message-ID:  <199611141954.LAA09643@austin.polstra.com>
In-Reply-To: Your message of "Thu, 14 Nov 1996 14:44:03 EST." <Pine.NEB.3.95.961114144229.5812I-100000@quagmire.ki.net> 
References:  <Pine.NEB.3.95.961114144229.5812I-100000@quagmire.ki.net> 

next in thread | previous in thread | raw e-mail | index | archive | help
> At 1024, data seems to be lost.  I send 1023 bytes across, and
> receive 4...send 1023, receive 907...I send across 100 packets,
> receive 2...
> 
> As soon as I go to 512 or 80 byte writes, I can pound at it repeatedly
> and get the complete image across every time, no errors.

I haven't seen your code, but do you realize that you can't just do a
single read?  E.g., if you're expecting to receive 1024 bytes, this
won't necessarily return all the data:

    read(s, buf, 1024);

If just part of the data has arrived, the read() call will return that,
and it will return the number of bytes it actually transferred, which
may be less than 1024.  To read all 1024 bytes, you'd have to do
something like this:

    char *ptr = buf;
    int nWant = 1024;
    int nGot = 0;

    while (nGot < nWant) {
	int n = read(s, ptr, nWant - nGot);
	if (n == -1)
	    /* Error */
	else if (n == 0)
	    /* EOF */
	else {
	    ptr += n;
	    nGot += n;
	}
    }

If you don't know how many bytes to expect, and just want to read it
all, then make the loop continue until read() returns a number <= 0.

In general, a read() on a socket will return as soon as _any_ data is
available -- even just 1 byte.

Could this be the problem?

John
--
   John Polstra                                       jdp@polstra.com
   John D. Polstra & Co., Inc.                Seattle, Washington USA
   "Self-knowledge is always bad news."                 -- John Barth



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199611141954.LAA09643>