1
1
Fork 0
mirror of https://github.com/QB64-Phoenix-Edition/QB64pe.git synced 2024-07-26 15:35:53 +00:00
QB64-PE/internal/help/_CONNECTED.txt
SMcNeill 6e01fc8dce Altered string compare routines (<,<=,>,>=) so they don't give false results with CHR$(0).
Added new _STRCMP and _STRICMP commands for quick string comparisons.
Cleaned up QB64 to finish removing the QUI (quick user insert) code and folders.
Altered UCASE and LCASE routines to be faster in some situations for us.
2014-09-22 08:19:03 -04:00

49 lines
2.6 KiB
Plaintext

The {{KW|_CONNECTED}} function returns the status of a TCP/IP connection handle.
{{PageSyntax}}
:''result&amp;'' = {{KW|_CONNECTED}}({{Parameter|connectionHandle&amp;}})
{{PageDescription}}
* The handle can come from the {{KW|_OPENHOST}}, {{KW|OPENCLIENT}} or {{KW|_OPENCONNECTION}} QB64 TCP/IP functions.
* Returns -1 if still connected or 0 if connection has ended/failed.
* Do not rely solely on this function to check for ending communication,
* Use &quot;time-out&quot; checking as well and {{KW|CLOSE}} any suspect connections.
* If this function indicates the handle is not connected, any unread messages can still be read using {{KW|INPUT (TCP/IP statement)|INPUT #}} or {{KW|GET (TCP/IP statement)|GET #}}.
* Even if this function indicates the handle is not connected, it is important to {{KW|CLOSE}} the connection anyway or important resources cannot be reallocated.
''Example:'' Updating the [[_OPENHOST|_OPENHOST chat program example]] to manage the Users array when users are no longer connected.
{{CodeStart}}
'' ''
{{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} numclients ' distribute incoming messages to all clients
{{Cl|IF...THEN|IF}} Users(i) {{Cl|THEN}} ' check for non-existing handle values(rare)
{{Cl|INPUT (TCP/IP statement)|INPUT #}}Users(i), message$
{{Cl|IF...THEN|IF}} message$ &lt;&gt; &quot;&quot; {{Cl|THEN}}
{{Cl|FOR...NEXT|FOR}} p = 1 {{Cl|TO}} numclients
{{Cl|IF...THEN|IF}} Users(p) {{Cl|THEN}} {{Cl|PRINT (TCP/IP statement)|PRINT #}}Users(p), message$
{{Cl|NEXT}} p
{{Cl|END IF}}
{{Cl|IF...THEN|IF}} {{Cl|_CONNECTED}}(Users(i)) {{Cl|IF...THEN|THEN}}
n = n + 1 ' new consecutive connected index
Users(n) = Users(i) ' assign handle value to consecutive index
{{Cl|ELSE}} : {{Cl|CLOSE}} #(Users(i)): Users(i) = 0 ' close and clear index
{{Cl|END IF}} ' if connected
{{Cl|END IF}} ' array handle exist
{{Cl|NEXT}} i
numclients = n: n = 0 '' ''
{{CodeEnd}}
:The connection routine is added to the [[_OPENHOST|chat program's]] message distribution code to update the User array and close bad connections. The value of the n index does not change for non-existing handles or lost connections, so it will always either match the numclients value or be less. This overwrites lost connection handles. Setting handles to 0 clears upper array indices. After the FOR loop has gone through all of the client users, the number of clients is updated to the value of n and n is reset to 0.
{{PageSeeAlso}}
* {{KW|_OPENCONNECTION}}
* {{KW|_OPENHOST}}
* {{KW|_OPENCLIENT}}
* {{KW|_CONNECTIONADDRESS$}}
* [[Downloading Files]]
{{PageNavigation}}