int write(int s, void *buf, int nbytes)
|
Effect:
|
int TCPsend( int s, char *buf, int nBytes )
{
int k, n;
k = 0; // No bytes sent yet....
/* ------------------------------------------------------------
Call write( ) as long as we did not send nBytes....
------------------------------------------------------------- */
while ( k < nBytes )
{
n = write( s, &buf[k], nBytes-k ); // Send "remaining" nBytes-k bytes
/* ========================================
Check if there was an error....
======================================== */
if ( n == -1 )
return -1; // Return error....
k = k + n; // We sent n more bytes, add to k
}
return nBytes; // Success...
}
|
int read(int s, void *buf, int nbytes)
|
Effect:
|
int TCPrecv( int s, char *buf, int nBytes )
{
int k;
k = 0; // No bytes received yet....
/* ------------------------------------------------------------
Call read( ) as long as we did not receive nBytes....
------------------------------------------------------------- */
while ( k < nBytes )
{
n = read( s, &buf[k], nBytes-k ); // Receive "remaining" nBytes-k bytes
/* ========================================
Check if there was an error....
======================================== */
if ( n == -1 )
return -1; // Return error....
k = k + n; // We sent n more bytes, add to k
}
return nBytes;
}
|
int s; // TCP socket used to communicate
close(s);
|
|
The similarity is obvious in the behavior:
|