Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: TcpSocket connect on Android does not behave correctly  (Read 773 times)

0 Members and 1 Guest are viewing this topic.

Unicode

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
TcpSocket connect on Android does not behave correctly
« on: June 01, 2024, 02:08:09 pm »
Hello everybody!
Working with the TcpSocket for some time now, I encountered a couple of problems using it. I think I will make new posts for the other ones in the future, but this one is really wierd and makes it kind of unusable.

Problem:
Whenever I initialize a TcpSocket on Android and call its connect() method, wierd things happen. With my local ip it works just fine. It waits the specified amount of time for a connection and returns either status Error or status Done (just as you'd expect). This is the case on both Windows and Android. But with some (including my) public ips (just!) on Android, it seems to always recognize the ip as incorrectly formatted or something like that and returns status Disconnected immediately.
This is wierd because the exact same code behaves perfectly fine on Windows (and mabye other OSs), but becomes unusable on Android if you want to have it initiate a connection (the sf::TcpListener works fine too).

Minimal example (same for Android as for Windows):
#include <SFML/Networking.hpp>

int main() {
    sf::TcpSocket socket;
    socket.connect("SomePublicIp", 42069, sf::seconds(10));
   
    return 0;
}
 

Additional info:
I used Visual Studio 2022 and SFML-2.6.1 on Windows and the (btw SUPER convenient) App CxxDroid with SFML-2.5.1 already setup on Android. The version difference shouldn't matter since the changelog shows that only very little in the networking module changed, nothing to do with this problem I think.

Also (if it matters) I have set up port forwarding and am on different networks (one being my home router and the other one my phones mobile network). I additionally even restarted my router to get a new public Ip, but it doesn't work with this new one either. It now works (i.e. waits 10 seconds and returns status Error instead of instant status Disconnected) though with my old Ip, just as my router switched Ips! What's going on here?

Any help will be appreciated. Thanks in advance! ;D

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10930
    • View Profile
    • development blog
    • Email
Re: TcpSocket connect on Android does not behave correctly
« Reply #1 on: June 01, 2024, 07:54:16 pm »
I've little experience with Android, so I don't have any special insights. ;D
A quick web search led me to INTERNET and ACCESS_NETWORK_STATE permissions. Did you set those in the manifest?

I assume, you're connected via WLAN, correct?
You could try to run Wireshark on your home network and/or phone, to see what's actually going on on the wire itself.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Unicode

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: TcpSocket connect on Android does not behave correctly
« Reply #2 on: June 02, 2024, 08:04:05 pm »
Thanks a lot!

Yeah I've had a look into both. The manifest and everything else is pretty much hidden and generated by the compiler App behind the scene. All one does to code a SFML program is to include it and write code for it. No set up or anything required, but therefore also not accessible.
Wireshark also just confirms that nothing actually leaves the device. The TcpSocket seems to decide when seeing my routers ip: Nope, (almost) every other Ip is fine but this one, naah.

I don't think some other permissions or requirements are the problem either, since it perfectly works with the UdpSocket, the TcpListener and the TcpSocket itself even, though just not when calling the connect() method on a (my) public Ip.

I also couldn't find any other post about this issue, although some have mentioned not being able to connect to public Ips via Tcp. Sadly these threads don't go to much into detail and are left abandoned after a short amount of time.

Could I somehow see where the status Disconnected comes from exactly? I could only pinpoint it down to the two calls ::connect() and select() in the TcpSocket connect() method but can't find where those themselves are coming from.
« Last Edit: June 02, 2024, 08:07:19 pm by Unicode »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10930
    • View Profile
    • development blog
    • Email
Re: TcpSocket connect on Android does not behave correctly
« Reply #3 on: June 02, 2024, 10:34:03 pm »
The TcpSocket seems to decide when seeing my routers ip: Nope, (almost) every other Ip is fine but this one, naah.

... just not when calling the connect() method on a (my) public Ip.
Can you describe your setup again in more detail?

If you're trying to call your own public IP within the same network and it doesn't work, then this might actually just be a NAT loopback issue. Your router may need to be configured to directly route packets back into the network that address itself.

Regarding Wireshark, if you're tracking the network from your own device, makes sure you're scanning the whole network and if you're unlucky, you might not even see the wifi chatter on the network.

Could I somehow see where the status Disconnected comes from exactly? I could only pinpoint it down to the two calls ::connect() and select() in the TcpSocket connect() method but can't find where those themselves are coming from.
You could try to get more information from the specific that is mapped in SFML: https://github.com/SFML/SFML/blob/2.6.x/src/SFML/Network/Unix/SocketImpl.cpp#L89
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Unicode

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: TcpSocket connect on Android does not behave correctly
« Reply #4 on: June 03, 2024, 10:27:20 pm »
My setup is fairly simple:
- My Anroid tablet using the c++ compiler app CxxDroid running the above code with SFML-2.5.1 preinstalled (no setup at all apart from installing the app)
- My Windows PC using a basic set up Visual Studio 2022 project with SFML-2.6.1 linked, etc., running the exact same (already mentioned) code
Should I go more into detail about my PC setup? Because everything works there. It is just Android that's wierd and I just have it as a reference :).

Yeah I know about NAT loopback, which is why I connect my tablet to a hotspot from my phone and try to connect to my home routers Ip (so there should be two different networks in play here).
I will have a look at wireshark again, but I am wondering (also regarding NAT loopback), if it even makes sense to check those. I mean better safe than sorry but is there something about the Tcp connect method that is so different from all the other networking stuff to not be working because of these? Asking since nothing else has problems and isn't the TcpSocket with its connect() method using the same stuff the other networking features use? (I know what TCP is and what it does. I hope you get my point.)

Again thanks for helping me here!
(Offtopic: And while already talking to one, if not *the* head of SFML: I love what you all have built here. SFML is such a great library, thanks for developing and taking care of it! I hope I someday will be able to contribute something and help :).)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10930
    • View Profile
    • development blog
    • Email
Re: TcpSocket connect on Android does not behave correctly
« Reply #5 on: June 15, 2024, 11:49:45 pm »
I don't think, I can help you much more from my side of things.

It's unclear to me where the signal is lost, which is kind of crucial to drill deeper and understand why it's lost there.

I'd probably start systematically checking all the paths (PC, router, mobile network, phone, etc.).
Maybe even try some simple TCP program in Android's native language or at least without SFML to try and see if there is the issue.

(Offtopic: And while already talking to one, if not *the* head of SFML: I love what you all have built here. SFML is such a great library, thanks for developing and taking care of it! I hope I someday will be able to contribute something and help :).)
Glad to hear and happy to help! :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/