xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

INetSetPeriodCallback()

Associates callback information with a socket.

Syntax

INetSetPeriodCallback( <pSocket>, ;
                       <aExecutableArray> ) --> NIL

Arguments

<pSocket>
This is a pointer to a socket created from any socket creation function.
<aExecutableArray>
A one dimensional array holding executable data must be passed as second parameter. Refer to function HB_ExecFromArray() for more information on executable arrays.

Return

The return value is always NIL.

Description

The function associates an executable array with a socket. The executable data stored in the array is automatically processed when a blocking sockets operation is called and a timeout period defined with INetSetTimeout() has elapsed. This way, a callback function can be called while a blocking sockets operation is not complete. Blocking sockets operations are INetAccept(), INetRecv() or INetDGramRecv().

The callback function can be used to display progress information or "still alive" messages until the blocking function has returned.

Info

See also:HB_ExecFromArray(), INetGetPeriodCallback(), INetCreate(), INetSetTimeout()
Category: Internet functions , Sockets functions , xHarbour extensions
Source:vm\INet.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example demonstrates how a callback can be installed before
// data is received from a HTTP server. The callback procedure Progress()
// displays a dot after each timeout period has elapsed while the program
// downloads data from the server.

   PROCEDURE Main
      LOCAL pSocket, 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 )

      IF nBytes == Len( cRequest )
         // timeout is 0.1 second
         INetSetTimeout( pSocket, 10 )

         // pass callback information as an executable array
         INetSetPeriodCallback( pSocket, { @Progress() } )

         // get HTTP response from server
         cResponse := ""

         DO WHILE INetCount( pSocket) > 0
            // a rather small buffer to get some output from Progress()
            cBuffer   := Space( 512 )
            nBytes    := INetRecv( pSocket, @cBuffer )
            cResponse += Left( cBuffer, nBytes )
         ENDDO

         INetClearPeriodCallback( pSocket )
         INetClearTimeout( pSocket )
      ELSE
         cResponse := "error: " + INetErrorDesc( pSocket )
      ENDIF

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

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

   PROCEDURE Progress
      // displaying a dot as progress is sufficient for the example
      ?? "."
   RETURN

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