| xHarbour Reference Documentation > Function Reference |
![]() |
![]() |
![]() |
Sends data to a socket.
INetSend( <pSocket>, <cData>, [<nBytes>] ) --> nBytesSent
The function returns a numeric value indicating the number of bytes sent to the socket. If the return value is smaller than <nBytes>, either no more bytes can be transmitted, or a network error ocurred. This can be identified using function INetErrorCode().
Function INetSend() sends the number of <nBytes> bytes of <cData> from the local computer to the connected socket <pSocket>.
If the function succeds, it returns the number of bytes that are actually transmitted. If the socket is closed while sending data, the return value is 0, and -1 on error.
Note: there is no guarantee that all the data requested to be sent is actually sent to the socket. Therefore, the return value should be compared with the <nBytes> and, if a smaller value is returned, INetSend() should be called iteratively to transmit the remaining portions of <cData> until all data is sent.
Alternatively, data can be sent with the INetSendAll() function. It ensures all data is transmitted before the function returns.
| See also: | INetAccept(), INetConnect(), INetErrorCode(), INetRecv(), INetRecvAll(), INetRecvLine(), 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
// The HTTP request is sent with INetSend(), and the server response is
// collected with INetRecv().
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