| xHarbour Reference Documentation > Function Reference |
![]() |
![]() |
![]() |
Reads data from a socket.
INetRecv( <pSocket>, @<cBuffer>, [<nBytes>] ) --> nBytesRead
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().
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.
| 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 |
// 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
http://www.xHarbour.com