How does one connect to a network server on PC using a client on an android device?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By johnygames

I have set up a basic network on my PC using this tutorial.
Although I managed to make it work perfectly on my PC by running two instances of Godot (1 server and 1 client) , I still can’t wrap my head around how to make it work with the server being hosted on PC and the client being on an android device. So far I’ve installed the client package on my smartphone and I’ve connected the device to my wifi network. I made sure to use the ipv4 ip address, which I found using ipconfig on Windows. The port number is the same across devices too!
When I try to run this setup, it just doesn’t respond. To make matters worse, it doesn’t even throw any errors. Both the client and the server keep working without any sign of connection between them.
Any insights on this?

Since your server is your PC and you use your Android through your router there may be problem with either your router blocking your requests to your PC or your PC firewall denies external connections to designated port. You should first check route availability. Install some app which allows you to use ping&telnet and try to ping your PC first and then telnet desired port on it using telnet. If it pings but port connection is refused - then its most likely firewall restrictions.

Calamander | 2018-11-10 19:55

Update: I am using the computer as a wifi hotspot for the smartphone to connect to, because the router’s wifi is currently not working for technical reasons. Does this play any role? Is the ip still the same?
Anyway I will look into the whole telnet thing even though I am not sure what exactly I am looking for, and I’ll be back for more info on my progress.

johnygames | 2018-11-10 21:31

First you should check if your server PC IP is reachable from your smartphone. You can do that by pinging its IP from your smartphone.
Then you use telnet to check if your server port is reachable from your smartphone. Telnet is easiest for that purpose, you give it ip and port, its either establishes connection (and waits for your input) or says that connection refused meaning port is closed and you should configure firewall.
By default most routers are preconfigured to allow any kind of connections inside LAN (any IP to any port), it should be the same for PC hotspot.

Calamander | 2018-11-10 21:48

Your PC IP-address is different for each network it is operating in. It can be localhost (127.0.0.1, it can be used for services operating on your computer locally) or something else, depending on how many networks you are connected to (wired, wireless, VPN, virtual machine). So of course first you have to be sure you’re connecting to the same IP your server listening port on.
If your PC is connected to the internet using wire and gives wifi for your phone then it is in at least two networks (excluding local). Your server should listen port on IP in the same network in which it shares wifi.

Calamander | 2018-11-10 22:01

I installed an app called Port Scanner on my smartphone and I checked my host IP for open ports. I found that some ports (like port 6007 and 135) are open. However, when I configure Godot to listen on these ports I still dont get any resuts. I have also played around with various other IPs and ports to no avail. Lastly, I have installed another app called VX Connectbot, which specifically uses a telnet option, but still didn’t succeed in finding what is wrong. Even if I found what ports are blocked, I have no clue as to how to unblock them. This is so frustrating!

johnygames | 2018-11-10 23:27

What is your server socket (IP:port) and smartphone IP?
If its something like 192.168.0.1 and 192.168.0.2 then they are most likely from one network.
If you can ping your server IP, but don’t see open port then you have to allow external connections to this port in your OS firewall settings.

Calamander | 2018-11-10 23:39

Yes, it is 192.168.1.64 and 192.168.137.1. I will see what I can do with firewall settings.

johnygames | 2018-11-10 23:42

It doesn’t look like same network actually. In LAN IPs rarely differs that much.

Calamander | 2018-11-10 23:47

I just checked that Godot Engine is permitted under Firewall Rules & Settings. Other than that I dont know what else I should be checking for. I tried temporarily disabling firewall altogether, but still no results.

johnygames | 2018-11-10 23:48

Which IP is which?

Calamander | 2018-11-10 23:51

Well, according to ipconfig, Ethernet adapter Ethernet: IPv4 = 192.168.1.64
and Wireless LAN adapter: IPv4=192.168.137.1

They both work when both client and server operate on PC.
By the way, I just tried exporting an HTML version of the setup which I ran on my browser on my PC. Guess what, it didn’t work.

johnygames | 2018-11-11 00:02

So both of them are your PC IPs?
Alright, so your phone IP should be something like 192.168.137.2,
on your phone you should try to connect to 192.168.137.1:server_port
and your PC should listen on 192.168.137.1:server_port.
You can check whether this port on your PC is opened by using Ctrl+R cmd netstat command.

Calamander | 2018-11-11 00:21

Ok, so I checked my phone and the IP address I see there is 192.168.137.61, while my PC’s IPs are 192.168.137.1 and 192.168.1.64.

The command prompt tells me the following:

TCP 192.168.137.1:6007 [phone model]:40276 CLOSE_WAIT
TCP 192.168.137.1:6007 [phone model]:40276 CLOSE_WAIT
TCP 192.168.137.1:6007 [phone model]:40693 CLOSE_WAIT
TCP 192.168.137.1:6007 [phone model]:40732 CLOSE_WAIT
TCP 192.168.137.1:6007 [phone model]:60712 CLOSE_WAIT
TCP 192.168.137.1:55459 [PC model]:6007 ESTABLISHED
TCP 192.168.137.1:55466 [PC model]:6007 ESTABLISHED
TCP 192.168.137.1:57107 [PC model]:6007 ESTABLISHED
TCP 192.168.137.1:57137 [PC model]:6007 ESTABLISHED

johnygames | 2018-11-11 00:33

well, if your server is running then there should be its socket with state LISTENING
Do you see your server port at all in netstat output?

Calamander | 2018-11-11 00:35

Nope! My server is running, yet not even one of those outputs has the LISTENING state.

johnygames | 2018-11-11 00:39

What is your server port and what line in your script you use to make server start listening port?

Calamander | 2018-11-11 00:45

And what line of code you use to connect on your client application?

Calamander | 2018-11-11 00:48

func _ready():
	var network = NetworkedMultiplayerENet.new()
	network.create_server(6007, 2)
	get_tree().set_network_peer(network)
	network.connect('peer_connected', self, '_peer_connected')
	network.connect('peer_disconnected', self, '_peer_disconnected')

func _peer_connected(id):
	text = 'User ' + str(id) + ' connected'
	get_parent().get_node('labelUserCount').text = "Total Users: " + str(get_tree().get_network_connected_peers().size())
	
func _peer_disconnected(id):
	text = 'User ' + str(id) + ' disconnected'
	get_parent().get_node('labelUserCount').text = "Total Users: " + str(get_tree().get_network_connected_peers().size())

this is used to create server and output number of connected users

johnygames | 2018-11-11 00:50

and this is used to create a client. I use two textedit nodes to make the whole process of choosing port and ip address dynamic:

func _on_buttonConnect_pressed():
	var ip = get_node("../").get_node("ipNumber").text
	print(ip)
	var port = get_node("../").get_node("portNumber").text
	print(port)
	var network = NetworkedMultiplayerENet.new()
	network.create_client(ip, int(port))
	get_tree().set_network_peer(network)

johnygames | 2018-11-11 00:53

Well, according to your netstat output you actually have multiple successful connections from your client to your server (in TIME_WAIT state, waiting for client to send close connection command) and vise versa (ESTABLISHED, meaning client ready to get commands from server). What exactly does not work?

Calamander | 2018-11-11 00:55

When I press the connect button on the client, the server is supposed to output that a user is connected. This happens only when server and client operate on PC. When the client is on phone, nothing happens. No matter what IP or port I use, pressing the connect button does not yield any results whatsoever.

EDIT: The ‘TCP 192.168.137.1:6007 [phone model]:60712 CLOSE_WAIT’ command shows up even when wifi is switched off on phone.

johnygames | 2018-11-11 00:59

allright. Try the following. Close your server and client, maybe even reboot both PC and phone (for the sake of insurance).
Use netstat.
Save its output.
Start server (192.168.137.1:6007).
Use netstat. Check if your server is there and what is its state, is it LISTENING. Save netstat output.
Start your client, connect to the server. Use netstat, see whats new.
From what I can see it does connects but doesn’t give you desired output.
You should debug your code. First of all check your create_client(…) output, it returns you valuable state information on whether it is connected or not. Check documentation:
NetworkedMultiplayerENet — Godot Engine (3.0) documentation in English
After you do this and if you still haven’t solved your problem - write me back.

Calamander | 2018-11-11 01:07

Ok, will do. Thank you for all the effort you put into this!

johnygames | 2018-11-11 01:15

Update: I restarted both PC and phone and ran netstat. This time the connections to the phone don’t even show up!
‘TCP 192.168.137.1:6007 [phone model]:60712 CLOSE_WAIT’ and
‘TCP 192.168.137.1:55459 [PC model]:6007 ESTABLISHED’ dont appear anymore.

As for the debugging, I created a label inside the client app, which outputs the result of the print(create_client) statement whenever I press the connect button. It always outputs 0.

johnygames | 2018-11-11 13:31

0 means “OK”. (see Error enum here: @globalscope)
But is server in LISTENING state (or any other) 6007 port at any point?
If your phone doesn’t even connect anymore but says it does you can check values refuse_new_connections, transfer_mode, return value of get_connection_status() of your network variable on your client.
You can also try to send/recieve something over your connection and should always check return values of used methods if there is any return value.
This is your manuals, all of its methods/properties are available to you:
NetworkedMultiplayerENet
NetworkedMultiplayerPeer
PacketPeer
You should also make server print as much information as it can. Maybe in addition to binding every network related event to some function ouputting something set Timer on the server for every 2-5 seconds and make it print all sorts of output which is not related to events like connection state, number of peers/packets/errors/etc.
There is also a number of network related methods available in SceneTree instance:
SceneTree
For some reason method set_network_peer is not listed there, but I assume it is here nevertheless, since it works for you locally. You can always check SceneTree network_peer member contents to see if it successfully binded after you created your server.

Calamander | 2018-11-11 16:43

Btw, I assumed you use latest stable Godot version 3.0.6. Is is true?

Calamander | 2018-11-11 16:45

Indeed, 3.0.6 is what I use. I will try to implement your ideas and I’ll come back. I should also mention that none of the ports/IPs has the LISTENING state.

johnygames | 2018-11-11 17:18

If there is no server socket in any state then its simply not working for some reason. There is no point in trying to connect to it until it works. Your first priority should be making your server work using desired port again.

Calamander | 2018-11-11 17:34

I can’t get desired port to listen. Also I added the following line in my code:

onready var network = NetworkedMultiplayerENet

This is so that I can reference my network in the _process() function.

I made a timer to output the network.get_connection_status() and it always outputs 2, even when nothing is connected to anything…

johnygames | 2018-11-11 18:43

I misled you, sorry. NetworkedMultiplayerENet actually use UDP connections rather then TCP, so it won’t show any state for UDP connection, because it just sends packets without confirming its acquisition.
So to determine if your server opened its port you should use:

netstat -a -p UDP

It shows us UDP connections only. It should be listed here after you start it under 0.0.0.0:6007 or something like that. Note that godot uses TCP port 6007 for remote debug, so to avoid confusion you’d better use another port for your UDP server. To connect to server you still should use 192.168.137.1 because its your PC’s external IP for the network your phone is in.
Returning 2 from get_connection_status is ok for server, it means its running and waiting for incoming UDP packets. But your client won’t return status 2 until it successfully connects to server (0 - not connected, 1 - connecting, 2 - connected), so you should pay attention to client output.
You may want to use get_tree().get_network_connected_peers().size() in your server timer output in addition to connection status to see if anyone connected.

Calamander | 2018-11-11 20:07

I updated the client to show the get_connected_status() output. It always shows 1 no matter if it connected to anything or not. I also used the netstat -a -p UDP command and I got the following in all cases (connected-not connected):

Active Connections

Proto Local Address Foreign Address State
UDP 0.0.0.0:53 :
UDP 0.0.0.0:123 :
UDP 0.0.0.0:148 :
UDP 0.0.0.0:500 :
UDP 0.0.0.0:4500 :
UDP 0.0.0.0:5050 :
UDP 0.0.0.0:5353 :
UDP 0.0.0.0:5355 :
UDP 0.0.0.0:27036 :
UDP 0.0.0.0:49972 :
UDP 0.0.0.0:49974 :
UDP 0.0.0.0:51659 :
UDP 0.0.0.0:61000 :
UDP 0.0.0.0:62513 :
UDP 127.0.0.1:1900 :
UDP 127.0.0.1:51660 :
UDP 127.0.0.1:61002 :
UDP 127.0.0.1:61692 :
UDP 192.168.1.64:137 :
UDP 192.168.1.64:138 :
UDP 192.168.1.64:1900 :
UDP 192.168.1.64:5353 :
UDP 192.168.1.64:61691 :
UDP 192.168.137.1:67 :
UDP 192.168.137.1:68 :
UDP 192.168.137.1:137 :
UDP 192.168.137.1:138 :
UDP 192.168.137.1:1900 :
UDP 192.168.137.1:5353 :
UDP 192.168.137.1:61690 :

I tried using port number 148 (random selection) and I still can’t connect.

johnygames | 2018-11-11 21:09

There is your port:
UDP 0.0.0.0:148 :
Though you shouldn’t use ports with number less then 1025 because they are reserved for system services.
Lets see. Port is open, but you seem to unable to connect. Try steps from the beginning, ping server IP from your phone and then scan server port. If it works then there must be something wrong with your client application, if it doesn’t - then you provided wrong ip/port or something is blocking your connection.
Try testing same client on your PC with predefined ip+port and then compile and test it on phone.
Can you write your contacts? Skype maybe?

Calamander | 2018-11-11 21:22

:bust_in_silhouette: Reply From: Calamander

Damn, turns out the problem was with blocking, but from somewhere else entirely. I completely forgot that Android has permission system for every little thing so the problem is you just need to enable two permissions in export settings of your project before compiling:
ACCESS_NETWORK_STATE and INTERNET
Maybe even only INTERNET is enough, i’m not sure.

Oh my god! It actually worked! What are you, the network whisperer or something? Thank you, you saved me from a lot of trouble and frustration. Now I’ll have to work on creating the actual game, which I am sure is going to be a pain. But the first step is taken. Thanks!

johnygames | 2018-11-12 01:34

Hi, Calamander! Can you help me with same question, but pc using wifi, android - mobile net, please?

Nazym | 2019-04-18 18:58

wow never expected it to be as simple as this. I was going all over the place trying to find where the port is blocked or something

Tooniis | 2020-06-05 14:58

How did you manage to do it I’m really stuck everything is fine on my pc and I code but it doesn’t connect

lurgx | 2022-06-04 07:59

I solved it
It seems that we have to look closely at the IP of the PC directly in the ethernet state, there is the real IP while Godot, due to my bad configuration, throws me another IP so that it is already there
In addition to that we must give permissions to the app before exporting with godot
Internet and Access network satate
With that the connection between pc and android is successful greetings!

lurgx | 2022-06-04 09:09

Unfortunately in my case, it doesn’t work even I set its permission like what you stated. I want to give a link to my issue here. I’ve enabled permissions on the Internet and Access_Network_State. I exported it and downloaded it on my phone AND exporting it (Android) directly in the play buttons of the editor and NONE of them connects to my server that is cross platform as Windows 10.

I really don’t know what’s going on with my case I feel like passing out solving this.

KramchayDig | 2022-08-08 05:37