Difference between revisions of "OPENHOST"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
 
(24 intermediate revisions by 4 users not shown)
Line 1: Line 1:
The '''_OPENHOST''' function opens a Host which listens for new connections and returns a Host status handle.
+
{{DISPLAYTITLE:_OPENHOST}}
 +
The [[_OPENHOST]] function opens a Host which listens for new connections and returns a Host status handle.
  
  
 +
{{PageSyntax}}
 +
: {{Parameter|hostHandle}} = [[_OPENHOST]]('''"TCP/IP:8080"''')
  
  
:::''Syntax:'' host_handle = _OPENHOST("TCP/IP:8080")
+
{{PageDescription}}
 
+
* Creates an [[ERROR Codes|Illegal Function Call]] error if called with a string argument of the wrong syntax.
 
 
 
 
* Creates an ILLEGAL FUNCTION CALL if called with a string argument of the wrong syntax.
 
 
* The port used in the syntax example is 8080.
 
* The port used in the syntax example is 8080.
* Valid handles are usually negative number values.
+
* Valid {{Parameter|hostHandle}} values are 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.   
+
* If the syntax is correct but they fail to begin/connect a {{Parameter|hostHandle}} of 0 is returned.
* [[CLOSE]] host_handle closes the host. A failed handle value of 0 does not need to be closed.
+
* Always check if the handle returned is 0 (failed) before continuing.   
 
+
* [[CLOSE]] {{Parameter|hostHandle}} closes the host. A failed handle value of 0 does not need to be closed.
 
 
''Example:'' Chat program that attempts to connect as a Client. If not it attempts to become the Host.
 
 
 
:[[PRINT]] "Mini Messenger"
 
:[[LOCATE]] , , 1 ' display the print cursor for INKEY$
 
:client = [[_OPENCLIENT]]("TCP/IP:7319:localhost") ' Attempt to connect to local host as a client
 
:[[IF...THEN|IF]] client THEN
 
::PRINT "[connected to " + [[_CONNECTIONADDRESS]](client) + "]"
 
::[[_TITLE]] "Client - Mini Messenger"
 
::[[INPUT]] "Enter your name: ", myname$
 
::PRINT #client, myname$ + " connected!"
 
::DO
 
:::GetMessage client
 
:::SendMessage myname$, mymessage$, client ' display current input on screen
 
:::[[_DELAY]] 0.01 ' reduce CPU usage
 
::LOOP
 
:[[ELSE]] ' "if client" alternative to open a new Host
 
::[[PRINT]] "[No existing host found]"
 
::host = [[_OPENHOST]]("TCP/IP:7319") ' no host found, so begin new host
 
::IF host THEN
 
:::[[_TITLE]] "Host - Mini Messenger"
 
:::PRINT "[Beginning new host chat session!]"
 
:::[[DIM]] Chatters(1 to 1000) ' array to hold other client info
 
:::numclients = 0
 
:::client = [[_OPENCLIENT]]("TCP/IP:7319:localhost")
 
:::IF client = 0 THEN PRINT "ERROR: could not attach host's personal client to host!"
 
:::[[INPUT]] "Enter your name:", myname$
 
:::PRINT #client, myname$ + " connected!"
 
:::PRINT "[Chat session active!]"
 
:::DO ' host main loop
 
::::newclient = [[_OPENCONNECTION]](host) ' receive any new connection
 
::::IF newclient THEN
 
:::::numclients = numclients + 1
 
:::::Chatters(numclients) = newclient
 
:::::[[PRINT (file statement)|PRINT #]]Chatters(numclients),"Welcome!"
 
::::END IF
 
::::[[FOR...NEXT|FOR]] i = 1 TO numclients ' distribute incoming messages to all clients
 
:::::IF Chatters(i) THEN
 
::::::[[INPUT (file statement)|INPUT #]]Chatters(i), message$
 
::::::IF message$ <> "" THEN
 
:::::::FOR p = 1 TO numclients
 
::::::::IF Chatters(p) THEN [[PRINT (file statement)|PRINT #]]Chatters(p), message$
 
:::::::NEXT p
 
::::::END IF
 
:::::END IF
 
::::NEXT i
 
::::GetMessage client ' allow host to get messages and chat also
 
::::SendMessage myname$, mymessage$, client
 
::::[[_DELAY]] 0.01 ' reduce CPU usage
 
:::LOOP
 
::END IF ' host
 
::PRINT "ERROR: Could not begin new host!"
 
:[[END IF]] ' if client from start
 
:[[SLEEP]]
 
:[[SYSTEM]]
 
 
 
:: '.................... END OF MAIN PROGRAM ................
 
 
 
 
:[[SUB]] GetMessage (client) ' get & display any new message
 
::[[INPUT (file statement)|INPUT #]]client, newmessage$
 
::IF newmessage$ <> "" THEN
 
:::[[VIEW PRINT]] 1 TO 23
 
:::[[LOCATE]] 23,1
 
:::[[PRINT]] newmessage$
 
:::[[VIEW PRINT]] 1 TO 24
 
::[[END IF]]
 
:[[END SUB]]
 
 
 
:[[SUB]] SendMessage (myname$, mymessage$, client) ' simple input handler
 
::k$ = [[INKEY$]]$
 
::IF [[LEN]](k$) THEN
 
:::IF k$ = [[CHR$]](8) AND [[LEN]](mymessage$) <> 0 THEN
 
::::mymessage$ = [[LEFT$]](mymessage$, [[LEN]](mymessage$) - 1)
 
:::[[ELSE]]
 
::::IF [[LEN]](k$) = 1 and [[ASC]](k$) >= 32 THEN mymessage$ = mymessage$ + k$
 
:::[[END IF]]
 
::[[END IF]]
 
::LOCATE 24, 1: PRINT [[SPACE$]](80); ' erase previous display
 
::LOCATE 24, 1: PRINT myname$ + ": ";mymessage$;
 
::IF k$ = [[CHR$]](13) THEN ' [Enter] sends the message
 
:::IF mymessage$ = "" THEN [[SYSTEM]] ' [Enter] with no message ends program
 
:::[[PRINT (file statement)|PRINT #]]client, myname$ + ": " + mymessage$
 
:::mymessage$ = ""
 
::[[END IF]]
 
::IF k$ = [[CHR$]](27) THEN [[SYSTEM]] ' [Esc] key ends program
 
:[[END SUB]]
 
 
 
 
 
''Explanation:'' The SendMessage SUB program controls the program exit unless both Client and Host fail. Entering no message and hitting [Enter] or pressing the [Esc]] key closes the program. Both SUB programs allow a Client or Host to communicate with others simply. ::[[INPUT (file statement)|INPUT #]] is used to read messages and [[PRINT (file statement)|PRINT #]] is used to send messages. The client handle value is used as the port number. Keep in mind that this is just an example. A lot can be added!
 
 
 
 
 
 
 
 
 
''See also:'' [[_OPENCONNECTION]], [[_OPENCLIENT]], [[_CONNECTED]], [[_CONNECTIONADDRESS]]
 
  
  
  
==Navigation:==
+
{{PageSeeAlso}}
 +
* [[_OPENCONNECTION]], [[_OPENCLIENT]]
 +
* [[_CONNECTED]], [[_CONNECTIONADDRESS]]
 +
* [[Email Demo]], [[Inter-Program Data Sharing Demo]]
 +
* [[Downloading Files]]
  
[[Keyword_Reference_-_Alphabetical|Go to Keyword Reference - Alphabetical]]
 
  
[[Keyword Reference - By usage|Go to Keyword Reference - By usage]]
+
{{PageNavigation}}

Latest revision as of 01:16, 20 April 2020

The _OPENHOST function opens a Host which listens for new connections and returns a Host status handle.


Syntax

hostHandle = _OPENHOST("TCP/IP:8080")


Description

  • Creates an Illegal Function Call error if called with a string argument of the wrong syntax.
  • The port used in the syntax example is 8080.
  • Valid hostHandle values are negative numbers.
  • If the syntax is correct but they fail to begin/connect a hostHandle of 0 is returned.
  • Always check if the handle returned is 0 (failed) before continuing.
  • CLOSE hostHandle closes the host. A failed handle value of 0 does not need to be closed.


See also



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