OPENCLIENT: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
The [[_OPENCLIENT]] function connects to a Host on the Internet as a Client and returns the Client status handle.
{{DISPLAYTITLE:_OPENCLIENT}}
The '''_OPENCLIENT''' function connects to a Host on the Internet as a Client and returns the Client status handle.


{{Text|'''HTTP functionality is current unstable, and requires [[$UNSTABLE]]:HTTP to be able to use.'''|red}}
{{Text|'''HTTP functionality is current unstable, and requires [[$UNSTABLE]]:HTTP to be able to use.'''|red}}


{{PageSyntax}}
{{PageSyntax}}
Line 12: Line 14:
*Connects to a host somewhere on the internet as a client.
*Connects to a host somewhere on the internet as a client.
*Valid {{Parameter|clientHandle&}} values are negative. 0 means that the connection failed. Always check that the handle returned is not 0.
*Valid {{Parameter|clientHandle&}} values are negative. 0 means that the connection failed. Always check that the handle returned is not 0.
*[[CLOSE]] client_handle closes the client. A failed handle of value 0 does not need to be closed.
*[[CLOSE]] clientHandle& closes the client. A failed handle of value 0 does not need to be closed.





Revision as of 22:38, 6 January 2023

The _OPENCLIENT function connects to a Host on the Internet as a Client and returns the Client status handle.

HTTP functionality is current unstable, and requires $UNSTABLE:HTTP to be able to use.


Syntax

clientHandle& = _OPENCLIENT("TCP/IP:8080:12:30:1:10")
clientHandle& = _OPENCLIENT("HTTP:url")


Description

  • An Illegal Function Call error will be triggered if the function is called with a string argument of the wrong syntax.
  • Connects to a host somewhere on the internet as a client.
  • Valid clientHandle& values are negative. 0 means that the connection failed. Always check that the handle returned is not 0.
  • CLOSE clientHandle& closes the client. A failed handle of value 0 does not need to be closed.


Examples

Example 1: Attempting to connect to a local host(your host) as a client. A zero return indicates failure.

client = _OPENCLIENT("TCP/IP:7319:localhost")
IF client THEN
   PRINT "[Connected to " + _CONNECTIONADDRESS(client) + "]"
ELSE PRINT "[Connection Failed!]"
END IF  
NOTE: Try a valid TCP/IP port setting to test this routine!

Example 2: Using HTTP to download from a URL.

' Content of the HTTP response is returned. The statusCode is also assigned.
FUNCTION Download$(url As STRING, statusCode As LONG)
    h& = _OPENCLIENT("HTTP:" + url)

    statusCode = _STATUSCODE(h)

    WHILE NOT EOF(h&)
        _LIMIT 60
        GET #h&, , s$
        content$ = content$ + s$
    WEND

    CLOSE #h&

    Download$ = content$
END FUNCTION


See also


Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link