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()
{
}
}