0 votes

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);
asked Nov 18, 2018 in Engine by eduo.abreu (14 points)
edited Nov 18, 2018 by eduo.abreu

1 Answer

0 votes

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())
answered Nov 18, 2018 by Oen44 (355 points)
edited Nov 18, 2018 by Oen44

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

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);

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

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.

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

Check answer edit.

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

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.
https://social.msdn.microsoft.com/Forums/en-US/92846ccb-fad3-469a-baf7-bb153ce2d82b/simple-udp-example-code?forum=netfxnetcom

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!

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.