Advertisement

[Monogame] lidgren not creating connection

Started by May 26, 2015 06:18 PM
1 comment, last by MiniJack 9 years, 3 months ago

Hi, I have a problem with lidgren not creating a connection between a server and client.

the port is set to 7253.

The netPeerConfiguration is set to "GameEngine"

And the server does allow ConnectionApproval

Full source code can be found here: CODE

Server Code:


public class NetworkConnection
    {
        public NetClient _client;
        public string debug = "";

        public bool isEstablished = false;
        public bool needToEstablish = false;
        public DateTime ConnectionStarted;

        public void Start()
        {
            debug = "Connecting...";
            var loginInformation = new NetworkLoginInformation() { Name = "USER" };
            _client = new NetClient(new NetPeerConfiguration("GameEngine") { Port = 7253 });
            _client.Start();
            var outmsg = _client.CreateMessage();
            outmsg.Write((byte)PacketType.Login);
            outmsg.WriteAllProperties(loginInformation);
            _client.Connect("localhost", 7253, outmsg);

            ConnectionStarted = DateTime.Now;
            needToEstablish = true;
        }

        // Called every tick that needToEstablish is true.
        public bool EstablishInfo(NetClient _client)
        {
            
            NetIncomingMessage inc;
            
             if ((inc = _client.ReadMessage()) != null)
            {
                debug = "Message received!";
                Debug.WriteLine(inc);
                Debug.WriteLine(inc.MessageType);
                switch (inc.MessageType)
                {
                    case NetIncomingMessageType.Data:
                        debug = "Received the right type of message!";
                        var data = inc.ReadByte();
                        if (data == (byte)PacketType.Login)
                        {
                            var accepted = inc.ReadBoolean();
                            if (accepted)
                            {
                                debug = "Connection accepted!";
                                return true;
                            }
                            else
                            {
                                return false;
                            }
                        }
                        else
                        {
                            return false;
                        }
                    case NetIncomingMessageType.DebugMessage:
                        Debug.WriteLine("Debug: " + inc.ReadString());
                        break;
                    default:
                        Debug.WriteLine("Unrecognized message type! (" + inc.MessageType + ")");
                        break; 

                }
            }
            return false;
            
        }
    } 

Client Code:


        public void Init(Engine engine)
        {
            // only run once. on the first time the button is clicked
            config = new NetPeerConfiguration("GameEngine") { Port = 7253 };
            config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);

            server = new NetServer(config);
            // server.Start() is called in a different function.
            running = true;
            engine._networkConnection.debug = "Starting Server...";

        }

        // this function is called every tick while the server is active
        public override void Update(Engine engine, float dt) 
        {      
            NetIncomingMessage inc;

            if ((inc = server.ReadMessage()) != null)
            {
                switch (inc.MessageType)
                {
                    case NetIncomingMessageType.Error:
                        break;
                    case NetIncomingMessageType.StatusChanged:
                        break;
                    case NetIncomingMessageType.UnconnectedData:
                        break;
                    case NetIncomingMessageType.ConnectionApproval:
                        engine._networkConnection.debug =  ("New Connection...");
                        var data = inc.ReadByte();
                        if (data == (byte)PacketType.Login)
                        {
                            engine._networkConnection.debug = ("... Connection Accepted");
                            var loginInformation = new NetworkLoginInformation();
                            inc.ReadAllProperties(loginInformation);
                            inc.SenderConnection.Approve();

                            var outmsg = server.CreateMessage();
                            outmsg.Write((byte)PacketType.Login);
                            outmsg.Write(true);
                            server.SendMessage(outmsg, inc.SenderConnection, NetDeliveryMethod.ReliableOrdered);
                        }
                        else
                        {
                            inc.SenderConnection.Deny("Didn't send correct information.");
                            engine._networkConnection.debug = "Didn't send correct information.";
                        }
                        break;
                    case NetIncomingMessageType.Data:
                        break;
                    case NetIncomingMessageType.Receipt:
                        break;
                    case NetIncomingMessageType.DiscoveryRequest:
                        break;
                    case NetIncomingMessageType.DiscoveryResponse:
                        break;
                    case NetIncomingMessageType.VerboseDebugMessage:
                        break;
                    case NetIncomingMessageType.DebugMessage:
                        break;
                    case NetIncomingMessageType.WarningMessage:
                        break;
                    case NetIncomingMessageType.ErrorMessage:
                        break;
                    case NetIncomingMessageType.NatIntroductionSuccess:
                        break;
                    case NetIncomingMessageType.ConnectionLatencyUpdated:
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }
            }      
        }

I think the obvious first place to start is to Console.Write out the DebugMessage, WarningMessage, and ErrorMessages to see if there is something obvious you are missing. Next, if this doesn't work, then remove the connection approval portion and try a simple send & receive test to see if you can get this to work.

It is also helpful to Console.Write out each incoming packet you receive so you can see where your messages had stopped sending or receiving.

Advertisement

I think the obvious first place to start is to Console.Write out the DebugMessage, WarningMessage, and ErrorMessages to see if there is something obvious you are missing. Next, if this doesn't work, then remove the connection approval portion and try a simple send & receive test to see if you can get this to work.

It is also helpful to Console.Write out each incoming packet you receive so you can see where your messages had stopped sending or receiving.

Thanks for your response, by outputting the debug information I found that the server was not receiving anything from the client, this led me to believe that there was a network configuration problem, so I changed the port and forwarded that one too, and now it work.

Thanks for the tip with the DebugMessage, that really helped me solve my problem.

This topic is closed to new replies.

Advertisement