Multiple clients from localhost

Hi, new to the forum! So this question is not Defold-bound, for example in my case it works with Love2D as well, but it’s just a matter of trying to figure out how to make this work, because I am missing something.

So I am using luasocket (but I am open to use other libraries if this can’t be solved with luasocket) and using UDP for a server-clients scenario (so more than 2 clients and 1 server). To make things easy to understand, the bare minimum scenario for this problem is 1 server and 2 clients and here is what I need to do as a pipeline:

1)client1 sends a string (message) to server, no message displayed to client1 yet
2)server receives message from client1, sends confirmation message back to client1 and also sends same message to client2
3)client1 screen now has a message saying that his message was successfully delivered to the server
4)client2 screen shows a message from the server, saying that client1
has sent a message

My problem is step 4 doesn’t work for me. I feel like I tried all sorts of combinations with port, address, etc, just doesn’t work. My skeleton project for this scenario is made in Love2D for fast testing, and I could share it if need be. But how would I implement this scenario pipeline above all on localhost? Thanks!

If step 3 works then step 4 should be no different. I suspect if you are running both clients on the same machine and if they have the same port number (client side) then the server wont know the difference and only 1 client will connect/receive data.
The simplest way to make this work with two local machines is to make sure each client socket that is opened has its own port number when connecting to the server.

1 Like

But my problem is localhost is working on same machine, but as soon as I try over LAN, so client is going to be the ipv4 address and not localhost, that doesn’t work. And my router/firewall/pc is good because I tested other games over LAN and it’s working, so it’s something wrong with the code, but I don’t know what. So just to repeat the problem, when I do local address, port = “localhost”, 12345, that works, but when I try local address, port = “192.168.0.7”, 12345 for example, it doesn’t. I can also send the full code of this bare bones project, since it’s literally just for testing purposes if it would help. Thank you!
enetTest.zip (1.1 KB)

As Britzl pointed out on Discord, the example you show here is not pure UDP sockets. This is enet. Which is actually much more like TCP (but higher level transport layer).
http://enet.bespin.org/

If you want to do something similar then you need a similar library for Defold. Being Luajit for most platforms you could use:

There are many others too. I havent tried this one so its not guaranteed to work of course.
The problems with using UDP in networks is, as I mentioned in Discord, most routers and systems block or impede their use because of their broadcast nature (generates lots of traffic for whole network rather than point to point). What enet does is try to manage this through various network management techniques - and its used in many places exactly for this reason.

Yep, I suggest that you either look into the ffi-enet solution by @dlannan or a similar high level networking solution to ENet. Some that come to mind are:

All three of them have Defold clients. NoobHub is by far the most basic (and simplest to set up) while both Nakama and Colyseus are very versatile and have a lot of features.

1 Like

I also found this from the luajit.org ffi-extensions page. So it should be more reliable than the other one I posted :slight_smile:
https://github.com/ColonelThirtyTwo/luajit-ffi-enet

If you want something working quick and easy - then use one of @Britzl’s suggestions above. They will be much easier to use for new network developers.

1 Like