xHarbour Reference Documentation > Function Reference xHarbour Developers Network  

INetConnect()

Establishes a sockets connection to a server.

Syntax

INetConnect( <cURL>, <nPort> ) --> pSocket

or

INetConnect( <cURL>, <nPort>, <pSocket> ) --> NIL

Arguments

<cURL>
This is a character string holding either the URL (DNS name) of the server to connect to, or its IP address in dotted notation (e.g. "192.168.1.145").
<nPort>
This is the numeric port number on the local computer to use for the socket connection. The default internet port is 80.
<pSocket>
Optionally, a socket created with INetCreate() or previously closed with INetClose() can be passed to establish a connection.

Return

If called with two parameters, the return value is a pointer to a socket. If a socket is passed as third parameter, the return value is NIL.

Description

Before INetConnect() can be called, the sockets system must be initialized with INetInit(). INetConnect() is a client side sockets function that establishes a connection to a server process. The server's internet address can be passed as the DNS name of the remote station or in dottet notation. If the connection is successfully established, the socket can be used for sending and receiving data to/from the server. If the connection fails, the cause of failure can be detected with INetErrorCode().

Note:  the function is not thread safe! It is the responsibility of the programmer to make sure that InetConnect() is not called simultaneously in two ore more threads, or that INetGetHosts() is not called at the same time as InetConnect().

Use INetConnectIP() for building thread safe connections, or protect a call to INetConnect() with a Mutex.

Info

See also:INetAccept(), INetCreate(), INetConnectIP(), INetDGram(), INetInit(), INetRecv(), INetSend(), INetServer()
Category: Internet functions , Sockets functions , xHarbour extensions
Source:vm\inet.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example outlines how a simple internet connection can be
// established for downloading a HTML page using the HTTP protocol.

   #define CRLF  Chr(13)+Chr(10)

   PROCEDURE Main
      LOCAL cBuffer, cRequest, cResponse, nBytes, pSocket

      // 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"                + CRLF + ;
                  "Host: www.xharbour.com"        + CRLF + ;
                  "User-Agent: HTTP-Test-Program" + CRLF + ;
                   CRLF

      nBytes   := INetSend( pSocket, cRequest )
      cBuffer  := Space(4096)
      cResponse:= ""

      // get HTTP response from server
      DO WHILE ( nBytes > 0 )
         nBytes    := INetRecv( pSocket, @cBuffer )
         cResponse += Left( cBuffer, nBytes )
         cBuffer   := Space(4096)
      ENDDO

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

      // save response and display
      Memowrit( "xharbour.txt", cResponse )
      Memoedit( cResponse )
   RETURN

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