UDP networking for multiplayer game
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: UDP networking for multiplayer game
Send a UDP message to 255.255.255.255, i. e. send a broadcast message. All devices listening on the same network on that destination port will receive it and can respond to it.
Edit: it's a little bit more complicated for IPv6, but not by much. Please support IPv6
Edit: it's a little bit more complicated for IPv6, but not by much. Please support IPv6
Re: UDP networking for multiplayer game
You can send a message to all hosts on the LAN by using the target address of 255.255.255.255. Something like this:
Then simply broadcast to everyone with:
EDIT: grump beat me to it and is right about IPv6
Code: Select all
local socket = require "socket"
local sock = socket.udp()
sock:setsockname("*", 55555)
assert(sock:setoption("broadcast", true))
Code: Select all
assert(sock:sendto(msg, "255.255.255.255", 55555))
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
-
- Prole
- Posts: 37
- Joined: Mon Feb 11, 2019 4:17 pm
Re: Broadcast
Thanks for the quick replies! I am going to try it out this evening.
Some questions in advance:
What exactly is the meaning of the "*" in?
I've read the UDP networking tutorial when I started and for some reason I could not get this running for my game. Instead, I had to give a specific address (server-side). (So for the server and the client the same "server address" was hardcoded)
Server:
Client:
My guess would be that after the server send such a message to all clients (and empty nodes) in the LAN, I can use getsockname() at the server to know the server IP address, right? And similar to that the client responds to the broadcast message with a sendto -> again use getsockname() for the client... at least that's what my understanding looks like atm.
I'm looking forward to test it asap thanks for helping!
Some questions in advance:
What exactly is the meaning of the "*" in
Code: Select all
sock:setsockname("*", 55555)
I've read the UDP networking tutorial when I started and for some reason I could not get this running for my game. Instead, I had to give a specific address (server-side). (So for the server and the client the same "server address" was hardcoded)
Server:
Code: Select all
udp:setsockname(serverAdress, serverPort)
Code: Select all
udp:setpeername(serverAdress, serverPort)
I'm looking forward to test it asap thanks for helping!
Re: Broadcast
The "*" lets the socket know to accept messages from all interfaces. For example, if you used "127.0.0.1" instead of "*", it would only accept messages from localhost.mxmlnbndsmnn wrote: ↑Wed Apr 03, 2019 6:09 am Some questions in advance:
What exactly is the meaning of the "*" in?Code: Select all
sock:setsockname("*", 55555)
You need to call:mxmlnbndsmnn wrote: ↑Wed Apr 03, 2019 6:09 am I've read the UDP networking tutorial when I started and for some reason I could not get this running for my game. Instead, I had to give a specific address (server-side). (So for the server and the client the same "server address" was hardcoded)
Server:Client:Code: Select all
udp:setsockname(serverAdress, serverPort)
My guess would be that after the server send such a message to all clients (and empty nodes) in the LAN, I can use getsockname() at the server to know the server IP address, right? And similar to that the client responds to the broadcast message with a sendto -> again use getsockname() for the client... at least that's what my understanding looks like atm.Code: Select all
udp:setpeername(serverAdress, serverPort)
I'm looking forward to test it asap thanks for helping!
Code: Select all
sock:setsockname("*", port)
Code: Select all
sock:sendto(msg, "255.255.255.255", port)
Code: Select all
msg, address, port = sock:receivefrom()
Code: Select all
sock:sendto(msg, address, port)
Code: Select all
sock:receivefrom()
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
-
- Prole
- Posts: 37
- Joined: Mon Feb 11, 2019 4:17 pm
Broadcast Nr 2
Alright, I am trying to implement that approach but ... on the server I cannot do this:
with this
It says (something like) that the specified host is unknown.
The client on the other hand says that the used address is not compatible with the protocol when I do receivefrom. :c
Edit: When I posted this, your last post was not yet visible to me, keharisso.
Edit 2: Yep, your steps are mostly what I did ... using the udp:setsockname("*", serverPort) on both client and server (I only did it on the server side) the client now "works" in terms of not producing the compatibility error any more. But the first error still occurs: the server does not send a broadcast message. Not knowing where it results from I tried it on 1 PC, 2 PCs with LAN and "normal"/dynamic and also static addresses. I even disabled IPv6 for now in my windows LAN settings... no changes.
Code: Select all
assert(udp:sendto("promo", "255.255.255.255", serverPort))
Code: Select all
udp:setsockname("*", serverPort)
The client on the other hand says that the used address is not compatible with the protocol when I do receivefrom. :c
Edit: When I posted this, your last post was not yet visible to me, keharisso.
Edit 2: Yep, your steps are mostly what I did ... using the udp:setsockname("*", serverPort) on both client and server (I only did it on the server side) the client now "works" in terms of not producing the compatibility error any more. But the first error still occurs: the server does not send a broadcast message. Not knowing where it results from I tried it on 1 PC, 2 PCs with LAN and "normal"/dynamic and also static addresses. I even disabled IPv6 for now in my windows LAN settings... no changes.
- Attachments
-
- network.lua
- (3.85 KiB) Downloaded 124 times
Re: UDP networking for multiplayer game
A few things:
1. You can't use the same port for client and server on the same host. You need a separate clientPort for the client (and broadcast to this port from the server).
2. In the client you need to use:
3. Don't use udp:setpeername in the client.
1. You can't use the same port for client and server on the same host. You need a separate clientPort for the client (and broadcast to this port from the server).
2. In the client you need to use:
Code: Select all
udp:setsockname("*", clientPort)
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
-
- Prole
- Posts: 37
- Joined: Mon Feb 11, 2019 4:17 pm
Re: UDP networking for multiplayer game
I used setpeername because the tutorial included it, but okay.
Still the broadcast wont work :c Setting the broadcast option to true returns 1 as it should. But the host is still "unknown".
Still the broadcast wont work :c Setting the broadcast option to true returns 1 as it should. But the host is still "unknown".
Re: UDP networking for multiplayer game
Sorry, where is host unknown?
I attached the code that seems to be working for me.
I attached the code that seems to be working for me.
- Attachments
-
- udp-networking-example.zip
- (3.26 KiB) Downloaded 108 times
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
-
- Prole
- Posts: 37
- Joined: Mon Feb 11, 2019 4:17 pm
Re: UDP networking for multiplayer game
The Does not work. It produces an error that says that the specified host (I guess the broadcast address) is unknown.
Same error with your files. So it seems to be device/OS dependend? Using love 11.1 on Windows 10. I'm only in a common home Wifi (deactivating it made no changes) and right now I'm only testing on one local machine. I'm confused-
Code: Select all
assert(udp:sendto("promo", "255.255.255.255", clientPort))
Same error with your files. So it seems to be device/OS dependend? Using love 11.1 on Windows 10. I'm only in a common home Wifi (deactivating it made no changes) and right now I'm only testing on one local machine. I'm confused-
Re: UDP networking for multiplayer game
You're right, it's an OS issue. Works on Debian Stretch, doesn't work on Windows 10. I tried playing around with the code and couldn't come up with any solution. The question is where the bug is. I'll look into it a bit more.mxmlnbndsmnn wrote: ↑Wed Apr 03, 2019 5:02 pm TheDoes not work. It produces an error that says that the specified host (I guess the broadcast address) is unknown.Code: Select all
assert(udp:sendto("promo", "255.255.255.255", clientPort))
Same error with your files. So it seems to be device/OS dependend? Using love 11.1 on Windows 10. I'm only in a common home Wifi (deactivating it made no changes) and right now I'm only testing on one local machine. I'm confused-
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot], hmmmmxd and 3 guests