gpt4 book ai didi

c# - Lidgren 网络,无法连接

转载 作者:太空狗 更新时间:2023-10-29 23:40:02 26 4
gpt4 key购买 nike

在我的 lindgren 网络测试中,我无法让我的客户端连接到我的服务器。我已经尝试了多种连接方式,但是当我尝试连接时它总是停止。对不起,如果这是一个广泛的问题,但我做错了什么?这是我的代码:

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();
}
}

此外,卡住的地方: The console

最佳答案

我发现了你的错误:

在您的 void StartClient() 中,您在 while 循环中设置了 message = server.ReadMessage()

这实际上应该是 message = client.ReadMessage()

抱歉回复晚了。

关于c# - Lidgren 网络,无法连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14249567/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com