CONNECTIONADDRESS: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 21: Line 21:
{{CodeStart}}
{{CodeStart}}
f = {{Cl|FREEFILE}}
f = {{Cl|FREEFILE}}
{{Cl|OPEN}} "ChatLog.dat" {{Cl|FOR}} {{Cl|APPEND}} {{Cl|AS}} #f ' code at start of host section before DO loop.
{{Cl|OPEN}} {{Text|<nowiki>"ChatLog.dat"</nowiki>|#FFB100}} {{Cl|OPEN#File_Access_Modes|FOR}} {{Cl|OPEN#File_Access_Modes|APPEND}} {{Cl|OPEN|AS}} #f {{Text|<nowiki>' code at start of host section before DO loop.</nowiki>|#919191}}


newclient = {{Cl|_OPENCONNECTION}}(host) ' receive any new client connection handles
newclient = {{Cl|_OPENCONNECTION}}(host) {{Text|<nowiki>' receive any new client connection handles</nowiki>|#919191}}
{{Cl|IF...THEN|IF}} newclient {{Cl|THEN}}
{{Cl|IF}} newclient {{Cl|THEN}}
  numclients = numclients + 1 ' increment index
    numclients = numclients + {{Text|1|#F580B1}} {{Text|<nowiki>' increment index</nowiki>|#919191}}
  Users(numclients) = newclient ' place handle into array
    Users(numclients) = newclient {{Text|<nowiki>' place handle into array</nowiki>|#919191}}
  IP$ = {{Cl|_CONNECTIONADDRESS}}(newclient)
    IP$ = {{Cl|_CONNECTIONADDRESS}}(newclient)
  {{Cl|PRINT}} IP$ + " has joined." ' displayed to Host only
    {{Cl|PRINT}} IP$ + {{Text|<nowiki>" has joined."</nowiki>|#FFB100}} {{Text|<nowiki>' displayed to Host only</nowiki>|#919191}}
  {{Cl|PRINT (file statement)|PRINT #f}}, IP$, numclients ' print info to a log file
    {{Cl|PRINT (file statement)|PRINT}} #f, IP$, numclients {{Text|<nowiki>' print info to a log file</nowiki>|#919191}}
  {{Cl|PRINT (file statement)|PRINT #}}Users(numclients),"Welcome!" ' from Host to new clients only
    {{Cl|PRINT (file statement)|PRINT}} #Users(numclients), {{Text|<nowiki>"Welcome!"</nowiki>|#FFB100}} {{Text|<nowiki>' from Host to new clients only</nowiki>|#919191}}
{{Cl|END IF}}
{{Cl|END IF}}
{{CodeEnd}}
{{CodeEnd}}
{{PreStart}}
{{PreStart}}
'''Explanation'''
'''Explanation'''
The function returns the new client's IP address to the IP$ variable. Prints the IP and the original login position to a log file. The information can later be used by the host for reference  if necessary. The host could set up a ban list too.
The function returns the new client's IP address to the IP$ variable.
Prints the IP and the original login position to a log file. The
information can later be used by the host for reference  if necessary.
The host could set up a ban list too.
{{PreEnd}}
{{PreEnd}}



Latest revision as of 21:51, 20 March 2023

The _CONNECTIONADDRESS function returns a connected user's STRING IP address value. For HTTP connections it returns the effective URL of the connection.


Syntax

result$ = _CONNECTIONADDRESS[$](connectionHandle&)


Description

  • The handle can come from the _OPENHOST, OPENCLIENT or _OPENCONNECTION.
  • For HOSTs: It may return "TCP/IP:8080:213.23.32.5" where 8080 is the port it is listening on and 213.23.32.5 is the global IP address which any computer connected to the internet could use to locate your computer. If a connection to the internet is unavailable or your firewall blocks it, it returns your 'local IP' address (127.0.0.1). You might like to store this address somewhere where other computers can find it and connect to your host. Dynamic IPs which can change will need to be updated.
  • For CLIENTs:
    • For TCP/IP, it may return "TCP/IP:8080:213.23.32.5" where 8080 is the port it used to connect to the listening host and 213.23.32.5 is the IP address of the host name it resolved.
    • For HTTP, it will return a url, such as "https://qb64phoenix.com". It is possible for this URL to differ from the one originally passed to _OPENCLIENT if a HTTP redirect occurs when connecting to the original URL. The formatting may also differ slightly from the original URL.
  • For CONNECTIONs (from clients): It may return "TCP/IP:8080:34.232.321.25" where 8080 was the host listening port it connected to and 34.232.321.25 is the IP address of the client that connected. This is very useful because the host can log the IP address of clients for future reference (or banning, for example).
  • The $ sygil is optional for compatibility with older versions.


Examples

Example
A Host logging new chat clients in a Chat program. See the _OPENHOST example for the rest of the code used.
f = FREEFILE
OPEN "ChatLog.dat" FOR APPEND AS #f ' code at start of host section before DO loop.

newclient = _OPENCONNECTION(host) ' receive any new client connection handles
IF newclient THEN
    numclients = numclients + 1 ' increment index
    Users(numclients) = newclient ' place handle into array
    IP$ = _CONNECTIONADDRESS(newclient)
    PRINT IP$ + " has joined." ' displayed to Host only
    PRINT #f, IP$, numclients ' print info to a log file
    PRINT #Users(numclients), "Welcome!" ' from Host to new clients only
END IF
Explanation
 The function returns the new client's IP address to the IP$ variable.
 Prints the IP and the original login position to a log file. The
 information can later be used by the host for reference  if necessary.
 The host could set up a ban list too.


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link