Conecting Godot to External Server

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

Hey guys, i’m having trouble connecting godot to an external server written in c# in a visual studio solution, I’m using the PacketPeerUDP, but the thing is, it tells me that the server is online when the server is not online, how can I connect the godot client to the server and receive data from it like an array?
This is my code:

extends Node
var IP_SERVER = "127.0.0.1"
var PORT_SERVER = 11000
var PORT_CLIENT = 5500
var socketUDP = PacketPeerUDP.new()

func _ready():
    start_client()

func _process(delta):
	if socketUDP.is_listening():
		socketUDP.set_dest_address(IP_SERVER, PORT_SERVER)
		var stg = "oi servidor!"
		var pac = stg.to_ascii()
		socketUDP.put_packet(pac)

	if socketUDP.get_available_packet_count() > 0:
		var array_bytes = socketUDP.get_packet()
		print("Server Msg: " + array_bytes.get_string_from_ascii())

func start_client():
	if (socketUDP.listen(PORT_CLIENT, IP_SERVER) != OK):
		print("Erro ao escutar na porta: " + str(PORT_CLIENT) + " no servidor: " + IP_SERVER)
		$Label.set_text("O SERVER ESTÁ: INDISPONIVEL")
	else:
		print("Ouvindo na porta: " + str(PORT_CLIENT) + " no servidor: " + IP_SERVER)
		$Label.set_text("O SERVER ESTÁ: DISPONÍVEL")
		
func _exit_tree():
    socketUDP.close()

Sorry if this is simple, i’m a beginner in gdscript! Thanks in advance!
Ps. English is not my main language, sorry if I butchered something.

Edit:
The Server part in the c# solution:

int start = 0;
        int startLeft = 0;
        private const int listenPort = 5500;       
        #endregion
        #region Constructor
        public MainWindow()
        {
            //udp
            UdpClient listener = new UdpClient(listenPort);
            IPEndPoint groupEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), listenPort);
            byte[] response = Encoding.ASCII.GetBytes("Message from Server");
            listener.Send(response, response.Length, groupEP);
            send_to_address = IPAddress.Parse("127.0.0.1");
            sending_end_point = new IPEndPoint(send_to_address, 11000);

ola amigo, então, viu sua situação, eu iria te sugerir uma coisa, escreva seu server no nodejs com dgram(UDP), é bem mais simples de fazer do que dessa forma que você fez…
ja no seu jogo… não sei como que funciona essa variavel PORT_CLIENT, mas no server(DGRAM) tem como tu pegar a porta e ip do cliente que se conectou ao server…
qualquer coisa me chama no whatsapp +351 911 143 162, em que eu poder ajudar, estamos aqui! falow

João Ribeiro | 2020-03-17 23:10

:bust_in_silhouette: Reply From: Oen44

Make sure to use different port for Client and Server.
Right now you are trying to connect to server on port 5000, using the same port for your client connection. Use any (free) port different than server and it’s going to work.

#Edit
Send from server (C#) to GoDot.

UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
byte[] response = Encoding.ASCII.GetBytes("Message from Server");
listener.Send(response, response.Length, groupEP);

Receive packet from server in GoDot:

func _process(delta):
    if socketUDP.get_available_packet_count() > 0:
        var array_bytes = socketUDP.get_packet()
        print("Server Msg: " + array_bytes.get_string_from_ascii())

How can I know what is the port of the server?

eduo.abreu | 2018-11-18 18:06

Well, if you made one using C#, then it’s there in source.
For example:

private const int listenPort = 11000;
new UdpClient(listenPort);
new IPEndPoint(IPAddress.Any, listenPort);

Oen44 | 2018-11-18 18:08

How can i do that in godot’s gdscript? I can’t find in the documentation

eduo.abreu | 2018-11-18 18:14

i’m having trouble connecting godot to an external server written in c# in a visual studio solution

Well, you made server, right? It’s written in C# so I guess you are using UdpClient and IPEndPoint classes. There is nothing to look for in GoDot. Open your server solution using Visual Studio, look for port and use it in GDScript. Just remember to use different port for Client.

Oen44 | 2018-11-18 18:18

Okay, I changed those things, but how can I send information from the c# solution to godot and check if it’s working properly?

eduo.abreu | 2018-11-18 18:30

Check answer edit.

Oen44 | 2018-11-18 19:10

I had tried that but it didn’t work, I posted in the edit the code for the server

eduo.abreu | 2018-11-18 21:16

No, no, no. You are doing it wrong. Use Google next time and read about basic stuff before asking questions. You are now copy-pasting code without knowledge about what’s going on. Why don’t you just learn stuff. You won’t get anywhere by copying someones code.
Simple UDP example code

Oen44 | 2018-11-18 21:25

I’m sorry but what am i doing wrong? I’m really not a c# developer so sorry if this is all coming to you as dumb haha, i’m just trying to glue this things together from a different project, this code is set on the MainWindow.xaml.cs, thanks for the link, i’ll be checking it out!

eduo.abreu | 2018-11-19 01:10

Check example I linked in above comment, you will see there what are you doing wrong, which is copying-pasting and hoping it’s going to work.
BTW. Servers are supposed to be headless (command line, console app) for better performance, not with windows and buttons and stuff.

Oen44 | 2018-11-19 11:21