1
\$\begingroup\$

I'm using the Lidgren v3 network for a UDP client/server networking model. On the server end, I'm initializing a NetServer object with the NetIncomingMessage.ConnectionApproval message type enabled.

So the client is able to successfully connect and the first packet it sends is a login packet, containing a username and password supplied by the user. The server is receiving that and doing some black magic to authenticate, and everything works up to that point.

If the login fails, the server calling NetIncomingMessage.SenderConnection.Deny("Invalid Login Credentials"). I want to know how to properly receive this deny message on the client.

I'm getting the message, it shows up with a message type of NetIncomingMessage.StatusChanged. If I call ReadString on that message, I get a corrupted version of the string I passed to the Deny method on the server. The type of corruption varies, I've seen odd characters in there but in every case it's truncated and is way shorter than the string I entered.

Any ideas? The official documentation is sparse on this topic. I could use pointers from anyone who has successfully used the Lidgren library and uses the Accept or Deny methods.

Also, if I don't do any authentication and just Approve() the connection every time, stuff actually works just fine and I'm getting reliable two-way UDP traffic. (And lastly, Stack Exchange said I don't have enough reputation to use the "Lidgren" tag....???)

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

I stumbled upon the same question today, from your StatusChanged message type, read the first byte of the message and cast it to NetConnectionStatus. After that, check if it is a Disconnected status message. If it is, read the string from the message, which is the disconnect reason.

case NetIncomingMessageType.StatusChanged:
    switch ((NetConnectionStatus)im.ReadByte())
    {
        //When connected to the server
        case NetConnectionStatus.Connected:
             break;
        //When disconnected from the server
        case NetConnectionStatus.Disconnected:
        {
             string reason = im.ReadString();
             if (string.IsNullOrEmpty(reason))
                  Debug.WriteLine("Disconnected");
             else
                  Debug.WriteLine("Disconnected, Reason: {0}", reason);
        }
\$\endgroup\$
0
\$\begingroup\$

Sorry for the slow answer; but the first byte of the StatusChanged message is a byte which contains a NetConnectionStatus enum - after that there's the human readable reason string.

\$\endgroup\$
2
  • \$\begingroup\$ Unfortunately the "human readable reason string" is always mangled in my case. In the end, I changed it to simply accept the message and then respond with a custom data message that had what I needed. \$\endgroup\$
    – TJ Mott
    Jun 18, 2014 at 14:50
  • 1
    \$\begingroup\$ It's not mangled if you read the byte first. When message comes, there's byte for message type and if it's status changed message, then there's second byte which tell's you why it changed, then the string. It's very important to read those in correct order. \$\endgroup\$
    – Katu
    Jul 3, 2014 at 4:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .