SCOUG-Programming Mailing List Archives
Return to [ 02 |
June |
1998 ]
>> Next Message >>
Content Type: text/plain
-----BEGIN PGP SIGNED MESSAGE-----
On Sun, 31 May 1998 21:54:48 PST8PDT, Rollin White wrote:
>The original version of GetLine() was simply this:
>
>char *GetLine(int Socket){
>
> static char Buffer[4096];
> int rc;
>
> rc = recv (Socket, Buffer, sizeof (Buffer), 0);
>
> if (rc > 0){
> return &Buffer;
> } else {
> return NULL;
> }
>}
>
>
>And it worked! But I soon discovered it was not always reading a complete line of data. recv()
>returns whatever data is available. It worked because I was testing it locally, so almost always, all of
>the data was available. But as I started to test it on a different (slower) machine, the complete line
>was not always available. So clearly I was going to have to be more inteligent about the buffer
>management.
OK, I'll take the next stab at it. Since we know how many characters were read,
the really simple way to see if the line is complete is to look at the last character
in the string. If necessary, then make another call to recv(). In this case we can
modify GetLine() as follows:
char *GetLine(int Socket){
static char Buffer[4096];
int rc;
int offset;
rc = recv (Socket, Buffer, sizeof (Buffer), 0);
if (rc > 0){
if ( '\n' == Buffer[rc-1] ) {
return &Buffer;
} else {
offset = rc;
rc = recv (Socket, Buffer+offset, sizeof (Buffer)-offset, 0);
return &Buffer;
}
} else {
return NULL;
}
}
There is a really obvious problem here. And I have also done some pointer
manipulations to keep everything in the one declared Buffer.
===============================================================
Gregory W. Smith (WD9GAY) gsmith@well.com
finger gsmith@well.com for PGP public key
-----BEGIN PGP SIGNATURE-----
Version: 2.6.2
iQCVAwUBNXQjDDtML4mvizAhAQHIZAQAtlo1qiC9WLJmdJX1NCz++QZbZCwbGGI/
J6mk6N9jeigUXIX1EGrJT0cd5YluxHPi+PoSaJdMbeehvJqiKTZzd/gCj3iZVUbD
nWNXkxix/CUodwwLY2ivHdD5SzjJ+Aez21sRV+/CXe5SskVe16FwUJNOC/KTAWmg
/6oRqJUwxso=
=HdqO
-----END PGP SIGNATURE-----
=====================================================
To unsubscribe from this list, send an email message
to "steward@scoug.com". In the body of the message,
put the command "unsubscribe scoug-programming".
For problems, contact the list owner at
"rollin@scoug.com".
=====================================================
>> Next Message >>
Return to [ 02 |
June |
1998 ]
The Southern California OS/2 User Group
P.O. Box 26904
Santa Ana, CA 92799-6904, USA
Copyright 2001 the Southern California OS/2 User Group. ALL RIGHTS
RESERVED.
SCOUG, Warp Expo West, and Warpfest are trademarks of the Southern California OS/2 User Group.
OS/2, Workplace Shell, and IBM are registered trademarks of International
Business Machines Corporation.
All other trademarks remain the property of their respective owners.
|