Difference between revisions of "OPENHOST"

From QB64 Wiki
Jump to navigation Jump to search
imported>Clippy
m
imported>Cyperium
Line 3: Line 3:
  
  
 
+
{{PageSyntax}}
:::''Syntax:'' host_handle = _OPENHOST("TCP/IP:8080")  
+
:<code>{{Parameter|host_handle}} = {{KW|_OPENHOST}}("TCP/IP:8080")</code>
  
  
  
 +
{{PageDescription}}
 
* Creates an ILLEGAL FUNCTION CALL 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.
Line 13: Line 14:
 
* If the syntax is correct but they fail to begin/connect a handle of 0 is returned.
 
* 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.   
 
* Always check if the handle returned is 0 (failed) before continuing.   
* [[CLOSE]] host_handle closes the host. A failed handle value of 0 does not need to be closed.
+
* {{KW|CLOSE}} {{Parameter|host_handle}} closes the host. A failed handle value of 0 does not need to be closed.
  
  
 +
{{PageExamples}}
 
''Example:'' Chat program that attempts to connect as a Client. If not it attempts to become the Host.
 
''Example:'' Chat program that attempts to connect as a Client. If not it attempts to become the Host.
  
:[[PRINT]] "Mini Messenger"
+
{{CodeStart}}
:[[LOCATE]] , , 1 ' display the print cursor for INKEY$
+
 
:client = [[_OPENCLIENT]]("TCP/IP:7319:localhost") ' Attempt to connect to local host as a client  
+
{{Cl|PRINT}} "Mini Messenger"
:[[IF...THEN|IF]] client THEN
+
{{Cl|LOCATE}} , , 1 ' display the print cursor for INKEY$
::PRINT "[connected to " + [[_CONNECTIONADDRESS]](client) + "]"
+
client = {{Cl|_OPENCLIENT}}("TCP/IP:7319:localhost") ' Attempt to connect to local host as a client  
::[[_TITLE]] "Client - Mini Messenger"
+
{{Cl|IF...THEN|IF}} client {{Cl|THEN}}
::[[INPUT]] "Enter your name: ", myname$
+
  {{Cl|PRINT}} "[connected to " + {{Cl|_CONNECTIONADDRESS}}(client) + "]"
::PRINT #client, myname$ + " connected!"
+
  {{Cl|_TITLE}} "Client - Mini Messenger"
::DO
+
  {{Cl|INPUT}} "Enter your name: ", myname$
:::GetMessage client
+
  {{Cl|PRINT (TCP/IP statement)|PRINT}} #client, myname$ + " connected!"
:::SendMessage myname$, mymessage$, client ' display current input on screen
+
  {{Cl|DO...LOOP|DO}}
:::[[_DELAY]] 0.01 ' reduce CPU usage
+
  GetMessage client
::LOOP
+
  SendMessage myname$, mymessage$, client ' display current input on screen
:[[ELSE]] ' "if client" alternative to open a new Host
+
  {{Cl|_DELAY}} 0.01 ' reduce CPU usage
::[[PRINT]] "[No existing host found]"
+
  {{Cl|LOOP}}
::host = [[_OPENHOST]]("TCP/IP:7319") ' no host found, so begin new host
+
{{Cl|ELSE}} ' "if client" alternative to open a new Host
::IF host THEN
+
  {{Cl|PRINT}} "[No existing host found]"
:::[[_TITLE]] "Host - Mini Messenger"
+
  host = {{Cl|_OPENHOST}}("TCP/IP:7319") ' no host found, so begin new host
:::PRINT "[Beginning new host chat session!]"
+
  {{Cl|IF...THEN|IF}} host {{Cl|THEN}}
:::[[DIM]] Users(1 to 1000) ' array to hold other client info
+
  {{Cl|_TITLE}} "Host - Mini Messenger"
:::numclients = 0
+
  {{Cl|PRINT}} "[Beginning new host chat session!]"
:::client = [[_OPENCLIENT]]("TCP/IP:7319:localhost")
+
  {{Cl|DIM}} Users(1 to 1000) ' array to hold other client info
:::IF client = 0 THEN PRINT "ERROR: could not attach host's personal client to host!"
+
  numclients = 0
:::[[INPUT]] "Enter your name:", myname$
+
  client = {{Cl|_OPENCLIENT}}("TCP/IP:7319:localhost")
:::PRINT #client, myname$ + " connected!"
+
  IF client = 0 THEN PRINT "ERROR: could not attach host's personal client to host!"
:::PRINT "[Chat session active!]"
+
  {{Cl|INPUT}} "Enter your name:", myname$
:::DO ' host main loop
+
  {{Cl|PRINT}} #client, myname$ + " connected!"
::::newclient = [[_OPENCONNECTION]](host) ' receive any new connection
+
  {{Cl|PRINT}} "[Chat session active!]"
::::IF newclient THEN
+
  {{Cl|DO...LOOP|DO}} ' host main loop
:::::numclients = numclients + 1
+
    newclient = {{Cl|_OPENCONNECTION}}(host) ' receive any new connection
:::::Users(numclients) = newclient
+
    {{Cl|IF...THEN|IF}} newclient {{Cl|THEN}}
:::::[[PRINT (TCP/IP statement)|PRINT #]]Users(numclients),"Welcome!"
+
    numclients = numclients + 1
::::END IF
+
    Users(numclients) = newclient
::::[[FOR...NEXT|FOR]] i = 1 TO numclients ' distribute incoming messages to all clients
+
    {{Cl|PRINT (TCP/IP statement)|PRINT}} #Users(numclients),"Welcome!"
:::::IF Users(i) THEN
+
    {{Cl|END IF}}
::::::[[INPUT (TCP/IP statement)|INPUT #]]Users(i), message$
+
    {{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} numclients ' distribute incoming messages to all clients
::::::IF message$ <> "" THEN
+
    {{Cl|IF...THEN|IF}} Users(i) {{Cl|THEN}}
:::::::FOR p = 1 TO numclients
+
      {{Cl|INPUT (TCP/IP statement)|INPUT}} #Users(i), message$
::::::::IF Users(p) THEN [[PRINT (TCP/IP statement)|PRINT #]]Users(p), message$
+
      {{Cl|IF...THEN|IF}} message$ <> "" {{Cl|THEN}}
:::::::NEXT p
+
      {{Cl|FOR...NEXT|FOR}} p = 1 {{Cl|TO}} numclients
::::::END IF
+
        {{Cl|IF...THEN|IF}} Users(p) {{Cl|THEN}} {{Cl|PRINT (TCP/IP statement)|PRINT}} #Users(p), message$
:::::END IF
+
      {{Cl|NEXT}} p
::::NEXT i
+
      {{Cl|END IF}}
::::GetMessage client ' allow host to get messages and chat also
+
    {{Cl|END IF}}
::::SendMessage myname$, mymessage$, client
+
    {{Cl|NEXT}} i
::::[[_DELAY]] 0.01 ' reduce CPU usage
+
    GetMessage client ' allow host to get messages and chat also
:::LOOP
+
    SendMessage myname$, mymessage$, client
::END IF ' host
+
    {{Cl|_DELAY}} 0.01 ' reduce CPU usage
::PRINT "ERROR: Could not begin new host!"  
+
  {{Cl|LOOP}}
:[[END IF]] ' if client from start
+
  {{Cl|END IF}} ' host  
:[[SLEEP]]
+
  {{Cl|PRINT}} "ERROR: Could not begin new host!"  
:[[SYSTEM]]
+
{{Cl|END IF}} ' if client from start
 +
{{Cl|SLEEP}}
 +
{{Cl|SYSTEM}}
  
:: '.................... END OF MAIN PROGRAM ................
+
  '.................... END OF MAIN PROGRAM ................
 
   
 
   
  
:[[SUB]] GetMessage (client) ' get & display any new message
+
{{Cl|SUB}} GetMessage (client) ' get & display any new message
::[[INPUT (TCP/IP statement)|INPUT #]]client, newmessage$
+
  {{Cl|INPUT (TCP/IP statement)|INPUT}} #client, newmessage$
::IF newmessage$ <> "" THEN
+
  {{Cl|IF...THEN|IF}} newmessage$ <> "" {{Cl|THEN}}
:::[[VIEW PRINT]] 1 TO 23
+
  {{Cl|VIEW PRINT}} 1 {{Cl|TO}} 23
:::[[LOCATE]] 23,1
+
  {{Cl|LOCATE}} 23,1
:::[[PRINT]] newmessage$
+
  {{Cl|PRINT}} newmessage$
:::[[VIEW PRINT]] 1 TO 24
+
  {{Cl|VIEW PRINT}} 1 {{Cl|TO}} 24
::[[END IF]]
+
  {{Cl|END IF}}
:[[END SUB]]
+
{{Cl|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 message displayed
 
::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 (TCP/IP 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 could be added like recording IP addresses!
+
{{Cl|SUB}} SendMessage (myname$, mymessage$, client) ' simple input handler
 +
  k$ = {{Cl|INKEY$}}
 +
  {{Cl|IF...THEN|IF}} {{Cl|LEN}}(k$) {{Cl|THEN}}
 +
  {{Cl|IF...THEN|IF}} k$ = {{Cl|CHR$}}(8) {{Cl|AND (boolean)|AND}} [[LEN]](mymessage$) <> 0 {{Cl|THEN}}
 +
    mymessage$ = {{Cl|LEFT$}}(mymessage$, {{Cl|LEN}}(mymessage$) - 1)
 +
  {{Cl|ELSE}}
 +
    {{Cl|IF...THEN|IF}} {{Cl|LEN}}(k$) = 1 {{Cl|AND (boolean)|AND}} {{Cl|ASC}}(k$) >= 32 {{Cl|THEN}} mymessage$ = mymessage$ + k$
 +
  {{Cl|END IF}}
 +
  {{Cl|END IF}}
 +
  {{Cl|LOCATE}} 24, 1: {{Cl|PRINT}} {{Cl|SPACE$}}(80); ' erase previous message displayed
 +
  {{Cl|LOCATE}} 24, 1: {{Cl|PRINT}} myname$ + ": ";mymessage$;
 +
  {{Cl|IF...THEN|IF}} k$ = {{Cl|CHR$}}(13) {{Cl|THEN}} ' [Enter] sends the message
 +
  {{Cl|IF...THEN|IF}} mymessage$ = "" {{Cl|THEN}} {{Cl|SYSTEM}} ' [Enter] with no message ends program
 +
  {{Cl|PRINT (TCP/IP statement)|PRINT}} #client, myname$ + ": " + mymessage$
 +
  mymessage$ = ""
 +
  {{Cl|END IF}}
 +
  {{Cl|IF...THEN|IF}} k$ = {{Cl|CHR$}}(27) {{Cl|THEN}} {{Cl|SYSTEM}} ' [Esc] key ends program
 +
{{Cl|END SUB}}
  
 +
{{CodeEnd}}
 +
{{OutputStart}}
 +
Mini Messenger
 +
[No existing host found]
 +
[Beginning new host chat session!]
 +
Enter your name:_
 +
{{OutputEnd}}
  
 +
''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. ::{{KW|INPUT (file statement)|INPUT #}} is used to read messages and {{KW|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 could be added like recording IP addresses!
  
  
''See also:'' [[_OPENCONNECTION]], [[_OPENCLIENT]], [[_CONNECTED]], [[_CONNECTIONADDRESS]], [[Email Demo]]
 
  
  
 +
{{PageSeeAlso}}
 +
*{{KW|_OPENCONNECTION}}, {{KW|_OPENCLIENT}}
 +
*{{KW|_CONNECTED}}, {{KW|_CONNECTIONADDRESS}}
 +
*[[Email Demo]]
  
==Navigation:==
 
  
[[Keyword_Reference_-_Alphabetical|Go to Keyword Reference - Alphabetical]]
 
  
[[Keyword Reference - By usage|Go to Keyword Reference - By usage]]
+
{{PageNavigation}}

Revision as of 01:15, 2 February 2010

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


Syntax

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


Description

  • Creates an ILLEGAL FUNCTION CALL if called with a string argument of the wrong syntax.
  • The port used in the syntax example is 8080.
  • Valid handle values are usually negative number values.
  • 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 host_handle closes the host. A failed handle value of 0 does not need to be closed.


Examples

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 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 Users(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 Users(numclients) = newclient PRINT #Users(numclients),"Welcome!" END IF FOR i = 1 TO numclients ' distribute incoming messages to all clients IF Users(i) THEN INPUT #Users(i), message$ IF message$ <> "" THEN FOR p = 1 TO numclients IF Users(p) THEN PRINT #Users(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 #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 message displayed 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 #client, myname$ + ": " + mymessage$ mymessage$ = "" END IF IF k$ = CHR$(27) THEN SYSTEM ' [Esc] key ends program END SUB

Mini Messenger [No existing host found] [Beginning new host chat session!] Enter your name:_

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 # is used to read messages and 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 could be added like recording IP addresses!



See also




Navigation:
Keyword Reference - Alphabetical
Keyword Reference - By Usage
Main Wiki Page