0
\$\begingroup\$

I started with the example client and server from here and modified the client to send a response back to the server... which never arrives. What am I doing wrong? How do I do this?

Server:

using LiteNetLib;
using LiteNetLib.Utils;

Console.WriteLine("=== Server ===");
EventBasedNetListener listener = new EventBasedNetListener();
NetManager server = new NetManager(listener);
server.Start(9050);

listener.ConnectionRequestEvent += request => {
    if (server.ConnectedPeersCount < 10 /* max connections */)
        request.AcceptIfKey("SomeConnectionKey");
    else
        request.Reject();
};

listener.PeerConnectedEvent += peer => {
    Console.WriteLine("We got connection: {0}", peer.EndPoint); // Show peer ip
    NetDataWriter writer = new NetDataWriter();                 // Create writer class
    writer.Put("Hello client!");                                // Put some string
    peer.Send(writer, DeliveryMethod.ReliableOrdered);             // Send with reliability
};

listener.NetworkReceiveEvent += (fromPeer, dataReader, deliveryMethod) => {
    Console.WriteLine("We got: {0}", dataReader.GetString(100 /* max length of string */));
    dataReader.Recycle();
};

while (!Console.KeyAvailable) {
    server.PollEvents();
    Thread.Sleep(15);
}

server.Stop();

Client:

using LiteNetLib;
using LiteNetLib.Utils;

Console.WriteLine("=== Client ===");
EventBasedNetListener listener = new EventBasedNetListener();
NetManager client = new NetManager(listener);
client.Start();
var serverPeer = client.Connect("localhost", 9050, "SomeConnectionKey");

listener.NetworkReceiveEvent += (fromPeer, dataReader, deliveryMethod) => {
    Console.WriteLine("We got: {0}", dataReader.GetString(100 /* max length of string */));
    dataReader.Recycle();
};

NetDataWriter writer = new NetDataWriter();                 // Create writer class
writer.Put("Hello server!");                                // Put some string
serverPeer.Send(writer, DeliveryMethod.ReliableOrdered);    // Send with reliability

while (!Console.KeyAvailable) {
    client.PollEvents();
    Thread.Sleep(15);
}

client.Stop();
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You can use client.SendUnconnectedMessage(packet,peer.EndPoint) to send message to a fix endpoint - in your case the endpoint is the server.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. \$\endgroup\$
    – liggiorgio
    Mar 23, 2023 at 8:10
  • \$\begingroup\$ Thanks I totally gave up on this side project (and in writing a multiplayer game in general) but I'll give it a try. \$\endgroup\$ Mar 23, 2023 at 14:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .