<< Previous | Index | Next >>

sock_read


int sock_read( tcp_Socket *s, byte *dp, int len );

Description

Reads up to len bytes from dp on socket s. This function will busy wait until either len bytes are read or there is an error condition. If sock_yield() has been called, the user-defined function that is passed to it will be called in a tight loop while sock_read() is busy waiting.

Starting with Dynamic C 7.05, this function is only valid for TCP sockets. For UDP sockets, use udp_recv() or udp_recvfrom(). Prior to 7.05, this function cannot be used on UDP sockets after sock_recv_init() is called.

Parameters

s

Pointer to a socket.

dp

Buffer to store bytes that are read.

len

Maximum number of bytes to write to the buffer.

Return Value

0: Success, number of bytes read..
-1: Error.

Library

TCP.LIB (Prior to DC 7.05, this was DCRTCP.LIB)

See Also

sock_fastread, sock_fastwrite, sock_write, sockerr, udp_recv, udp_recvfrom

Example

Note that sock_fastread() and sock_read() do not necessarily return a complete or single line--they return blocks of bytes. In comparison, sock_getc() returns a single byte at a time and thus yields poor performance.


do {
len = sock_bytesready(s);
if (len > 0) {
if (len > sizeof( buffer) - 1)  //
If too many bytes, read some
len = sizeof( buffer ) - 1;  // now, read the rest next time.

    sock_read( s, buffer, len);

    buffer[len] = 0;
printf( "%s", buffer);
}
} while ( tcp_tick(s));

TCP/IP Manual
Vol 1
<<Previous | Index | Next>> rabbit.com