SERVER:
Code:
namespace TankzLidgrenServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (txtInput.Text != "")
{
txtServerOutput.Text += "\n " + txtInput.Text;
txtInput.Text = "";
}
}
private void btnCloseServer_Click(object sender, EventArgs e)
{
}
private void btnStartServer_Click(object sender, EventArgs e)
{
NetPeerConfiguration config = new NetPeerConfiguration("Tankz");
config.Port = 14242;
NetServer server = new NetServer(config);
server.Start();
txtServerOutput.Text += "\nServer Started.";
config.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
NetIncomingMessage msg;
while ((msg = server.ReadMessage()) != null)
{
switch (msg.MessageType)
{
case NetIncomingMessageType.VerboseDebugMessage:
case NetIncomingMessageType.DebugMessage:
case NetIncomingMessageType.WarningMessage:
case NetIncomingMessageType.ErrorMessage:
txtServerOutput.Text += "\n" + msg.ReadString();
break;
case NetIncomingMessageType.DiscoveryRequest:
NetOutgoingMessage response = server.CreateMessage();
response.Write("Tankz Server 0.0.1");
txtServerOutput.Text += "\n" + "Client Connected from " + msg.SenderEndpoint;
server.SendDiscoveryResponse(response, msg.SenderEndpoint);
break;
default:
txtServerOutput.Text += "\n" + "Unhandled type: " + msg.MessageType;
break;
}
server.Recycle(msg);
}
}
}
}
Now the server seems to work. When I first ran it, Windows informed me that it was trying to access the internet. That's good.
The client on the other hand..
CLIENT:
Code:
namespace LidgrenClientTest
{
public partial class frmClientTest : Form
{
public frmClientTest()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnConnect_Click(object sender, EventArgs e)
{
NetPeerConfiguration config = new NetPeerConfiguration("Tankz");
config.EnableMessageType(NetIncomingMessageType.DiscoveryResponse);
NetClient client = new NetClient(config);
client.Start();
client.DiscoverKnownPeer("localhost", 14242); //These two lines.. I've tried DicoverLocalPeers, I've tried using the loopback IP as the IP,
client.Connect("localhost", 14242 ); //...the server says its binding a socket to O.O.O.O, so ive even tried that as the IP.. no luck.
//the server has no code to handle incoming messages just yet, but it should at very least indicate
//that a client has made an attempt to discover.. but it doesnt.
NetOutgoingMessage sendMsg = client.CreateMessage();
sendMsg.Write("Hello");
sendMsg.Write(42);
NetIncomingMessage inc;
while ((inc = client.ReadMessage()) != null)
{
switch (inc.MessageType)
{
case NetIncomingMessageType.DiscoveryResponse:
txtOutput.Text += "Found server at " + inc.SenderEndpoint + " name: " + inc.ReadString();
break;
}
}
}
}
}
So for now, as far as I can tell, the Client is not discovering the server... or at least the server isn't reacting to the discovery request the way it should.
This is the first time I've posted actual code on this site, looking for help. It feels very dirty. Ugh. (then again, with Lidgren im using someone elses code under the hood of my game, which also feels dirty.. what the heck, right?)
EDIT: Since posting this, ive noticed something. In the server code, I moved the:
config.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
line ABOVE where the config is used to initialize the server. Makes sense. But didn't help any. lol.
_________________
"None are more hopelessly enslaved than those who falsely believe they are free."
"It is no measure of health to be well adjusted to a profoundly sick society."
"Hope is the first step on the road to dissapointment." -Jonah Orion
http://tankzgame.blogspot.com