UDP Networking Question

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
stormforce02
Prole
Posts: 4
Joined: Tue Aug 07, 2012 1:49 pm

UDP Networking Question

Post by stormforce02 »

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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: UDP Networking Question

Post by Robin »

How do you mean that?

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.
stormforce02
Prole
Posts: 4
Joined: Tue Aug 07, 2012 1:49 pm

Re: UDP Networking Question

Post by stormforce02 »

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.
User avatar
Charles Randall
Prole
Posts: 34
Joined: Wed May 09, 2012 2:52 pm

Re: UDP Networking Question

Post by Charles Randall »

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
User avatar
flashkot
Prole
Posts: 27
Joined: Sun Jul 29, 2012 4:56 pm
Location: ru

Re: UDP Networking Question

Post by flashkot »

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 :)
stormforce02
Prole
Posts: 4
Joined: Tue Aug 07, 2012 1:49 pm

Re: UDP Networking Question

Post by stormforce02 »

Don't quite get how setting up flags before data works. Can you give me some example code?
User avatar
flashkot
Prole
Posts: 27
Joined: Sun Jul 29, 2012 4:56 pm
Location: ru

Re: UDP Networking Question

Post by flashkot »

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:

Code: Select all

udp = socket.udp()

udp:send("Name:KrimsonKing")
udp:send("Position:37,16")
On server side, you receive and process this packets like this

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
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)
stormforce02
Prole
Posts: 4
Joined: Tue Aug 07, 2012 1:49 pm

Re: UDP Networking Question

Post by stormforce02 »

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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests