SCOUG-Programming Mailing List Archives
Return to [ 31 |
May |
1998 ]
<< Previous Message <<
Content Type: text/plain
For those that are anxious to jump into TCP/IP programming, Greg's example is a good start.
However, given the diversity we have in the group, I think I'm going to try a slightly slower, more
deliberate approach.
So let's start writing GetLine(). Again, I'm going to go through the process one might go through
writing this for the first time - mistakes and all.
Please point out mistakes, differences in ideology, etc.
The purpose of GetLine() is to retrieve one line of text from the remote session. Its prototype is:
char *GetLine (int Socket)
Aside: Something I haven't mentioned is that Sockets are represented by integers. It might be a
good idea to typedef it to a new type such as SOCKET so that the compiler could catch potential
errors - but I didn't. C++ programmers will quickly want to wrap this.
GetLine() will use it's own static buffer and return a pointer into it.
The basic call we will use is recv() (which is used in Greg's client.c). It takes four parameters the
socket identifier, the pointer to the buffer in which to place date, the maximum number of bytes to
put in the buffer, and a flag parameter. recv() returns the number of bytes read.
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.
Stay tunned tomorrow for the next stumble, err I mean step. Or propose your own!
=====================================================
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".
=====================================================
<< Previous Message <<
Return to [ 31 |
May |
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.
|