Using DefNet P2P Discovery

I wanted to try and learn how to include multiplayer in games and so I’ve started to make a very basic turn based mobile game just to test how it would work, using the DefNet library.

I am using the p2p_discovery module and I have set up a menu in which one user will “host” the game, broadcasting the connection onto a port and then one user will “join” the game, listening for the connection on that same port. Now I am unsure on two things:

  1. Is there a way for both users to see whether a connection has been made?

  2. How do you send data across this connection, like certain values or variables?

The P2P discovery module is used only to detect a “host” on the network. It is not used to exchange data. The host must also accept incoming connections somehow. One way to do this is for the host to accept TCP connections on some port, for instance using the example TCP Server or HTTP server module in DefNet. The client would use the IP from the P2P discovery and connect on another port where the server running on the host is waiting for connections.

1 Like

Ah ok thank you very much

I’ve tried using the examples of the TCP server and client but they don’t actually end up connecting and I don’t know why. Is there something wrong with my code?

local p2p_discovery = require "defnet.p2p_discovery"
local tcp_client = require "defnet.tcp_client"
local tcp_server = require "defnet.tcp_server"
local PORT = 50000
local script_start = false
local server_start = false
local local_host = nil

local function on_data(data, ip, port, client)
	print("Received", data, "from", ip)
	return "My response"
end

local function on_client_connected(ip, port, client)
	print("Client", ip, "connected")
end

local function on_client_disconnected(ip, port, client)
	print("Client", ip, "disconnected")
end

local function join_p2p(self, handle, time_elapsed)
	print(local_host)
	if local_host ~= nil then
		self.client = tcp_client.create(local_host, 6000, function(data)
			print("TCP client received data " .. data)
		end,
		function()
			print("On disconnected")
			self.client = nil
		end)
	else
		print("Host not found")
	end
end

function on_message(self, message_id, message, sender)
	if message_id == hash("host") then
		script_start = true
		self.p2p = p2p_discovery.create(PORT)
		self.p2p.broadcast("findme")
		server_start = true
		self.server = tcp_server.create(6000, on_data, on_client_connected, on_client_disconnected)
		self.server.start()
	elseif message_id == hash("join") then
		script_start = true
		self.p2p = p2p_discovery.create(PORT)
		self.p2p.listen("findme", function(ip, port)
			print("Found server", ip, port)
			local_host = ip
		end)
		timer.delay(10, false, join_p2p)
	end
end

function final(self)
	self.server.stop()
end

function update(self, dt)
	if script_start == true then
		self.p2p.update()
	elseif server_start == true then
		self.server.update()
	elseif self.client then
		self.client.update()
	end
end

Does the client detect the host by fail connecting or is it something else? Have far do you get?

I don’t think the client detects anything. It prints to the console that it’s creating a TCP client but nothing else happens no matter how long I wait.

I don’t really know what’s wrong then. You have access to the code so you can do some debugging yourself to figure out where it goes wrong.

Does the example that comes with DefNet work for you?

Yeah the example works fine. If the server hasn’t been created then the client won’t connect but with mine it doesn’t show an error message

I’m running the program on my laptop and my phone. On my phone the program creates the client but doesn’t connect to the server at all, but on my laptop it just doesn’t create the client all together.