{{DISPLAYTITLE:_OPENHOST}}
The '''_OPENHOST''' function opens a Host which listens for new connections and returns a Host status handle.


{{PageSyntax}}
::: host_handle = '''_OPENHOST('''&quot;TCP/IP:8080&quot;''')'''


{{PageDescription}}
* 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.  
* {{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.

{{CodeStart}}

 {{Cl|PRINT}} &quot;Mini Messenger&quot;
 {{Cl|LOCATE}} , , 1 ' display the print cursor for INKEY$
 client = {{Cl|_OPENCLIENT}}(&quot;TCP/IP:7319:localhost&quot;) ' Attempt to connect to local host as a client 
 {{Cl|IF...THEN|IF}} client {{Cl|THEN}}
  {{Cl|PRINT}} &quot;[connected to &quot; + {{Cl|_CONNECTIONADDRESS}}(client) + &quot;]&quot;
  {{Cl|_TITLE}} &quot;Client - Mini Messenger&quot;
  {{Cl|INPUT}} &quot;Enter your name: &quot;, myname$
  {{Cl|PRINT (TCP/IP statement)|PRINT}} #client, myname$ + &quot; connected!&quot;
  {{Cl|DO...LOOP|DO}}
   GetMessage client
   SendMessage myname$, mymessage$, client ' display current input on screen
   {{Cl|_DELAY}} 0.01 ' reduce CPU usage
  {{Cl|LOOP}}
 {{Cl|ELSE}} ' &quot;if client&quot; alternative to open a new Host
  {{Cl|PRINT}} &quot;[No existing host found]&quot;
  host = {{Cl|_OPENHOST}}(&quot;TCP/IP:7319&quot;) ' no host found, so begin new host
  {{Cl|IF...THEN|IF}} host {{Cl|THEN}}
   {{Cl|_TITLE}} &quot;Host - Mini Messenger&quot;
   {{Cl|PRINT}} &quot;[Beginning new host chat session!]&quot;
   {{Cl|DIM}} Users(1 to 1000) ' array to hold other client info
   numclients = 0
   client = {{Cl|_OPENCLIENT}}(&quot;TCP/IP:7319:localhost&quot;)
   IF client = 0 THEN PRINT &quot;ERROR: could not attach host's personal client to host!&quot;
   {{Cl|INPUT}} &quot;Enter your name:&quot;, myname$
   {{Cl|PRINT (TCP/IP statement)|PRINT}} #client, myname$ + &quot; connected!&quot;
   {{Cl|PRINT}} &quot;[Chat session active!]&quot;
   {{Cl|DO...LOOP|DO}} ' host main loop
    newclient = {{Cl|_OPENCONNECTION}}(host) ' receive any new connection
    {{Cl|IF...THEN|IF}} newclient {{Cl|THEN}}
     numclients = numclients + 1
     Users(numclients) = newclient
     {{Cl|PRINT (TCP/IP statement)|PRINT}} #Users(numclients),&quot;Welcome!&quot;
    {{Cl|END IF}}
    {{Cl|FOR...NEXT|FOR}} i = 1 {{Cl|TO}} numclients ' distribute incoming messages to all clients
     {{Cl|IF...THEN|IF}} Users(i) {{Cl|THEN}}
      {{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|END IF}}
    {{Cl|NEXT}} i
    GetMessage client ' allow host to get messages and chat also
    SendMessage myname$, mymessage$, client
    {{Cl|_DELAY}} 0.01 ' reduce CPU usage
   {{Cl|LOOP}}
  {{Cl|END IF}} ' host 
  {{Cl|PRINT}} &quot;ERROR: Could not begin new host!&quot; 
 {{Cl|END IF}} ' if client from start
 {{Cl|SLEEP}}
 {{Cl|SYSTEM}}

   '.................... END OF MAIN PROGRAM ................
 

 {{Cl|SUB}} GetMessage (client) ' get &amp; display any new message
  {{Cl|INPUT (TCP/IP statement)|INPUT}} #client, newmessage$
  {{Cl|IF...THEN|IF}} newmessage$ &lt;&gt; &quot;&quot; {{Cl|THEN}}
   {{Cl|VIEW PRINT}} 1 {{Cl|TO}} 23
   {{Cl|LOCATE}} 23,1
   {{Cl|PRINT}} newmessage$
   {{Cl|VIEW PRINT}} 1 {{Cl|TO}} 24
  {{Cl|END IF}}
 {{Cl|END SUB}}

 {{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$) &lt;&gt; 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$) &gt;= 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$ + &quot;: &quot;;mymessage$;
  {{Cl|IF...THEN|IF}} k$ = {{Cl|CHR$}}(13) {{Cl|THEN}} ' [Enter] sends the message
   {{Cl|IF...THEN|IF}} mymessage$ = &quot;&quot; {{Cl|THEN}} {{Cl|SYSTEM}} ' [Enter] with no message ends program
   {{Cl|PRINT (TCP/IP statement)|PRINT}} #client, myname$ + &quot;: &quot; + mymessage$
   mymessage$ = &quot;&quot;
  {{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 (TCP/IP statement)|INPUT #}} is used to read messages and {{KW|PRINT (TCP/IP 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!
&lt;center&gt;'''To manage the users array, see the [[_CONNECTED]] page example.'''&lt;/center&gt;


{{PageSeeAlso}}
* [[_OPENCONNECTION]], [[_OPENCLIENT]]
* [[_CONNECTED]], [[_CONNECTIONADDRESS]]
* [[Email Demo]], [[Inter-Program Data Sharing Demo]] 
* [[IP Configuration]], [[WGET]] {{text|(HTTP and FTP file transfer)}}
* [[Downloading Files]]


{{PageNavigation}}