UDP Networking Question
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 4
- Joined: Tue Aug 07, 2012 1:49 pm
UDP Networking Question
When someone sends you packets of data to the server, how can you differentiate whether it's the first time or not?
Last edited by stormforce02 on Tue Aug 07, 2012 2:09 pm, edited 1 time in total.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: UDP Networking Question
How do you mean that?
We might be able to help you better if you say what it is you want to achieve.
We might be able to help you better if you say what it is you want to achieve.
Help us help you: attach a .love.
-
- Prole
- Posts: 4
- Joined: Tue Aug 07, 2012 1:49 pm
Re: UDP Networking Question
I want to create a new player when someone connects. Also, to extend my question, how do I differentiate between the different data being sent to the server? ie player position, name, etc.
- Charles Randall
- Prole
- Posts: 34
- Joined: Wed May 09, 2012 2:52 pm
Re: UDP Networking Question
Generally it's up to you to decide what the data is, and how to interpret it. A common method to do this is to pack data descriptors in with the data. So you might have a set of flags that represent what the data is, and send a flag before the data. So your data might look like this:
Flag: Player Name
"Bob"
Flag: Player Position
x, y
Flag: Player Name
"Bob"
Flag: Player Position
x, y
Re: UDP Networking Question
When you receive packet, you also get originating ip and port. You can create table players and add each sender to it using ip as a key. Then just check if record exists in table.
For storing data in packet: check out my post viewtopic.php?f=5&t=10363 may be it will be helpful
For storing data in packet: check out my post viewtopic.php?f=5&t=10363 may be it will be helpful
-
- Prole
- Posts: 4
- Joined: Tue Aug 07, 2012 1:49 pm
Re: UDP Networking Question
Don't quite get how setting up flags before data works. Can you give me some example code?
Re: UDP Networking Question
I think his idea was to just use words to distinguish one data type from another.
In Lua socket you are sending and receiving strings. So, you should send your data like this:
On server side, you receive and process this packets like this
Quite simple, if you not going to send huge amount of data in one packet. (theoretically UDP packet can be almost 64k bytes long, but network hardware may limit this to something about 512 bytes or so. In real world big packets can be simply dropped)
You also should note, that it is not guaranteed what order of received packets will match their sending order (In this example you can receive Position packet and only then packet with Name)
In Lua socket you are sending and receiving strings. So, you should send your data like this:
Code: Select all
udp = socket.udp()
udp:send("Name:KrimsonKing")
udp:send("Position:37,16")
Code: Select all
data, msg_or_ip, port_or_nil = udp:receivefrom()
if data:sub(1, 4) == "Name" then
Player.Name = data:sub(6)
elseif data:sub(1, 8) == "Position" then
Player.x, Player.y = data:match("^Position%:(%d+)%,(%d+)$")
-- This values are still strings. So convert them to numbers
Player.x = tonumber(Player.x)
Player.y = tonumber(Player.y)
end
You also should note, that it is not guaranteed what order of received packets will match their sending order (In this example you can receive Position packet and only then packet with Name)
-
- Prole
- Posts: 4
- Joined: Tue Aug 07, 2012 1:49 pm
Re: UDP Networking Question
Alright, sounds simple enough. One last thing. I've trying looking in many different places, and I can't seem to find and proper documentation on what things like (%d+)% is an %a and %b, etc are. If you can give me a link, or even briefly explain it to me, it'd be very helpful.
Who is online
Users browsing this forum: whateverest and 5 guests