How to start a socket in codea

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

@Archimedes, it’s better if you keep all your socket-related stuff in one thread, helps keep the forums tidy

Nice work, we do need a good example to help people use this =D>

A modified version that shows what the other device is typing in realtime.
I have tested is and it works.

function setup()
    socket = require("socket")
--here you put the port of your socket. 
    local myip, myport = getLocalIP(), 5544
    print("Connect to " .. myip .. ":" .. myport)

    server = socket.udp()
    server:setsockname(myip, myport)
    server:settimeout(0)

    client = socket.udp()

--ip must be with quotations and periods seperating the segments as such
-- "111.222.3.4" (not actual ip address that im aware of) port is simple
-- such as 5544, doesnt need quotations or anything.
-- here you put the ip and port of the device you want to connect to. 
    client:setpeername("ip of other device", "port of other device")
    client:settimeout(0)

    parameter.text("msg_to_send", "")
    parameter.action("send message", sendMessage)
end

function getLocalIP()
    --you input your ip here
    local randomIP = "your ip, its need quotations and periods"
    --this port below is just a place holder it does nothing but it needs to be there
    local randomPort = "1991" 
    local randomSocket = socket.udp() 
    randomSocket:setpeername(randomIP,randomPort) 

    local localIP, somePort = randomSocket:getsockname()

    randomSocket:close()
    randomSocket = nil

    return localIP
end

function sendMessage()
    print("Client sent to server: '" .. msg_to_send .. "' at", os.date("%x, %X", os.time()))
end

function draw()
--I modified the original so that the message is sent each frame, you see the other -- person's message typed out letter by letter as they type it.
--It shows your message on top and the message being received on the bottom
    background(0, 0, 0, 255)
    client:send(msg_to_send)
    stroke(255, 255, 255, 255)
    fontSize(30)
    text(msg_to_send, 250, 600)
    

    local data, msg_or_ip, port_or_nil = server:receivefrom()
    if data then
        stroke(255, 255, 255, 255)
        fontSize(30)
        text(data, 250, 300)
        
        
    end
end

This is also modified from how @JakAttak originally made it so that it allows for two way data sending between devices in one program.
Have fun