Hi, I have a problem with lidgren not creating a connection between a server and client.
the port is set to 7253.
The netPeerConfiguration is set to "GameEngine"
And the server does allow ConnectionApproval
Full source code can be found here: CODE
Server Code:
public class NetworkConnection
{
public NetClient _client;
public string debug = "";
public bool isEstablished = false;
public bool needToEstablish = false;
public DateTime ConnectionStarted;
public void Start()
{
debug = "Connecting...";
var loginInformation = new NetworkLoginInformation() { Name = "USER" };
_client = new NetClient(new NetPeerConfiguration("GameEngine") { Port = 7253 });
_client.Start();
var outmsg = _client.CreateMessage();
outmsg.Write((byte)PacketType.Login);
outmsg.WriteAllProperties(loginInformation);
_client.Connect("localhost", 7253, outmsg);
ConnectionStarted = DateTime.Now;
needToEstablish = true;
}
// Called every tick that needToEstablish is true.
public bool EstablishInfo(NetClient _client)
{
NetIncomingMessage inc;
if ((inc = _client.ReadMessage()) != null)
{
debug = "Message received!";
Debug.WriteLine(inc);
Debug.WriteLine(inc.MessageType);
switch (inc.MessageType)
{
case NetIncomingMessageType.Data:
debug = "Received the right type of message!";
var data = inc.ReadByte();
if (data == (byte)PacketType.Login)
{
var accepted = inc.ReadBoolean();
if (accepted)
{
debug = "Connection accepted!";
return true;
}
else
{
return false;
}
}
else
{
return false;
}
case NetIncomingMessageType.DebugMessage:
Debug.WriteLine("Debug: " + inc.ReadString());
break;
default:
Debug.WriteLine("Unrecognized message type! (" + inc.MessageType + ")");
break;
}
}
return false;
}
}
Client Code:
public void Init(Engine engine)
{
// only run once. on the first time the button is clicked
config = new NetPeerConfiguration("GameEngine") { Port = 7253 };
config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
server = new NetServer(config);
// server.Start() is called in a different function.
running = true;
engine._networkConnection.debug = "Starting Server...";
}
// this function is called every tick while the server is active
public override void Update(Engine engine, float dt)
{
NetIncomingMessage inc;
if ((inc = server.ReadMessage()) != null)
{
switch (inc.MessageType)
{
case NetIncomingMessageType.Error:
break;
case NetIncomingMessageType.StatusChanged:
break;
case NetIncomingMessageType.UnconnectedData:
break;
case NetIncomingMessageType.ConnectionApproval:
engine._networkConnection.debug = ("New Connection...");
var data = inc.ReadByte();
if (data == (byte)PacketType.Login)
{
engine._networkConnection.debug = ("... Connection Accepted");
var loginInformation = new NetworkLoginInformation();
inc.ReadAllProperties(loginInformation);
inc.SenderConnection.Approve();
var outmsg = server.CreateMessage();
outmsg.Write((byte)PacketType.Login);
outmsg.Write(true);
server.SendMessage(outmsg, inc.SenderConnection, NetDeliveryMethod.ReliableOrdered);
}
else
{
inc.SenderConnection.Deny("Didn't send correct information.");
engine._networkConnection.debug = "Didn't send correct information.";
}
break;
case NetIncomingMessageType.Data:
break;
case NetIncomingMessageType.Receipt:
break;
case NetIncomingMessageType.DiscoveryRequest:
break;
case NetIncomingMessageType.DiscoveryResponse:
break;
case NetIncomingMessageType.VerboseDebugMessage:
break;
case NetIncomingMessageType.DebugMessage:
break;
case NetIncomingMessageType.WarningMessage:
break;
case NetIncomingMessageType.ErrorMessage:
break;
case NetIncomingMessageType.NatIntroductionSuccess:
break;
case NetIncomingMessageType.ConnectionLatencyUpdated:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}