UDP error listening on port

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

I used TCP at first, but it was too slow. And I decided to try UDP. I took the code from this article as a basis. When I started it only on my computer, everything worked. But when it was done on different computers connected by a local network it didn’t work. Although TCP worked. This gives me a port error. Honestly, I don’t know what to do. Maybe I didn’t search well. I will be grateful if you point out my mistake.

I think it’s right to leave the code here.

Here I can choose which mode the program should run in (Server/client):

extends Node2D

func _ready():
	$c.connect("pressed", self, "client_press")
	$s.connect("pressed", self, "server_press")
	$client/apply.connect("pressed", self, "client")
	$server/apply.connect("pressed", self, "server")

func server():
	G.PORT_SERVER = int($server/h_port.text)

func client():
	G.IP_SERVER = str($client/ip.text)
	G.PORT_CLIENT = int($client/c_port.text)
	G.PORT_SERVER = int($client/h_port.text)

func server_press():
	$c.disabled = true

func client_press():
	$s.disabled = true

how it seems

This is a code of client:

extends Node


var socketUDP = PacketPeerUDP.new()

var cron_send = 1

func _ready():

func _process(delta):
	if cron_send > 0:
		cron_send -= delta
		if cron_send <= 0:
			if socketUDP.is_listening():
				socketUDP.set_dest_address(IP_SERVER, PORT_SERVER)
				var stg = "hi server!"
				var pac = stg.to_ascii()
				cron_send = 3

	if socketUDP.get_available_packet_count() > 0:
		var array_bytes = socketUDP.get_packet()
		printt("msg server: " + array_bytes.get_string_from_ascii())

func start_client():
	if (socketUDP.listen(PORT_CLIENT, IP_SERVER) != OK):
		printt("Error listening on port: " + str(PORT_CLIENT) + " in server: " + IP_SERVER)
		printt("Listening on port: " + str(PORT_CLIENT) + " in server: " + IP_SERVER)

func _exit_tree():

And code of server:

extends Node



var socketUDP = PacketPeerUDP.new()

func _ready():

func _process(delta):

	if socketUDP.get_available_packet_count() > 0:
		var array_bytes = socketUDP.get_packet()
		var IP_CLIENT = socketUDP.get_packet_ip()
		var PORT_CLIENT = socketUDP.get_packet_port()
		printt("msg server: " + array_bytes.get_string_from_ascii())
		socketUDP.set_dest_address(IP_CLIENT, PORT_CLIENT)
		var stg = "hi server!"
		var pac = stg.to_ascii()

func start_server():
	if (socketUDP.listen(PORT_SERVER) != OK):
		printt("Error listening on port: " + str(PORT_SERVER))
		printt("Listening on port: " + str(PORT_SERVER))

func _exit_tree():

Maybe it important but global script looks it:
(In here i send params to use it in server or client)

var IP_SERVER = ""
var PORT_SERVER = 1507
var PORT_CLIENT = 1509
:bust_in_silhouette: Reply From: MitchReidNZ

Stupid question, but you’re not trying to use as the IP_SERVER from the client are you? is the loopback address, it’s only a way for the computer to talk to itself.

If your client is on a different computer on the same network, you’ll need to use the local network IP for the server. Probably something like 192.168.X.Y, or 10.0.X.Y
Run ipconfig (for windows) from the command prompt on the server to see what its IP is.
Alternatively, godot probably provides some methods to retrieve your IP.

Assuming I’ve just misread your question, and you’re using the right IP, I’d look at any firewall you have installed first. If that doesn’t seem to be the cause it might be time to bust out wireshark.

Thanks. Your comment prompted me to take a deeper look at my question. It seems the problem was in the logic of the program. However, this helped me. I think that later I will publish somewhere here the final working version.

Andrew | 2020-02-04 17:20

:bust_in_silhouette: Reply From: mobileMojo

The port error for me in these situations usually is because the port is still technically being used even when the host closes the game. It could be a PC thing. But find a way to officially open a port back up if it is not doing do, otherwise you will continue to get the error about the port not able to listen. Hope that helps. I am currently working on this also for LAN connections. Restarting my computer fixes it so that tells me its a port open/close issue.