Functions:
network.socket(["tcp" | "udp"]) - Creates a new socket in either TCP or UDP mode; defaults to TCP. Returns the socket as an object. NOTE: UDP unfinished.
socket:connect("host/IP", port) - Connects a socket to a server, returns true/false and error message. Retruns true/false and error message (on fail)
socket:listen("host/IP", port) - Sets the socket to listen (as a server) for incoming connections, host/IP and port are the ones to listen to; use "" for the host/IP to listen on any available connections. Returns true/false and error message (on fail)
socket:send("message goes here") - Send a message to the remote destination on this socket. Returns true/false and error message (on fail)
socket:close() - Shuts down the connection; this also happens automatically if/when the socket object is destroyed
socket:socket() - Returns the socket ID (an integer value)
Events:
socketconnected - A server application pushes this event when a new client has connected. The new socket object for this client will be pushed as the second (and only) additional parameter.
socketreceived - Whenever a socket receives any message, this event is pushed. Additional parameters are the socket ID (not object!) and the actual message.
socketdisconnected - When a connection is closed for any reason, this event is raised.The socket ID is also pushed as the second and only parameter.
socketerror - whenever there's any unhandled error on a socket. Socket ID and error number are also pushed.
Notes:
All sockets are run in separate threads and will not block or limit your application.
All received data is, therefor, handled internally and only accessible through the event system.
Make sure you notice that socketconnected pushes a socket object while other socket events only receive the socket ID; a server application should store new clients in a list for use, otherwise the connection could be closed when the variable goes out of scope.
Examples:
Here's a basic client/server setup. It initiates the connections, handled multiple clients, and sends some messages.
server.lua
Code: Select all
local serverSocket;
local clients;
function macro.init()
clients = {};
serverSocket = network.socket();
local success,msg = serverSocket:listen("", 9999);
if( not success ) then
error("Failed to listen: " .. msg, 0);
end
print("Waiting for clients");
end
function macro.main(dt)
end
function macro.event(e, ...)
if( e == "socketconnected" ) then
sock = ...;
print("New client found:", sock);
clients[sock:socket()] = sock;
sock:send("Greetings from the server");
end
if( e == "socketreceived" ) then
sock, msg = ...;
print("Data received from client:", sock, msg);
end
if( e == "socketdisconnected" ) then
sock = ...;
print("Client disconnected", sock);
clients[sock] = nil;
end
end
Code: Select all
function macro.init()
socket = network.socket();
local success,msg = socket:connect("127.0.0.1", 9999);
if( not success ) then
error("Failed to listen: " .. msg, 0);
end
print("Connected to host");
socket:send("Hello!");
end
function macro.main(dt)
end
function macro.event(e, ...)
if( e == "socketreceived" ) then
sock, msg = ...;
print("Data received from server", msg);
end
if( e == "socketdisconnected" ) then
sock = ...;
print("Socket disconnected", sock);
end
end
Accessing HTML content from webservers is not much different. Just open a socket in TCP mode, connect on port 80, and send the standard HTTP request header.
web.lua:
Code: Select all
function macro.init()
local socket = network.socket("tcp");
print(socket);
local connected,err = socket:connect("www.google.com", 80);
print("Connected:", connected, err);
print("Send:", socket:send("GET / HTTP/1.1\r\n\r\n"));
end
function macro.main()
return true;
end
function macro.event(e, ...)
if( e == "socketconnected" ) then
print(e, ...);
end
if( e == "socketreceived" ) then
print("Socket received:", ...);
end
if( e == "socketdisconnected" ) then
print("Socket closed.", ...);
end
if( e == "socketerror" ) then
print("Socket error", ...);
end
end