| xHarbour Reference Documentation > Function Reference |
![]() |
![]() |
![]() |
Establishes a sockets connection to a server.
INetConnect( <cURL>, <nPort> ) --> pSocket or INetConnect( <cURL>, <nPort>, <pSocket> ) --> NIL
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.
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.
| 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 |
// 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
http://www.xHarbour.com