xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

INetRecv()

Reads data from a socket.

Syntax

INetRecv( <pSocket>, @<cBuffer>, [<nBytes>] ) --> nBytesRead

Arguments

<pSocket>
This is a pointer to a socket as returned by INetConnect() or INetAccept().
<cBuffer>
A memory variable holding a character string must be passed by reference to INetRecv(). It must have at least <nBytes> characters.
<nBytes>
This is a numeric value specifying the number of bytes to transfer from the socket into the memory variable <cBuffer>. It defaults to Len(<cBuffer>).

Return

The function returns a numeric value indicating the number of bytes read from the socket. If the return value is smaller than Len(<cBuffer>), either no more bytes are available, or a network error ocurred. This can be identified using function INetErrorCode().

Description

Function INetRecv() blocks the current thread, reads <nBytes> bytes from the socket <pSocket> and copies them into a memory variable. To accomplish this, INetRecv() must receive a string buffer <cBuffer>, large enough to receive the number of bytes specified with <nBytes>. Since the bytes are copied into a memory variable, the buffer variable must be passed by reference to INetRecv().

Note:  there is no guarantee that INetRecv() fills <cBuffer> with the specified number of bytes. The function is blocking until either no more data is available, the connection is lost or the socket is closed, whichever comes first.

Use INetRecvAll() to block the current thread until the whole <cBuffer> is filled or <nBytes> bytes are read.

Info

See also:INetAccept(), INetConnect(), INetErrorCode(), INetRecvAll(), INetRecvLine(), INetSend(), INetSendAll()
Category: Internet functions , Sockets functions , xHarbour extensions
Source:vm\INet.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example outlines a simple internet communication with a HTTP server

   PROCEDURE Main
      LOCAL pSocket, cBuffer, nBytes, cRequest, cResponse

      CLS

      // initialize sockets system and connect to server
      INetInit()
      pSocket := INetConnect( "www.xharbour.com", 80 )

      IF INetErrorCode( pSocket ) <> 0
         ? "Socket error:", INetErrorDesc( pSocket )
         INetCleanUp()
         QUIT
      ENDIF

      // send HTTP request to server
      cRequest := "GET / HTTP/1.1"                + INetCRLF() + ;
                  "Host: www.xharbour.com"        + INetCRLF() + ;
                  "User-Agent: HTTP-Test-Program" + INetCRLF() + ;
                   INetCRLF()

      nBytes   := INetSend( pSocket, cRequest )
      cResponse:= ""

      // get HTTP response from server
      DO WHILE nBytes > 0
         cBuffer   := Space( 4096 )
         nBytes    := INetRecv( pSocket, @cBuffer )
         cResponse += Left( cBuffer, nBytes )
      ENDDO

      // disconnect and cleanup memory
      INetClose( pSocket )
      INetCleanUp()

      // save response
      Memowrit( "xharbour.txt", cResponse )
      ? "Data written to file: xHarbour.txt"
   RETURN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe