I'm having trouble getting my client to connect to my server in my lindgren network test. I've tried multiple ways of connecting but it always stops when I try to connect. Sorry if this is a broad question, but what am I doing wrong? Here is my code:
class Program
{
static NetPeerConfiguration serverconfig;
static NetPeerConfiguration clientconfig;
static NetServer server;
static NetClient client;
static void Main()
{
Console.Title = "LidgrenNetworkTest";
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("LidgrenNetworkTest console initialized.");
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("Color codes: ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("System ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("Input ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("Sent ");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("Received");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("\n");
Console.WriteLine("Configuring server...");
serverconfig = new NetPeerConfiguration("LidgrenNetworkTest");
serverconfig.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
serverconfig.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);
serverconfig.LocalAddress = NetUtility.Resolve("localhost");
serverconfig.Port = 8080;
Console.WriteLine("Server configured.");
Console.WriteLine("Configuring client...");
clientconfig = new NetPeerConfiguration("LidgrenNetworkTest");
clientconfig.EnableMessageType(NetIncomingMessageType.DiscoveryResponse);
Console.WriteLine("Client configured.");
Console.WriteLine("Initializing server...");
server = new NetServer(serverconfig);
server.Start();
Console.WriteLine("Server initialized.");
Console.WriteLine("Initializing client...");
client = new NetClient(clientconfig);
client.Start();
Console.WriteLine("Client initialized.");
Thread serverthread = new Thread(StartServer);
serverthread.Start();
StartClient();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Connecting client to server...");
//client.Connect(new IPEndPoint(NetUtility.Resolve("localhost"), 8080));
client.DiscoverLocalPeers(8080);
AcceptConsoleInput();
}
static void StartServer()
{
NetIncomingMessage message;
while ((message = server.ReadMessage()) != null)
{
switch (message.MessageType)
{
case NetIncomingMessageType.DiscoveryRequest:
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("(Server) Got request from client.");
NetOutgoingMessage response = server.CreateMessage();
response.Write((byte)1); // Do I need to do this?
server.SendDiscoveryResponse(response, message.SenderEndPoint);
break;
case NetIncomingMessageType.ConnectionApproval:
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("(Server) Connection accepted.");
message.SenderConnection.Approve();
break;
case NetIncomingMessageType.DebugMessage:
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("(Server) Debug: " + message.ReadString());
break;
default:
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("(Server) Unrecognized message type! (" + message.MessageType + ")");
break;
}
server.Recycle(message);
}
}
static void StartClient()
{
NetIncomingMessage message;
while ((message = server.ReadMessage()) != null)
{
switch (message.MessageType)
{
case NetIncomingMessageType.DiscoveryResponse:
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("(Client) Got response from server.");
client.Connect(message.SenderEndPoint);
Console.WriteLine("(Client) Attempting to connect to server...");
break;
case NetIncomingMessageType.DebugMessage:
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("(Client) Debug: " + message.ReadString());
break;
default:
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("(Client) Unrecognized message type! (" + message.MessageType + ")");
break;
}
client.Recycle(message);
}
}
static void AcceptConsoleInput()
{
string input = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(input))
{
// Send input from client to server.
}
AcceptConsoleInput();
}
}
Also, where it gets stuck:
I found your error:
In your void StartClient()
, you are setting message = server.ReadMessage()
in your while loop.
This should actually be message = client.ReadMessage()
.
Sorry for the late response.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With