Lidgren Networking not working

I have been trying to get Lidgren to work for the past couple of days in my Unity3D game and for some reason the client WILL NOT connect to the server. Ever. It is driving me insane. I have googled and googled and googled for an answer, and checked/debugged my code several times. I haven’t found anything wrong with it at all. Just so you know, I have typed in both ‘localhost’ and ‘127.0.0.1’ for the IP Address and neither work. I have 2 game instances open. One is the server that I start first, the other is the client. If someone could help me I would appreciate it.

using UnityEngine;

using System;
using System.Collections;
using System.IO;
using System.Net;
using Lidgren;
using Lidgren.Network;

public enum GUIState
{
None,
Login,
IPAddressEntrance,
}

public class GUIScript : MonoBehaviour
{
public const String AppName = “LidgrenTest”;
public const int DefaultPort = 15151;

public NetServer TheNetServer;
public NetClient TheNetClient;
String UsernameText = String.Empty;
String PasswordText = String.Empty;
String IPAddress = String.Empty;
GUIState TheGUIState =  GUIState.Login;
String TextToRender = "";

void OnGUI()
{
	if(TheGUIState == GUIState.Login)
	{
		UsernameText = GUI.TextField(RectExtensions.CreateCenteredRect(ScreenExtensions.CenterOfScreen - new Vector2(0.0f, 70.0f), new Vector2(300, 50)), UsernameText);
		PasswordText = GUI.PasswordField(RectExtensions.CreateCenteredRect(ScreenExtensions.CenterOfScreen, new Vector2(300, 50)), PasswordText, '*');
		
		GUI.Label(RectExtensions.CreateCenteredRect(ScreenExtensions.CenterOfScreen + new Vector2(0.0f, 150.0f), new Vector2(300, 50)), TextToRender);
			
		if(GUI.Button(RectExtensions.CreateCenteredRect(ScreenExtensions.CenterOfScreen + new Vector2(0.0f, 70.0f), new Vector2(300, 50)), "Login"))
		{
// Code isn't related to Lidgren here, so I removed it.
		}
	}
	else if(TheGUIState == GUIState.IPAddressEntrance)
	{
		IPAddress = GUI.TextField(RectExtensions.CreateCenteredRect(ScreenExtensions.CenterOfScreen + new Vector2(0.0f, -70.0f), new Vector2(300, 50)), IPAddress);
		
		if(GUI.Button(RectExtensions.CreateCenteredRect(ScreenExtensions.CenterOfScreen + new Vector2(0.0f, 0.0f), new Vector2(300, 50)), "Connect"))
		{			
			NetPeerConfiguration ClientConfiguration = new NetPeerConfiguration(AppName);
			
			TheNetClient = new NetClient(ClientConfiguration);
			
			TheNetClient.Start();
			
			System.Threading.Thread.Sleep(500);
			
			TheNetClient.Connect(IPAddress, DefaultPort);
			
			System.Threading.Thread.Sleep(500);
			
			if(TheNetClient.ConnectionStatus != NetConnectionStatus.Connected)
			{
				TextToRender = TheNetClient.ConnectionStatus.ToString();
			}
		}
		
		if(GUI.Button(RectExtensions.CreateCenteredRect(ScreenExtensions.CenterOfScreen + new Vector2(0.0f, 70.0f), new Vector2(300, 50)), "Create Server"))
		{
			NetPeerConfiguration ServerConfiguration = new NetPeerConfiguration(AppName);
			ServerConfiguration.Port = DefaultPort;
			ServerConfiguration.MaximumConnections = 32;
			
			TheNetServer = new NetServer(ServerConfiguration);
			TheNetServer.Start();
		}
		
		GUI.Label(RectExtensions.CreateCenteredRect(ScreenExtensions.CenterOfScreen + new Vector2(0.0f, 150.0f), new Vector2(300, 50)), TextToRender);
	}
}

void Update()
{
}

}

2 Answers

2

At a glance, I believe the server has to be configured to accept a connection request, and approve new connections. So:

ServerConfiguration.EnableMessageType(NetIncomingMessageType.ConnectionApproval);

And then in update (or wherever), something like this to approve the message:

NetIncomingMessage inc;
while((inc = TheNetServer.ReadMessage()) != null)
{
	if(inc.MessageType == NetIncomingMessageType.ConnectionApproval)
		inc.SenderConnection.Approve();
}

On the other hand, I’m not an expert on the matter, so I don’t really know! The client might need to send a message on the NetClient.Connect. At least, that’s how I have mine set up.

I’ll add to this so it’s somewhere on the net.

I debugged this problem by adding

config.EnableMessageType(NetIncomingMessageType.StatusChanged);
        
case NetIncomingMessageType.StatusChanged:
    NetConnectionStatus connectionStatus = (NetConnectionStatus)incMsg.ReadByte();
    string message = incMsg.ReadString();
    break;

into the client code.

This gave me a problem with differences in the NetPeerConfiguration appIdentifier fields. They have to be the same strings in both the server and the client.

NetPeerConfiguration clientConfig = new NetPeerConfiguration("sameString");
netClient = new NetClient(clientConfig);


NetPeerConfiguration serverConfig = new NetPeerConfiguration("sameString");
netServer = new NetServer(serverConfig);