lidgren / lidgren-network-gen3

Lidgren Network Library
https://groups.google.com/forum/#!forum/lidgren-network-gen3
MIT License
1.19k stars 331 forks source link

Server can't receive connection #158

Closed leandrobattochio closed 4 months ago

leandrobattochio commented 4 months ago

So I have an Unity client trying to connect to a lidgren server.

This is my code:

NetPeerConfiguration config = new NetPeerConfiguration("server");
config.Port = 28080;
config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
config.EnableMessageType(NetIncomingMessageType.Data);
config.EnableMessageType(NetIncomingMessageType.StatusChanged);
config.UnreliableSizeBehaviour = NetUnreliableSizeBehaviour.NormalFragmentation;
config.ConnectionTimeout = 120f;
config.MaximumHandshakeAttempts = 3;
config.ResendHandshakeInterval = 1f;
config.ReceiveBufferSize = 131071;
config.SendBufferSize = 131071;
config.PingInterval = 4f;
config.MaximumTransmissionUnit = 1024;
config.AcceptIncomingConnections = true;
var server = new NetPeer(config);
server.Start();

// keep going until ESCAPE is pressed
Console.WriteLine("Press ESC to quit");
while (!Console.KeyAvailable || Console.ReadKey().Key != ConsoleKey.Escape)
{
    NetIncomingMessage msg;

    while ((msg = server.ReadMessage()) != null)
    {
        switch (msg.MessageType)
        {
            case NetIncomingMessageType.VerboseDebugMessage:
            case NetIncomingMessageType.DebugMessage:
            case NetIncomingMessageType.WarningMessage:
            case NetIncomingMessageType.ErrorMessage:
                Console.WriteLine(msg.ReadString());
                break;
            case NetIncomingMessageType.ConnectionApproval:
                Console.WriteLine("Connection Approval");
                break;
            default:
                Console.WriteLine("Unhandled type: " + msg.MessageType);
                Console.WriteLine("Data: " + JsonConvert.SerializeObject(msg.Data));
                break;
        }
        server.Recycle(msg);
    }
}

If I remove config.AcceptIncomingConnections = true; I get the Warning message: ?Received Connect, but we're not accepting incoming connections!. But if I set it to true, I don't receive any messages. The switch (msg.MessageType) is never executed.

What is going on?

leandrobattochio commented 4 months ago

It didn't work because both server & client need to have the same config name.