_CONNECTED - QB64 Wiki

_CONNECTED

From QB64 Wiki

Jump to: navigation, search

The _CONNECTED function returns the status of a TCP/IP connection handle.


Contents

Syntax

result& = _CONNECTED(connectionHandle&)


Description

  • The handle can come from the _OPENHOST, OPENCLIENT or _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 "time-out" checking as well and CLOSE any suspect connections.
  • If this function indicates the handle is not connected, any unread messages can still be read using INPUT # or GET #.
  • Even if this function indicates the handle is not connected, it is important to CLOSE the connection anyway or important resources cannot be reallocated.


Examples

Snippet: Updating the _OPENHOST chat program example to manage the Users array when users are no longer connected.

FOR i = 1 TO numclients ' distribute incoming messages to all clients IF Users(i) THEN ' check for non-existing handle values(rare) INPUT #Users(i), message$ IF message$ <> "" THEN FOR p = 1 TO numclients IF Users(p) THEN PRINT #Users(p), message$ NEXT p END IF IF _CONNECTED(Users(i)) THEN n = n + 1 ' new consecutive connected index Users(n) = Users(i) ' assign handle value to consecutive index ELSE : CLOSE #(Users(i)): Users(i) = 0 ' close and clear index END IF ' if connected END IF ' array handle exist NEXT i numclients = n: n = 0

The connection routine is added to the 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.


See also



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page
Views
  • Page
  • Discussion
  • View source
  • History
Personal tools
  • Log in
Toolbox
  • What links here
  • Related changes
  • Special pages
  • Printable version