OPENCLIENT: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
(Created page with "The _OPENCLIENT function connects to a Host on the Internet as a Client and returns the Client status handle.{{PageSyntax}} :{{Parameter|clientHandle&}} = _OPENCLIENT('''"TCP/IP:8080:12:30:1:10"''') {{PageDescription}} *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 {{Parameter|clientHandle&}} values are negativ...") Tag: visualeditor |
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.{{PageSyntax}} | The [[_OPENCLIENT]] function connects to a Host on the Internet as a Client and returns the Client status handle. | ||
{{PageSyntax}} | |||
:{{Parameter|clientHandle&}} = [[_OPENCLIENT]]('''"TCP/IP:8080:12:30:1:10"''') | :{{Parameter|clientHandle&}} = [[_OPENCLIENT]]('''"TCP/IP:8080:12:30:1:10"''') | ||
{{PageDescription}} | {{PageDescription}} | ||
*An [[ERROR Codes|Illegal Function Call]] error will be triggered if the function is called with a string argument of the wrong syntax. | *An [[ERROR Codes|Illegal Function Call]] error will be triggered if the function is called with a string argument of the wrong syntax. | ||
Line 6: | Line 11: | ||
*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]] client_handle closes the client. A failed handle of value 0 does not need to be closed. | ||
{{PageExamples}} | |||
''Example 1:'' Attempting to connect to a local host(your host) as a client. A zero return indicates failure. | |||
{{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}} | ||
Line 13: | Line 23: | ||
{{Cl|ELSE}} {{Cl|PRINT}} "[Connection Failed!]" | {{Cl|ELSE}} {{Cl|PRINT}} "[Connection Failed!]" | ||
{{Cl|END IF}} '' '' | {{Cl|END IF}} '' '' | ||
{{CodeEnd}} | {{CodeEnd}} | ||
:'''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.{{CodeStart}} | ''Example 2:'' Using a "raw" Download function to download the QB64 bee image and displays it. | ||
{{CodeStart}} | |||
'replace the fake image address below with a real image address you want to download | 'replace the fake image address below with a real image address you want to download | ||
{{Cl|IF...THEN|IF}} Download("www.qb64.org/qb64.png", "qb64logo.png", 10) {{Cl|THEN}} ' timelimit = 10 seconds | {{Cl|IF...THEN|IF}} Download("www.qb64.org/qb64.png", "qb64logo.png", 10) {{Cl|THEN}} ' timelimit = 10 seconds | ||
Line 68: | Line 77: | ||
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|TIMER}} > t! + timelimit ' (in seconds) | {{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|TIMER}} > t! + timelimit ' (in seconds) | ||
{{Cl|CLOSE}} client | {{Cl|CLOSE}} client | ||
{{Cl|END FUNCTION}} | {{Cl|END FUNCTION}} | ||
{{CodeEnd}}{{PageSeeAlso}} | {{CodeEnd}} | ||
{{PageSeeAlso}} | |||
*[[_OPENHOST]], [[_OPENCONNECTION]] | *[[_OPENHOST]], [[_OPENCONNECTION]] | ||
*[[_CONNECTED]], [[_CONNECTIONADDRESS$]] | *[[_CONNECTED]], [[_CONNECTIONADDRESS$]] |
Revision as of 16:08, 20 November 2022
The _OPENCLIENT function connects to a Host on the Internet as a Client and returns the Client status handle.
Syntax
- clientHandle& = _OPENCLIENT("TCP/IP:8080:12:30:1:10")
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 a "raw" Download function to download the QB64 bee image and displays it.
'replace the fake image address below with a real image address you want to download IF Download("www.qb64.org/qb64.png", "qb64logo.png", 10) THEN ' timelimit = 10 seconds SCREEN _LOADIMAGE("qb64logo.png",32) ELSE: PRINT "Couldn't download image." END IF SLEEP SYSTEM ' ---------- program end ----------- FUNCTION Download (url$, file$, timelimit) ' returns -1 if successful, 0 if not url2$ = url$ x = INSTR(url2$, "/") IF x THEN url2$ = LEFT$(url$, x - 1) client = _OPENCLIENT("TCP/IP:80:" + url2$) IF client = 0 THEN EXIT FUNCTION e$ = CHR$(13) + CHR$(10) ' end of line characters url3$ = RIGHT$(url$, LEN(url$) - x + 1) x$ = "GET " + url3$ + " HTTP/1.1" + e$ x$ = x$ + "Host: " + url2$ + e$ + e$ PUT #client, , x$ t! = TIMER ' start time DO _DELAY 0.05 ' 50ms delay (20 checks per second) GET #client, , a2$ a$ = a$ + a2$ i = INSTR(a$, "Content-Length:") IF i THEN i2 = INSTR(i, a$, e$) IF i2 THEN l = VAL(MID$(a$, i + 15, i2 - i -14)) i3 = INSTR(i2, a$, e$ + e$) IF i3 THEN i3 = i3 + 4 'move i3 to start of data IF (LEN(a$) - i3 + 1) = l THEN CLOSE client ' CLOSE CLIENT d$ = MID$(a$, i3, l) fh = FREEFILE OPEN file$ FOR OUTPUT AS #fh: CLOSE #fh ' erase existing file? OPEN file$ FOR BINARY AS #fh PUT #fh, , d$ CLOSE #fh Download = -1 'indicates download was successfull EXIT FUNCTION END IF ' availabledata = l END IF ' i3 END IF ' i2 END IF ' i LOOP UNTIL TIMER > t! + timelimit ' (in seconds) CLOSE client END FUNCTION |
See also
- _OPENHOST, _OPENCONNECTION
- _CONNECTED, _CONNECTIONADDRESS$
- Email Demo, Inter-Program Data Sharing Demo
- Downloading Files