Author Topic: OpenClient trouble  (Read 2489 times)

0 Members and 1 Guest are viewing this topic.

Offline awkutchins

  • Newbie
  • Posts: 12
    • View Profile
OpenClient trouble
« on: May 29, 2020, 11:50:55 am »
Hello reader,
I am trying to get into networking and am starting simple by just trying to connect two programs on two different computers (my pc and a rpi). The rpi has this code:
Code: QB64: [Select]
  1. hostHandle = _OPENHOST("TCP/IP:8080")
  2. result$ = _CONNECTIONADDRESS$(hostHandle)
  3. LOCATE 1, 1: PRINT _CONNECTED(hostHandle)
  4. LOCATE 3, 1: PRINT result$

And my PC has:
Code: QB64: [Select]
  1. port = 8080
  2. counter = 0
  3.     'port = port + 1
  4.     counter = counter + 1
  5.     LOCATE 1, 1: PRINT port; counter
  6.     clientHandle& = _OPENCLIENT("TCP/IP:8080:127.0.0.1")
  7. LOOP UNTIL clientHandle& <> 0 OR counter > 100

With the rpi (host) program running (and the _OPENCLIENT address comes directly from result$), I try to run the client program. It tries to connect to the port 100 times but it still comes back with clientHandle& = 0.

My question is are there any settings in my pc I have to adjust in order to connect? What else might give the _OPENCLIENT trouble connecting?

Thank you

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: OpenClient trouble
« Reply #1 on: May 29, 2020, 12:37:44 pm »
until someone comes to assist you, perhaps this info on connecting a windows PC with the PI might give you some ideas https://www.circuitbasics.com/how-to-connect-to-a-raspberry-pi-directly-with-an-ethernet-cable/

Offline awkutchins

  • Newbie
  • Posts: 12
    • View Profile
Re: OpenClient trouble
« Reply #2 on: May 29, 2020, 12:47:59 pm »
Hey Jack!
I am planning to connect a few computers to the pi at the same time on different local networks so I'm not sure if that is the best solution. I guess I can connect the two when I'm coding and testing locally but switch to wifi when the server is up and running (but at that rate I should just stick with wifi right?)
I appreciate it tho!

Offline Real2Two

  • Newbie
  • Posts: 18
    • View Profile
Re: OpenClient trouble
« Reply #3 on: May 29, 2020, 02:16:36 pm »
I'm not too sure but:
1. rpi and computer might not be connecting
2. I feel like this would be spamming OPENCLIENT... I think you should only connect the client once and use GET...

Offline awkutchins

  • Newbie
  • Posts: 12
    • View Profile
Re: OpenClient trouble
« Reply #4 on: May 29, 2020, 10:38:17 pm »
Even if _openclient returns zero? I would assume something other than qb64 is wrong I just don't know how to narrow down the problem

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: OpenClient trouble
« Reply #5 on: May 30, 2020, 04:29:05 am »
127.0.0.1 is the localhost address; connections addressed to it do not leave the computer.

You will need to replace that with the Pi's address as given by `ip addr` for the wifi interface.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: OpenClient trouble
« Reply #6 on: May 30, 2020, 08:53:33 am »
127.0.0.1 is the localhost address; connections addressed to it do not leave the computer.

You will need to replace that with the Pi's address as given by `ip addr` for the wifi interface.

And make certain ports are open, firewalls aren't blocking anything, and all that good junk as well...
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline awkutchins

  • Newbie
  • Posts: 12
    • View Profile
Re: OpenClient trouble
« Reply #7 on: May 30, 2020, 01:08:22 pm »
So I looked around on the interweb and such and did a few things:
1. I typed "ip addr" and found the line inet 192.168.1.153/24 brd 192.168.1.255, and I assume the ip I use is the 192.168.1.153?
2. I went into my router settings and allowed port range forwarding for 52030-53030 to go to the above ip address.
3. I found my ipv6 address for the pi as fe80::b3e7:3018:6543:17a2/64 and I assume the address is everything before the /64?
4. I added that address into the firewall settings for my router to allow the above ports
5. I installed ufw on the rpi and ran "sudo ufw allow 53020"
6. I changed the port value in both programs to reflect the new port (and changed the IP address on the client side to the ipv4)

Still I can't get the client to connect. What other processes/hurdles need to be done that might stop this from working?

Offline awkutchins

  • Newbie
  • Posts: 12
    • View Profile
Re: OpenClient trouble
« Reply #8 on: May 30, 2020, 01:13:13 pm »
I have not done anything for my pc (client). Would I have to open the port on it too?

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: OpenClient trouble
« Reply #9 on: May 30, 2020, 09:21:11 pm »
Quote
1. I typed "ip addr" and found the line inet 192.168.1.153/24 brd 192.168.1.255, and I assume the ip I use is the 192.168.1.153?
Yes.
Quote
I found my ipv6 address for the pi as fe80::b3e7:3018:6543:17a2/64 and I assume the address is everything before the /64?
IPv6 addresses beginning with fe80 are a special link-local address that Linux always assigns. "Real" addresses begin with a 2. There's no need to bother with anything IPv6 related here.
Quote
went into my router settings and allowed port range forwarding for 52030-53030 to go to the above ip address.
You're not connecting from the outside internet, so port forwarding is neither applicable nor relevant.

Your server code should be calling _OPENCONNECTION repeatedly to accept new incoming connections. _CONNECTED simply tests whether a read or write on an existing connection failed or not.

See https://www.qb64.org/forum/index.php?topic=2378.0 for the full details of what the commands do. I'm sure there's also demos floating around that someone will post a link to as well.

Offline awkutchins

  • Newbie
  • Posts: 12
    • View Profile
Re: OpenClient trouble
« Reply #10 on: May 31, 2020, 01:00:35 am »
Would it be relevant if I were trying to connect from the outside internet? I'm not currently but I plan to expand it to have my three friends' computers (and mine) connected all on different networks.

I updated my server code to:
Code: QB64: [Select]
  1. PRINT "Connecting to port..."
  2. hostHandle& = _OPENHOST("TCP/IP:53020")
  3. hostName$ = _CONNECTIONADDRESS$(hostHandle&)
  4. PRINT "Host name: "; hostName$
  5.  
  6. message$ = "Hello World"
  7.    clientHandle& = _OPENCONNECTION(hostHandle&)
  8.    LOCATE 5, 1: PRINT "ClientHandle from _openconnection: "; clientHandle&
  9.  
  10.    IF clientHandle& <> 0 then
  11.       PUT clientHandle&, , message$
  12.    END IF
  13.  
  14.    LOCATE 10, 1: PRINT TIMER
  15. LOOP UNTIL clientHandle& <> 0

And my client code to:
Code: QB64: [Select]
  1. FOR i = 1 TO 100
  2.     LOCATE 1, 1: PRINT "Connecting to port..."; i
  3.     clientHandle& = _OPENCLIENT("TCP/IP:53020:192.168.1.153")
  4.     LOCATE 2, 1: PRINT clientHandle&
  5.     IF clientHandle& <> 0 OR i > 100 THEN EXIT FOR

I am getting TCP/IP:53020:127.0.0.1 as hostName$. I'm wondering if the _OPENHOST isn't working properly if it's not saying the actual IP

Offline awkutchins

  • Newbie
  • Posts: 12
    • View Profile
Re: OpenClient trouble
« Reply #11 on: June 01, 2020, 02:33:41 pm »
Ok so an update:
I found a nice demo in another forum post here: https://www.qb64.org/forum/index.php?topic=1389.msg105754#msg105754
I copied the code into to qb64 programs on my pc and it worked beautifully.

I then moved the host program to my raspberry pi and changed the ip address on the client program (I tried a few) and still the same problem.

I then tried to see if I could get both programs to on the pi and it can't connect. So I think it might be something with the Pi not being able to listen to a port properly or a missing library or something. I'm not too sure where to go from here tho.

After work I'm going to try to run the client program on a different pc and the host on my pc to see if I can get a connection on different devices period.

If anyone has any ideas or places for me to look I will be so grateful.