Server / client connection for Lidgren

Hi,

Trying to get my head around the Lidgren network library, I have looked through several tutorials and samples and I can’t figure out why it seems my client and server that I have created below are either not connected or not able to send stuff to eachother. Just thought I’ll post it here and see if anyone can spot any obvious errors or if there is something else I am missing.

The client is in a MonoGame project and the server in a console application project.

CLIENT:

    public void StartClient()
    {
        var config = new NetPeerConfiguration("hej");
        config.AutoFlushSendQueue = false;
        client = new NetClient(config);
        client.Start();

        string ip = "localhost";
        int port = 14242;
        client.Connect(ip, port);
     }

    public void SendMessage()
    {
        NetOutgoingMessage message = client.CreateMessage("blabla");
        client.SendMessage(message, NetDeliveryMethod.ReliableOrdered);
        client.FlushSendQueue();
    }

SERVER:

public void StartServer()
    {
        var config = new NetPeerConfiguration("hej")
        { Port = 14242 };
        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...");
        }
        clients = new List<NetPeer>();
    }

    public void ReadMessages()
    {
        NetIncomingMessage message;
        while ((message = server.ReadMessage()) != null)
        {
            switch (message.MessageType)
            {
                case NetIncomingMessageType.Data:
                    Console.WriteLine("i got smth!");
                    var data = message.ReadString();
                    Console.WriteLine(data);
                    break;
                case NetIncomingMessageType.DebugMessage:
                    Console.WriteLine(message.ReadString());
                    break;
                case NetIncomingMessageType.StatusChanged:
                    Console.WriteLine(message.SenderConnection.Status);
                    if (message.SenderConnection.Status == NetConnectionStatus.Connected)
                    {
                        clients.Add(message.SenderConnection.Peer);
                        Console.WriteLine($"{message.SenderConnection.Peer.Configuration.LocalAddress} has connected.");
                    }
                    if (message.SenderConnection.Status == NetConnectionStatus.Disconnected)
                    {
                        clients.Remove(message.SenderConnection.Peer);
                        Console.WriteLine($"{message.SenderConnection.Peer.Configuration.LocalAddress} has disconnected.");
                    }
                    break;
                default:
                    Console.WriteLine($"Unhandled message type: {message.MessageType}");
                    break;
            }
            server.Recycle(message);
        }

I can also add that the samples I have tried out does work on my computer. For example the chat client on Lidgrens github which have a very similar set up as this.

Hi,

Your code works fine for me. I don’t know if you’re using ReadMessages and SendMessage in loops. If not, you should :slight_smile:
Here’s my full working example VS solution: LidGrenTest.zip

I hope it helps.

Yea the loop was it, I thought I had that handled by the initial loop. Thanks a lot!