Client identification, how?
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.
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.
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.
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.
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.
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.
Thanks for all the replies!
Yeah, guess so. I'm still a newbie with lidgren, so I'll just have to explore some more.. Thanks!
Quote:Original post by KylotanQuote: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!
> 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.
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).
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.
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement