Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Chat with Me -- HOST
#1
Now, as I mentioned in the topic Come chat with me! (qb64phoenix.com), I told you guys I was going to share the HOST part of the program which we were all using to play around and chat with yesterday -- and try to highlight the steps necessary to get it to run properly for everyone.

First, let's start with the code.  We'll begin with the HOST version of my Mini Messenger (qb64phoenix.com):

Code: (Select All)
DIM SHARED Users(1 TO 1000) ' array to hold other client info
DIM SHARED NumClients
DIM SHARED out$


PRINT "[Steve's Mini Messenger]"
host = _OPENHOST("TCP/IP:7319") ' no host found, so begin new host
IF host THEN
    PRINT "[Beginning new host chat session!]"
    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!]"
ELSE
    PRINT "ERROR: Could not begin new host!"
END IF ' host


DO ' host main loop
    newclient = _OPENCONNECTION(host) ' receive any new connection
    IF newclient THEN
        NumClients = NumClients + 1
        Users(NumClients) = newclient
        PRINT "Welcome to Steve's Mini Messenger!"
    END IF
    FOR i = 1 TO NumClients
        GetMessage Users(i) 'check all clients for a message
        IF out$ <> "" THEN
            l = LEN(out$)
            FOR j = 1 TO NumClients ' distribute incoming messages to all clients
                PUT #Users(j), , l
                PUT #Users(j), , out$
            NEXT
        END IF
    NEXT i

    SendMessage myname$, mymessage$, client
    _LIMIT 30
LOOP


SUB GetMessage (client) ' get & display any new message
    GET #client, , l
    IF l > 0 THEN
        out$ = SPACE$(l)
        GET #client, , out$
        VIEW PRINT 1 TO 20
        LOCATE 20, 1
        PRINT out$
        VIEW PRINT 1 TO 24
    ELSE
        out$ = ""
    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
    VIEW PRINT 1 TO 24
    LOCATE 22, 1: PRINT SPACE$(80); ' erase previous message displayed
    LOCATE 22, 1: PRINT myname$ + ": "; mymessage$;
    IF k$ = CHR$(13) THEN ' [Enter] sends the message
        IF mymessage$ = "" THEN SYSTEM ' [Enter] with no message ends program
        mymessage$ = myname$ + ":" + mymessage$
        l = LEN(mymessage$)
        PUT #client, , l
        PUT #client, , mymessage$
        mymessage$ = ""
    END IF
    IF k$ = CHR$(27) THEN SYSTEM ' [Esc] key ends program
END SUB[color=#cccccc][font=Monaco, Consolas, Courier, monospace][/font][/color]

Now, I tried to keep this as simple as I possibly could -- without any bells or whistles to complicate the viewing and understanding of the basic process we're using here -- so don't think this is anything fancy at all.  All this basically does is show how to set up a host connection, accept users trying to connect to that connection, and then transfer information back and forth between the two.

It's a demo to showcase the very bare bones of the TCP/IP stuff, and nothing more, so keep that in mind as we go along and talk about things.  Wink

First change needed to swap this over from LOCALHOST to world wide web hosting involves...   dum dum de dum....   Opening a  browser tab and going to What Is My IP? Shows Your Public IP Address - IPv4 - IPv6.  If you're using a VPN or such to protect your IP, you may be able to get by with using it, or you may not.   That all depends on your VPN's safety protocols.  If it just forwards anything that comes its way back your way, you're fine.  If it wants you to open ports and such as that and only forward certain things, then you're screwed unless you jump through their hoops and allow the transfer.  

My advice here:  Use your actual web address.  172.83.131.239 happens to be my permanent little home on the web.  From time to time, I tend to host my own website, and I need a static IP so I can always connect to it when it's up and going.  Most folks have a dynamic IP, which is assigned to them randomly every time they connect to the internet (it costs extra $$ each month for a static IP), so you'll need to update your chat program with the current IP with each reboot of your computer.

Once you've gotten your IP address, you can now go into the code above and make the drastic change from local to www connections:

Code: (Select All)
    client = _OpenClient("TCP/IP:7319:localhost")

Change the line above to where it now has your IP address, rather than "localhost".

Code: (Select All)
    client = _OpenClient("TCP/IP:7319:172.83.131.239")

^That's what it'll look like for me.  For you, the same, except with a different IP address in there.  Wink

And that's basically the ONLY TCP/IP requirement that QB64-PE makes you have to change to make it work and communicate across the net!



And chances are, if some of you try to make that work, it's not going to work for you.  In fact, I'd bet against it.  Sad

WHY??

First is the TCP/IP:7319...   What the heck is that 7319, and why is it in there?  

It's one of the multitude of ports which our modern PCs have available for us to use to communicate with things.  How was 7319 chosen?  That was all just Steve, picking an unused port on my PC, and deciding, "I'm ah gonna use dis one!"  Most of the time, our modern routers tend to lock down port access for most people and things.  You have one or two ports open for common stuff (like http and https transfers), but the rest of those ports are locked for security purposes.  

Since there's a zillion different routers, and a zillion different ways they're configured, with a zillion different sets of software to interact with them, *I CAN'T HELP YOU WITH YOUR ROUTER SETTINGS.*  You'll have to Google, dig out the manual that came packaged when you bought the router, or call your ISP and ask them to help.  At the end of the day though, you're NOT going to be able to share communications unless you're sharing on a port that's opened and allows it.  <--This part, unfortunately, you're on your own to puzzle out.  All I can say is "Open your router settings, choose a port you like that's not currently in use, and open it -- however you do that on your router."

Once you've got an open port, and if it's not 7319 like I chose for it to be, then you'd need to change your program to work on the port you've chosen.

Code: (Select All)
    client = _OpenClient("TCP/IP:####:172.83.131.239")

Try that, and it MAY work for you.  Once again, however, I'd be willing to bet against it.  Sad

Once more, go into your router settings, and this time look for PORT FORWARDING.  Most of us have multiple devices set up for the internet.  Our phones are connected, as is our pc, our ipad, our tv, all our damn smart lightbulbs...  You probably need to do a little more specific directing with port forwarding to tell your router where you want to send that open port information to.

Once again, I'm sorry, but I can't really help much with this step as every router has it's own software and way of interacting with you.


[Image: image.png]

Click on the image above, if you want, and it'll show my router's port forwarding setup.  The first three and where I host my own private server from time to time (ports 80 and 443 and http and https traffic, while 3306 is where my SQL Database likes to communicate back and forth when it's up and running).  The last entry in that list, however, is the one of interest for you guys -- Laptop Forwarding on port 7319, and which is going to 10.243.1.77...

That 10.242.1.77 is my laptop's local address on my network.  By setting up port forwarding like this, communications on port 7319 are now routed directly to it, allowing it to communicate with the rest of the world.

Once you've set up the open port, and forwarded it to your machine which is going to run the compiled EXE, chances are you're good to go!!  Your firewall might pop up a question "Do you really want to allow this", but you can feel free to tell it to pisser off.  You've got to let the information travel back and forth to your PC, or else you'll never be able to communicate on an open port like this with the outside world.  




So you run it...  And it works!!  YAAAAAAYYYY!!!

You go to bed, get up the next morning, notice that Windows did an update on you, and it now no longer works.   WTF?!!  (I can just hear some of you cussing already!  No worries -- no judgement.  I've been there as well!!)

Two important things to keep in mind:

1) If you don't have a permanent STATIC IP address (you'll know if you do because you asked for it specifically from your ISP and are paying extra each month for it), then your IP address is dynamically allocated for you.  You'll need to get the new address, swap it into your program, and try it again.

2) And if number one doesn't fix the issue, problem number two is... dum dum de dum... once again dynamic addresses.  That last step that we did, with the port forwarding...  Remember it?  You forwarded your data to a specific local IP address...   If you don't have that configured as a static address (set up manually instead of automatic), then it may not be the same as it was before either.  You may have to go back and change your port forwarding address once again so it works as you'd expect.
Reply
#2
And, with all that said and posted, here's what I'd recommend:

If you're just going to play around a few times to test the capabilities and learn about things, then don't worry about STATIC vs DYNAMIC stuff.  Set it up, make the few changes needed, get used to your router and interacting with it, and just swap those addresses as needed.

IF you're going to be using the same program repeatedly, however, and you don't want to have to jump through an endless set of hoops, call up your ISP and tell them to swap you over to a STATIC address.  Mine costs me an extra $5.00 per month, and it's worth that little amount extra, just to save me the headache and trouble of having to change numbers after each reboot or update.  

As for the local setting for your PC, those you can configure yourself for no extra cost or anything.  It just takes a little time to tell Windows (or Linux/Mac as your device dictates) to always connect this computer as 10.243.1.77 (or whatever you specifically want it to be.)

Serious web hosting should be done across static connections.  It'll save you a ton of troubleshooting, compared to having to always redo your dynamic setup with each run of the software/reboot of the system.


EDIT: https://www.onmsft.com/how-to/how-and-wh...windows-10 <-- Link to configuring windows to use a STATIC IP locally for your PC/laptop, so it doesn't change with each reboot. (Step 2 with port forwarding that I addressed; not step 1 -- find your internet address. This is for a static local address. You'll need your ISP to give you a static web address.)
Reply
#3
Find your IP4 Address for Windows systems. (See the caution at the top of the code).

Edit: There may be a language difference that prevents the search match. The routine is not case-sensitive, but see, "search$= " for supported search terms.

Code: (Select All)
' CAUTION - THE SHELL STATEMENT WILL OVERWRITE ANY PREEXISTING FILE YOU HAVE IN YOUR LOCAL DIRECTORY NAMED "tmp.tmp".
' Change tmp.tmp to something else if that's a problem.
SHELL _HIDE "ipconfig>tmp.tmp"
OPEN "tmp.tmp" FOR BINARY AS #1
a$ = SPACE$(LOF(1))
GET #1, , a$
CLOSE #1
DO
    j = INSTR(LCASE$(a$), search$)
    IPAddy$ = MID$(a$, j)
    IPAddy$ = MID$(IPAddy$, 1, INSTR(IPAddy$, CHR$(13)) - 1)
    IPAddy$ = _TRIM$(MID$(IPAddy$, INSTR(IPAddy$, ":") + 1))
    IF j AND LEN(search$) > 0 THEN
        PRINT "Your IP Address is: "; IPAddy$
        EXIT DO
    ELSE
        SELECT CASE cnt
            CASE 0
                search$ = "ipv4 address"
            CASE 1
                search$ = "ip address"
            CASE 2
                search$ = "ipv4 "
            CASE 3
                search$ = "ipv4-address"
            CASE 4
                search$ = "ip-address"
            CASE 5
                search$ = "ipv4-"
            CASE ELSE
                PRINT "Sorry, can't find IP addy. Opening Notepad so you can find it..."
                SHELL _DONTWAIT "Notepad tmp.tmp"
                EXIT DO
        END SELECT
    END IF
    cnt = cnt + 1
LOOP

Pete
Reply
#4
The file is created, but is it by design that the output doesn't appear on the console?
Reply
#5
(12-11-2022, 06:02 PM)Kernelpanic Wrote: The file is created, but is it by design that the output doesn't appear on the console?

The output should appear. It does on mine. Let's do this.

------> I edited the above code to search for both IPv4 Address and IP Address.

1) Open the tmp.tmp file in Notepad and look for...

IPv4 Address

IPv4 (space) Address. It doesn't matter what the case is, upper, lower, mixed.

If it isn't there, I'll bet you don't have network on a wireless router, or it is being reported as IP Address, or something similar.

So let me know, and if others are not getting an output, please post either the tmp file, if you don't mind giving out your personal info, or how it is configured, whatever we can be used so we can get an automated method to assign the IP address.

Thanks,

Pete
Reply
#6
Nothing is displayed for the changed file. The tmp.one and tmp.two files are identical. With tmp.one-n, only the IPv4 address has been made unrecognizable.

Code: (Select All)
' CAUTION - THE SHELL STATEMENT WILL OVERWRITE ANY PREEXISTING FILE YOU HAVE IN YOUR LOCAL DIRECTORY NAMED "tmp.tmp".
' Change tmp.tmp to something else if that's a problem.
Shell _Hide "ipconfig>tmp.one"
Open "tmp.one" For Binary As #1
a$ = Space$(LOF(1))
Get #1, , a$
Close #1
IP4Address$ = Mid$(a$, InStr(LCase$(a$), "ipv4 address"))
IP4Address$ = Mid$(IP4Address$, 1, InStr(IP4Address$, Chr$(13)) - 1)
IP4Address$ = _Trim$(Mid$(IP4Address$, InStr(IP4Address$, ":") + 1))
Print "Your IPv4 Address is: ";
Print IP4Address$

End

Code: (Select All)
Windows-IP-Konfiguration


Ethernet-Adapter Ethernet:

   Verbindungsspezifisches DNS-Suffix: fritz.box
   IPv6-Adresse. . . . . . . . . . . : 2001:1a81:52c8:4a00:a86:9c55:2734:b7a1
   Tempor„re IPv6-Adresse. . . . . . : 2001:1a81:52c8:4a00:6011:887f:c7ce:4387
   Verbindungslokale IPv6-Adresse  . : fe80::9e8d:4fa3:2ed1:96c8%14
   IPv4-Adresse  . . . . . . . . . . : 192.---.---.--
   Subnetzmaske  . . . . . . . . . . : 255.255.255.0
   Standardgateway . . . . . . . . . : fe80::2e3a:fdff:fef8:9a1e%14
                                       192.---.---.-

Ethernet-Adapter VirtualBox Host-Only Network:

   Verbindungsspezifisches DNS-Suffix:
   Verbindungslokale IPv6-Adresse  . : fe80::84ec:3c21:6d38:fc30%6
   IPv4-Adresse  . . . . . . . . . . : 192.---.---.-
   Subnetzmaske  . . . . . . . . . . : 255.255.255.0
   Standardgateway . . . . . . . . . :

[Image: IPv4-ermitteln2-Fehler2022-12-11.jpg]
Reply
#7
@Kernalpanic -- it's your language and the search term:  search$ = "ip address"  vs "IPv4-Adresse" Wink

It's not printing anything simply because the search isn't matching and finding anything to report back for you. Not that I'm even certain how the heck you'd get a 192.---.---.- address. Is that something with the virtual machine itself? Or did you edit the output to protect your address yourself? Those dashes are new to me. Wink
Reply
#8
(12-11-2022, 08:26 PM)SMcNeill Wrote: @Kernalpanic -- it's your language and the search term:  search$ = "ip address"  vs "IPv4-Adresse" Wink

It's not printing anything simply because the search isn't matching and finding anything to report back for you.  Not that I'm even certain how the heck you'd get a 192.---.---.- address.  Is that something with the virtual machine itself?  Or did you edit the output to protect your address yourself?  Those dashes are new to me.  Wink

The dashes are mine.  Wink A dash for each number. There is a full IPv4 address.

PS: The Virtual Box: This is the installed Oracle Virtual Box.
Reply
#9
(12-11-2022, 08:47 PM)Kernelpanic Wrote:
(12-11-2022, 08:26 PM)SMcNeill Wrote: @Kernalpanic -- it's your language and the search term:  search$ = "ip address"  vs "IPv4-Adresse" Wink

It's not printing anything simply because the search isn't matching and finding anything to report back for you.  Not that I'm even certain how the heck you'd get a 192.---.---.- address.  Is that something with the virtual machine itself?  Or did you edit the output to protect your address yourself?  Those dashes are new to me.  Wink

The dashes are mine.  Wink A dash for each number. There is a full IPv4 address.

I was wondering!  The only time I've ever seen dashes in an IP address was when it was a range of addresses assigned to someone.

123.45.678.90-9  <-- this means that all numbers from 123.45.678.90 to 123.45.678.99 are all assigned to the same person.

And honestly, I think I've only ever seen that type of format once or twice in my illustrious hobbyist carrier as a programmer.  Smile
Reply
#10
(12-11-2022, 08:55 PM)SMcNeill Wrote:
(12-11-2022, 08:47 PM)Kernelpanic Wrote:
(12-11-2022, 08:26 PM)SMcNeill Wrote: @Kernalpanic -- it's your language and the search term:  search$ = "ip address"  vs "IPv4-Adresse" Wink

It's not printing anything simply because the search isn't matching and finding anything to report back for you.  Not that I'm even certain how the heck you'd get a 192.---.---.- address.  Is that something with the virtual machine itself?  Or did you edit the output to protect your address yourself?  Those dashes are new to me.  Wink

The dashes are mine.  Wink A dash for each number. There is a full IPv4 address.

I was wondering!  The only time I've ever seen dashes in an IP address was when it was a range of addresses assigned to someone.

123.45.678.90-9  <-- this means that all numbers from 123.45.678.90 to 123.45.678.99 are all assigned to the same person.

And honestly, I think I've only ever seen that type of format once or twice in my illustrious hobbyist carrier as a programmer.  Smile

I hardly know anything about TCP/IP anymore. I was busy with it once: TCP/IP for Dummies from 1998, as I just saw.
Reply




Users browsing this thread: 1 Guest(s)