Advertisement

Can't get Lidgren client to connect to local server

Started by May 06, 2015 01:41 PM
1 comment, last by CirdanValen 9 years, 6 months ago

I'm trying to get Lidgren working, however my client won't connect to the server for some reason. Server doesn't receive any messages, so I'm guessing the problem is entirely with the client. Client gets a "StatusChanged" message, but client never connects. Full source here:

Server


        static void Main(string[] args)
        {
            Console.WriteLine("Otrera Server");

            NetPeerConfiguration config = new NetPeerConfiguration("OtreraServer");
            config.Port = 14242;
            config.MaximumConnections = 5;
            NetServer server = new NetServer(config);

                server.Start();

            if(server.Status == NetPeerStatus.Running)
            {
                Console.WriteLine("Server is running on port " + config.Port);
            }
            else
            {
                Console.WriteLine("Server not started...");
            }
            

            NetIncomingMessage msg;
            while(true)
            {
                server.WaitMessage(500);
                while((msg = server.ReadMessage()) != null)
                {
                    switch(msg.MessageType)
                    {
                        case NetIncomingMessageType.ConnectionApproval:
                            msg.SenderConnection.Approve();
                            Console.WriteLine("Connection Approved");
                            break;
                        case NetIncomingMessageType.StatusChanged:
                            Console.WriteLine("Status changed");
                            break;
                        case NetIncomingMessageType.Data:
                            Console.WriteLine("Recieved message");
                            break;
                        default:
                            Console.WriteLine("Unkown message of type"+msg.MessageType.ToString());
                            break;
                    }
                    
                }
            }
            
        }

Client:


            if(client == null)
            {
                NetPeerConfiguration config = new NetPeerConfiguration("OtreraClient");
                client = new NetClient(config);
                client.Start();

                Thread.Sleep(500);

                client.Connect("localhost", 14242);
                
                

                Thread.Sleep(500);

            }

            NetIncomingMessage msg;

            while((msg = client.ReadMessage()) != null)
            {
                switch(msg.MessageType)
                {
                    case NetIncomingMessageType.DiscoveryResponse:
                        //client.Connect(msg.SenderEndPoint);
                        break;
                    case NetIncomingMessageType.Error:
                        Console.WriteLine("Connection error");
                        break;
                    case NetIncomingMessageType.StatusChanged:
                        Console.WriteLine("Status changed");
                        break;
                    case NetIncomingMessageType.Data:
                        Console.WriteLine("Received message");
                        break;
                    case NetIncomingMessageType.DebugMessage:
                        Console.WriteLine("DebugMessage");
                        break;
                    case NetIncomingMessageType.WarningMessage:
                        Console.WriteLine("Warning message");
                        break;
                }

I am pretty sure the string in the NetPeerConfiguration constructor needed to be the same on the server and client.

you also looking for the ConnectionApproval message on the server. But by default this message type isn't enabled.

Advertisement

Awesome, that seemed to have been the issue (connection IDs needed to be the same). Thanks

This topic is closed to new replies.

Advertisement