xHarbour Reference Documentation > Function Reference |
Associates callback information with a socket.
INetSetPeriodCallback( <pSocket>, ; <aExecutableArray> ) --> NIL
The return value is always NIL.
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.
See also: | HB_ExecFromArray(), INetGetPeriodCallback(), INetCreate(), INetSetTimeout() |
Category: | Internet functions , Sockets functions , xHarbour extensions |
Source: | vm\INet.c |
LIB: | xhb.lib |
DLL: | xhbdll.dll |
// 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
http://www.xHarbour.com