The problem is that between the time connection (a message on its own) and the first data message, the connection had not been fully setup on the server side. As a hack on my proof, I simply added a short delay (Thread.Sleep(500)). For a more effective fix, I plan on implementing a response message from the server prior to the client sending more.
Lidgren is not sending/receiving Data messages
Question
I have grabbed the latest edition of Lidgren, from https://code.google.com/p/lidgren-network-gen3/
I've reviewed many tutorials, but none seem to work. I presume I must be missing something in my code.
using Lidgren.Network;
using System;
namespace LGServer
{
class Program
{
static void Main(string[] args)
{
NetPeerConfiguration config = new NetPeerConfiguration("test");
config.Port = 5432;
config.LocalAddress = new System.Net.IPAddress(new byte[] { 127, 0, 0, 1 });
config.MaximumConnections = 1000;
NetServer server = new NetServer(config);
server.Start();
NetIncomingMessage msg = null;
while (true)
{
while ((msg = server.ReadMessage()) != null)
{
Console.WriteLine(msg.MessageType.ToString());
if (msg.MessageType == NetIncomingMessageType.Data)
{
Console.WriteLine(msg.ReadInt16());
Console.WriteLine(msg.ReadString());
}
}
}
}
}
}
//// My client code:
using Lidgren.Network;
using System;
namespace LGClient
{
class Program
{
static void Main(string[] args)
{
NetPeerConfiguration config = new NetPeerConfiguration("test");
NetClient client = new NetClient(config);
client.Start();
client.Connect("127.0.0.1", 5432);
NetOutgoingMessage msg = client.CreateMessage();
msg.Write((Int16)3346);
msg.Write("Test Message 1 whooahaa");
client.SendMessage(msg, NetDeliveryMethod.ReliableUnordered);
client.FlushSendQueue();
}
}
}
The server gets a status change every time I connect (?status changes from running to running?) Then I get a debug message with the time it takes between client/server
But I never get the data message. does this code work on someone elses machine? is there something obvious I'm missing?
Thanks.
Solution
OTHER TIPS
If your intention is to send a message as soon as you connect you should send your message when you receive a statuschange update from the server that notifies that you've connected as Connect does not block nor prevent subsequent code from executing if you've yet to establish a connection.