So I decided to do some work with @JakAttak socket program. I stripped it down to the bare necessities and included comments on what does what and why.
function setup()
socket = require("socket")
-- this tells the program to use the socket namespace
server = socket.udp()
--this creates a socket host with the ip and port inserted below
--the ip must be the ip of the host device, port is any integer between 0 and 6400
server:setsockname("ip of host", "port of host")
server:settimeout(0)
--this creates a client socket(ie the one that joins)
client = socket.udp()
--the ip and port here must be the same as the host to connect to
client:setpeername("ip to connect to", "port to connect to")
client:settimeout(0)
parameter.text("msg_to_send", "")
parameter.action("send message", sendMessage)
end
function sendMessage()
--this sends a string to the socket the client is connected to
client:send(msg_to_send)
print(msg_to_send)
end
function draw()
background(255, 255, 255, 255)
--the only piece of information that actually needs attention is 'data'
--the other two terms are dead end paths, they are here only because server:receivefrom() returns three pieces of information, the message, the ip of client, and port of client
local data, msg_or_ip, port_or_nil = server:receivefrom()
if data then
print(data)
end
end
In actual useage this would be two programs, as such
This is host.
function setup()
socket = require("socket")
-- this tells the program to use the socket namespace
server = socket.udp()
--this creates a socket host with the ip and port inserted below
--the ip must be the ip of the host device, port is any integer between 0 and 6400
server:setsockname("ip of host", "port of host")
server:settimeout(0)
end
function draw()
background(255, 255, 255, 255)
--the only piece of information that actually needs attention is 'data'
--the other two terms are dead end paths, they are here only because server:receivefrom() returns three pieces of information, the message, the ip of client, and port of client
local data, msg_or_ip, port_or_nil = server:receivefrom()
if data then
print(data)
end
end
This is client
function setup()
socket = require("socket")
--this creates a client socket(ie the one that joins)
client = socket.udp()
--the ip and port here must be the same as the host to connect to
client:setpeername("ip to connect to", "port to connect to")
client:settimeout(0)
parameter.text("msg_to_send", "")
parameter.action("send message", sendMessage)
end
function sendMessage()
--this sends a string to the socket the client is connected to
client:send(msg_to_send)
print(msg_to_send)
end