OPENCONNECTION - QB64 Wiki

OPENCONNECTION

From QB64 Wiki

Jump to: navigation, search

The _OPENCONNECTION function open's a connection from a client that the host has detected and returns a status handle.



Syntax:

connect_handle = _OPENCONNECTION(host_handle)



Description:

  • Valid handles returned are usually negative numbers.
  • If the syntax is correct but they fail to begin/connect a handle of 0 is returned.
  • Always check if the handle returned is 0 (failed) before continuing.
  • CLOSE #connect_handle closes the connection. Failed connections(connect_handle = 0) do not need to be closed.
  • As a Host you can check for new clients(users). Each will have a unique connection handle.
  • Creates an ILLEGAL FUNCTION CALL error if called with a string argument of the wrong syntax.
  • Handle values can be used as the open number by INPUT # or GET # read statements and PUT # or PRINT # write statements.


Code Examples:

Example: Using the _OPENCONNECTION new client return with INPUT # or GET # message or data reads.

host = _OPENHOST("TCP/IP:8080") DO newclient = _OPENCONNECTION(host) ' monitor host connection IF newclient THEN SLEEP 1 ' wait one second for data to arrive INPUT #newclient, a PRINT a CLOSE #newclient ' close after each read ELSE : _DELAY .05 ' share resources with other programs (20 loops per second) END IF LOOP UNTIL INKEY$ <> "" ' any keypress quits CLOSE #host SYSTEM

Explanation: The function finds new clients and waits one second to read a message or other data. If a message or data was sent, it displays it on the screen and closes the connection.

Note: When sending data, the client should wait about 3 seconds before closing their connection!



See also:



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page