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.
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}}


{{PageSyntax}}
{{PageSyntax}}
:{{Parameter|clientHandle&}} = [[_OPENCLIENT]]('''"TCP/IP:8080:12:30:1:10"''')
:{{Parameter|clientHandle&}} = [[_OPENCLIENT]]('''"TCP/IP:8080:12:30:1:10"''')
:{{Parameter|clientHandle&}} = [[_OPENCLIENT]]('''"HTTP:url"''')




Line 17: Line 19:
{{CodeStart}}
{{CodeStart}}
client = {{Cl|_OPENCLIENT}}("TCP/IP:7319:localhost")
client = {{Cl|_OPENCLIENT}}("TCP/IP:7319:localhost")
{{Cl|IF...THEN|IF}} client {{Cl|THEN}}  
{{Cl|IF...THEN|IF}} client {{Cl|THEN}}
   {{Cl|PRINT}} "[Connected to " + {{Cl|_CONNECTIONADDRESS}}(client) + "]"  
   {{Cl|PRINT}} "[Connected to " + {{Cl|_CONNECTIONADDRESS}}(client) + "]"
{{Cl|ELSE}} {{Cl|PRINT}} "[Connection Failed!]"
{{Cl|ELSE}} {{Cl|PRINT}} "[Connection Failed!]"
{{Cl|END IF}} '' ''
{{Cl|END IF}} '' ''
Line 24: Line 26:
:'''NOTE:''' Try a valid TCP/IP port setting to test this routine!
:'''NOTE:''' Try a valid TCP/IP port setting to test this routine!


''Example 2:'' Using a "raw" Download function to download the QB64 bee image and displays it.
''Example 2:'' Using HTTP to download from a URL.
{{CodeStart}}
{{CodeStart}}
'replace the fake image address below with a real image address you want to download
' Content of the HTTP response is returned. The statusCode is also assigned.
{{Cl|IF...THEN|IF}} Download("www.qb64.org/qb64.png", "qb64logo.png", 10) {{Cl|THEN}} ' timelimit = 10 seconds
{{Cl|FUNCTION}} Download$(url As {{Cl|STRING}}, statusCode As {{Cl|LONG}})
{{Cl|SCREEN}} {{Cl|_LOADIMAGE}}("qb64logo.png",32)
    h& = {{Cl|_OPENCLIENT}}("HTTP:" + url)
{{Cl|ELSE}}: {{Cl|PRINT}} "Couldn't download image."
{{Cl|END IF}}
{{Cl|SLEEP}}
{{Cl|SYSTEM}}
' ---------- program end -----------


{{Cl|FUNCTION}} Download (url$, file$, timelimit) ' returns -1 if successful, 0 if not
     statusCode = {{Cl|_STATUSCODE}}(h)
url2$ = url$
x = {{Cl|INSTR}}(url2$, "/")
{{Cl|IF...THEN|IF}} x {{Cl|THEN}} url2$ = {{Cl|LEFT$}}(url$, x - 1)
client = {{Cl|_OPENCLIENT}}("TCP/IP:80:" + url2$)
{{Cl|IF...THEN|IF}} client = 0 {{Cl|THEN}} {{Cl|EXIT FUNCTION}}
e$ = {{Cl|CHR$}}(13) + {{Cl|CHR$}}(10) ' end of line characters
url3$ = {{Cl|RIGHT$}}(url$, {{Cl|LEN}}(url$) - x + 1)
x$ = "GET " + url3$ + " HTTP/1.1" + e$
x$ = x$ + "Host: " + url2$ + e$ + e$
{{Cl|PUT (TCP/IP statement)|PUT}} #client, , x$
t! = {{Cl|TIMER}} ' start time
{{Cl|DO}}
    {{Cl|_DELAY}} 0.05 ' 50ms delay (20 checks per second)
    {{Cl|GET (TCP/IP statement)|GET}} #client, , a2$
    a$ = a$ + a2$
    i = {{Cl|INSTR}}(a$, "Content-Length:")
     {{Cl|IF...THEN|IF}} i {{Cl|THEN}}
      i2 = {{Cl|INSTR}}(i, a$, e$)
      {{Cl|IF...THEN|IF}} i2 {{Cl|THEN}}
      l = {{Cl|VAL}}({{Cl|MID$}}(a$, i + 15, i2 - i -14))
      i3 = {{Cl|INSTR}}(i2, a$, e$ + e$)
        {{Cl|IF...THEN|IF}} i3 {{Cl|THEN}}
          i3 = i3 + 4 'move i3 to start of data
          {{Cl|IF...THEN|IF}} ({{Cl|LEN}}(a$) - i3 + 1) = l {{Cl|THEN}}
            {{Cl|CLOSE}} client ' CLOSE CLIENT
            d$ = {{Cl|MID$}}(a$, i3, l)
            fh = {{Cl|FREEFILE}}
            {{Cl|OPEN}} file$ {{Cl|FOR}} {{Cl|OUTPUT}} {{Cl|AS}} #fh: {{Cl|CLOSE}} #fh ' erase existing file?


            {{Cl|OPEN}} file$ {{Cl|FOR}} {{Cl|BINARY}} {{Cl|AS}} #fh
    {{Cl|WHILE}} {{Cl|NOT}} {{Cl|EOF}}(h&)
            {{Cl|PUT}} #fh, , d$
        {{Cl|_LIMIT}} 60
            {{Cl|CLOSE}} #fh
        {{Cl|GET}} #h&, , s$
            Download = -1 'indicates download was successfull
        content$ = content$ + s$
            {{Cl|EXIT FUNCTION}}
    {{Cl|WEND}}
          {{Cl|END IF}} ' availabledata = l
 
        {{Cl|END IF}} ' i3
    {{Cl|CLOSE}} #h&
      {{Cl|END IF}} ' i2
 
     {{Cl|END IF}} ' i
     Download$ = content$
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|TIMER}} > t! + timelimit ' (in seconds)
{{Cl|CLOSE}} client
{{Cl|END FUNCTION}}
{{Cl|END FUNCTION}}
{{CodeEnd}}
{{CodeEnd}}

Revision as of 07:45, 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 client_handle 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