xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

INetSend()

Sends data to a socket.

Syntax

INetSend( <pSocket>, <cData>, [<nBytes>] ) --> nBytesSent

Arguments

<pSocket>
This is a pointer to a socket as returned by INetConnect() or INetAccept().
<cData>
This is a character string holding the data to send to the socket. It must have at least <nBytes> characters.
<nBytes>
This is a numeric value specifying the number of bytes to transfer to the socket. It defaults to Len(<cData>).

Return

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().

Description

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.

Info

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

Example

// 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

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