Sign in to follow this  

Client identification, how?

This topic is 3458 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, I've just finished my rpg game, and starting to play around with networking. I'm using C# with lidgren's network library, and I wonder how should the server know which client it talks to. This is how I think it's done: Client randomize a number (client id) at application start, sends it to server after connection. Server compares the Id with the client id list, if it's unique. Then tell the client it's okay to use. If not... wtf... now what? The server doesn't know to which client to tell it's not okay (because that client id is already used by another client), so the client can randomize a new id. Question for lidgren users: When using the lidgren library, and I'm sending a msg to a client. It sends to all clients that are connected to the server. Is there any way to send to a specefic client? I'm using broadcast now, and the myServer.sendMessage doesnt work for me, because I have no idea what NetChannel I should use.

Share this post


Link to post
Share on other sites
You store the client to the connected socket.

So kinda like this (doesnt HAVE to be this way).

Client Connects, Send EG packet with opcode 0x01 or maybe client version or whatever you wants.
Server knows its client connecting, waiting for login.
Client logs in, username (lets say "blabla")
Server store connection socket/loginname
Client selects a character, lets call it "lala"
Add lala as character to the socket/username.

Then later, when char is moving, talking ect, select the lala socket and send to it, or if to all, run true the list, sending to all in the connected list wich is ingame.

Well anyways kinda roughly said, but its usually how its done.

And since you cant have 2 connections on same socket, the uniqueness isnt really a issue.

Share this post


Link to post
Share on other sites
I'm confused what this ID is for. Why doesn't the server assign it? I don't use Lidgren's Network Library for my C# game, but the concept is pretty much the same. I have a list containing all users, then each map has a list of users on that map. Each user class contains a reference to the socket to communicate with the user. When I want to send to everyone, I simply use the list of all users. When I want to send to an individual user, since I already know the user to send to, I just use the socket in the user's class.

For the ID, do you mean a way for the server to tell the client which character is which? I just use a custom collection I made that works kinda like a List but doesn't ever modify the index. Because characters one map don't ever need to know the ID of another, all my IDs are map-based.

Can't really say much more without knowing what exactly you need help with better.

Share this post


Link to post
Share on other sites
Quote:
Original post by Iderik
I'm using C# with lidgren's network library, and I wonder how should the server know which client it talks to.


Typically, a connection is uniquely identified by the combination of host address, host port number, client address, and client port number. But that is often stored implicitly in your socket or connection object. You just then associate that with whichever game objects you like.

Share this post


Link to post
Share on other sites
Thanks for all the replies!

Quote:
Original post by Kylotan
Quote:
Original post by Iderik
I'm using C# with lidgren's network library, and I wonder how should the server know which client it talks to.


Typically, a connection is uniquely identified by the combination of host address, host port number, client address, and client port number. But that is often stored implicitly in your socket or connection object. You just then associate that with whichever game objects you like.


Yeah, guess so. I'm still a newbie with lidgren, so I'll just have to explore some more.. Thanks!

Share this post


Link to post
Share on other sites

> I wonder how should the server know which client it talks to.

The server holds an array of connections; loop thru myServer.Connections to see all connected clients. Whenever you receive a message (using ReadMessage) there's a property called "Sender" which holds a reference to the connection (client) which sent the message.

> Is there any way to send to a specefic client?

Yes. Use myConnection.SendMessage() or myServer.SendMessage() with the correct parameters.

> I have no idea what NetChannel I should use.

Check out the first paragraph on http://code.google.com/p/lidgren-library-network/ - if you're still unsure, NetChannel.Ordered1 is a safe bet - your messages will always arrive, and arrive in order.

As far as "client id" goes; you can just use the index in the myServer.Connections array. There's also a "Tag" property on every connection where you can attach your own data if you need it.

Share this post


Link to post
Share on other sites
Quote:
you can just use the index in the myServer.Connections


Won't that change as new connections come and old connections go?
It seems to me that using the Tag is much better (it saves a look-up, too, if the Tag is simply your client object).

Share this post


Link to post
Share on other sites
Quote:
Original post by hplus0603
Won't that change as new connections come and old connections go?


I wasn't precise enough in my last post; the connections array is of fixed size and can thus contain null entries. This means the index of a connection won't change.
Using the Tag is faster and easier tho, I agree.

If a unique identifying number is needed there's also the "Endpoint" property on the connection object which holds an IPEndPoint - use GetHashCode() to get an integer hashed from the IP and port.

Share this post


Link to post
Share on other sites
In case you're still wondering about which NetChannel to use, I wrote a FAQ point on this.

Does the data definately absolutely have to arrive?
----Yes
--------MUST all messages arrive in the exact order they were sent?
------------Yes - Use NetChannel.ReliableUnordered
------------No - Use NetChannel.Ordered
----No
--------What happens if a message is delayed, and arrives AFTER a newer message has already arrived?
------------The old data should be discarded - Use NetChannel.Sequenced
------------The old data should be delivered - Use NetChannel.Unreliable

Typical Unreliable message: Player waves hello
Typical Sequenced message: Player health and ammo count
Typical ReliableUnordered message: Player fires a missile
Typical Ordered message: Player sends a chat message

Any better typical examples are appreciated; for example, waving hello could argubly be an ReliableUnordered message.

Share this post


Link to post
Share on other sites
Sending your current position is a good example of an unreliable message - as long as most of them get through, it's ok. It doesn't matter if you drop a few as you interpolate between them.

Share this post


Link to post
Share on other sites
True; that is probably the most used case. It does require some explanation though that positions are timestamped or tick-numbered in some way and then interpolated between to acquire a display position; for a naive approach to positions Sequenced is more appropriate.

Share this post


Link to post
Share on other sites
I disagree with Kylotan -- Position updates are almost always Sequenced. You will almost always want to throw away a position update if it arrives out of order. Putting timestamps and tick counts on the position updates is just a way of implementing Sequenced on top of Unreliable.

Share this post


Link to post
Share on other sites
Well, I suppose that depends on how much interpolation you use. If you're interpolating more than tickcount x 2 back in time, out of order messages can still hold information you want to use when interpolating. If the packet is very late, and contains information older than your current interpolation window - it will be dropped automatically by that piece of code.

Share this post


Link to post
Share on other sites

This topic is 3458 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this