Jump to content
  • Advertisement
Sign in to follow this  
Iderik

[SOLVED] [C# with lidgren] Send to a particular client, not to all clients?

This topic is 3645 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone, I'm having some trouble with the network library Lidgren. The best would be that someone could make a example how it could work, because I'm been having trouble with this in a very long time. Please The problem is about sending messages to the client. When I'm using the broadcast it will send the message to all clients. But I have no idea how to send to just one particular client. Here is a quote from lidgren homepage:
NetMessage outMsg = new NetMessage();
outMsg.Write("Hello!");
myServer.SendMessage(outMsg, conn, NetChannel.Unreliable); // <- to send to connection 'conn'


When using broadcast: sServer.Broadcast(outMsg, NetChannel.Unreliable); It will send to all clients like I said. When using sendmessage: sServer.SendMessage(outMsg, sConn, NetChannel.Unreliable); The sConn will be null, because I haven't defined the NetConnection sConn. And I have no idea how to, how do I know which client is which? Here is from the lidgren's source file:
/// <summary>
		/// Sends a message to a certain connection using the channel specified
		/// </summary>
		public bool SendMessage(NetMessage msg, NetConnection connection, NetChannel channel)
		{
			if (connection == null)
				throw new ArgumentNullException("connection");

			if (connection.Status == NetConnectionStatus.Disconnected)
			{
				Log.Warning("SendMessage failed - Connection is Disconnected!");
				return false;
			}

			connection.SendMessage(msg, channel);
			return true;
		}




I'm new to lidgren and networking at all, so please don't forget details. Thanks! EDIT: Thank you so much for helping me out! [Edited by - Iderik on July 3, 2008 1:04:36 PM]

Share this post


Link to post
Share on other sites
Advertisement
I believe you get the connection when the you get the client connect notification. Check the documentation to make sure.

To post source, code, use [ SOURCE ] and [ / SOURCE ] tags (but without the spaces).

Share this post


Link to post
Share on other sites
Yes, like hplus wrote; hook the StatusChanged event and examine the arguments passed to it - it will contain the new status (for example; "Connected") and a connection object, which you can use to send messages to that client.
You can also examine the Connections array on the server object; it's a fixed size array filled with null entries in the beginning, but when a client connects, one of the entries will point to the client connection object.
A third way is to examine a received message; it has a Sender property which holdd a reference to the connection object of the client that sent the message.

Which particular client do you want to send a message to?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!