Reputation: 141
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:
Upvotes: 5
Views: 4060
Reputation: 352
solution
using System;
using System.Threading;
using System.Collections.Generic;
using Lidgren.Network;
using System.Net;
namespace ChatClient
{
class Program
{
static NetPeerConfiguration serverconfig;
static NetPeerConfiguration clientconfig;
static NetServer server;
static NetClient client;
static void Main()
{
NetPeerConfiguration serverconfig = new NetPeerConfiguration("chat");
serverconfig.Port = 8081;
serverconfig.MaximumConnections = 100;
server = new NetServer(serverconfig);
Thread serverthread = new Thread(StartServer);
serverthread.Start();
NetPeerConfiguration clientconfig = new NetPeerConfiguration("chat");
clientconfig.AutoFlushSendQueue = false;
client = new NetClient(clientconfig);
Thread clientthread = new Thread(StartClient);
clientthread.Start();
AcceptConsoleInput();
}
static void StartServer()
{
server.Start();
NetIncomingMessage message;
while (true)
{
message = server.WaitMessage(500);
if (message != null)
{
switch (message.MessageType)
{
case NetIncomingMessageType.DiscoveryRequest:
NetOutgoingMessage response = server.CreateMessage();
response.Write((byte)1); // Do I need to do this?
server.SendDiscoveryResponse(response, message.SenderEndPoint);
break;
case NetIncomingMessageType.DebugMessage:
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("(Server) Debug: " + message.ReadString());
break;
case NetIncomingMessageType.StatusChanged:
NetConnectionStatus status = (NetConnectionStatus)message.ReadByte();
string reason = message.ReadString();
Console.WriteLine(NetUtility.ToHexString(message.SenderConnection.RemoteUniqueIdentifier) + " " + status + ": " + reason);
if (status == NetConnectionStatus.Connected)
Console.WriteLine("Remote hail: " + message.SenderConnection.RemoteHailMessage.ReadString());
break;
case NetIncomingMessageType.Data:
// incoming chat message from a client
string chat = message.ReadString();
// broadcast this to all connections, except sender
List<NetConnection> all = server.Connections; // get copy
//all.Remove(message.SenderConnection);
Console.WriteLine(all + "hello ");
NetOutgoingMessage om = server.CreateMessage();
server.SendMessage(om, all, NetDeliveryMethod.ReliableOrdered, 0);
break;
default:
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("(Server) Unrecognized message type! (" + message.MessageType + ")");
break;
}
}
server.Recycle(message);
}
Thread.Sleep(1);
}
static void StartClient()
{
client.Start();
client.DiscoverLocalPeers(8081);
NetOutgoingMessage hail = client.CreateMessage("This is the hail message");
NetIncomingMessage message;
while (true)
{
message = client.WaitMessage(500);
if (message != null) {
switch (message.MessageType)
{
case NetIncomingMessageType.DiscoveryResponse:
Console.WriteLine("(Client) Got response from server.");
client.Connect(message.SenderEndPoint, hail);
Console.WriteLine("(Client) Attempting to connect to server...");
break;
case NetIncomingMessageType.DebugMessage:
Console.WriteLine(message.ReadString());
break;
case NetIncomingMessageType.ErrorMessage:
Console.WriteLine(message.ReadString());
break;
case NetIncomingMessageType.WarningMessage:
Console.WriteLine(message.ReadString());
break;
case NetIncomingMessageType.VerboseDebugMessage:
Console.WriteLine(message.ReadString());
break;
case NetIncomingMessageType.Data:
string chat = message.ReadString();
break;
case NetIncomingMessageType.StatusChanged:
NetConnectionStatus status = (NetConnectionStatus)message.ReadByte();
Console.WriteLine(status.ToString());
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))
{
NetOutgoingMessage om = client.CreateMessage(input);
client.SendMessage(om, NetDeliveryMethod.ReliableSequenced);
client.FlushSendQueue();
}
AcceptConsoleInput();
}
}
}
I changed quite a bit so I could test for each part. I found the problem which is that almost immediately the message becomes null. I changed it to check continuously and keep checking even if it's null.
Upvotes: 0
Reputation: 21
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.
Upvotes: 2