Since | Origin / Contributor | Maintainer | Source |
---|---|---|---|
2014-12-22 | Zeroday | Zeroday | net.c |
Constants to be used in other functions: net.TCP
, net.UDP
Creates a client.
net.createConnection(type, secure)
type
net.TCP
or net.UDP
secure
1 for encrypted, 0 for plainnet.socket sub module
net.createConnection(net.UDP, 0)
Creates a server.
net.createServer(type, timeout)
type
net.TCP
or net.UDP
timeout
for a TCP server timeout is 1~28’800 seconds (for an inactive client to be disconnected)net.server sub module
net.createServer(net.TCP, 30) -- 30s timeout
Closes the server.
net.server.close()
none
nil
-- creates a server
sv = net.createServer(net.TCP, 30)
-- closes the server
sv:close()
Listen on port from IP address.
net.server.listen(port,[ip],function(net.socket))
port
port numberip
IP address string, can be omittedfunction(net.socket)
callback function, pass to caller function as param if a connection is created successfullynil
-- 30s time out for a inactive client
sv = net.createServer(net.TCP, 30)
-- server listens on 80, if data received, print data to console and send "hello world" back to caller
sv:listen(80, function(c)
c:on("receive", function(c, pl)
print(pl)
end)
c:send("hello world")
end)
Closes socket.
close()
none
nil
Connect to a remote server.
connect(port, ip|domain)
port
port numberip
IP address or domain name stringnil
Provides DNS resolution for a hostname.
dns(domain, function(net.socket, ip))
domain
domain namefunction(net.socket, ip)
callback function. The first parameter is the socket, the second parameter is the IP address as a string.nil
sk = net.createConnection(net.TCP, 0)
sk:dns("www.nodemcu.com", function(conn, ip) print(ip) end)
sk = nil
Register callback functions for specific events.
on(event, function())
event
string, which can be «connection», «reconnection», «disconnection», «receive» or «sent»function(net.socket[, string])
callback function. The first parameter is the socket. If event is «receive», the second parameter is the received data as string.nil
sk = net.createConnection(net.TCP, 0)
sk:on("receive", function(sck, c) print(c) end )
sk:connect(80,"192.168.0.66")
sk:on("connection", function(sck,c)
-- Wait for connection before sending.
sk:send("GET / HTTP/1.1\r\nHost: 192.168.0.66\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
end)
Sends data to server.
send(string, function(sent))
string
data in string which will be sent to serverfunction(sent)
callback function for sending stringnil
Multiple consecutive send()
calls aren’t guaranteed to work (and often don’t) as network requests are treated as separate tasks by the SDK. Instead, subscribe to the «sent» event on the socket and send additional data (or close) in that callback. See #730 for an example and explanation.
Gets the IP address of the DNS server used to resolve hostnames.
net.dns.getdnsserver(dns_index)
dns_index which DNS server to get (range 0~1)
IP address (string) of DNS server
print(net.dns.getdnsserver(0)) -- 208.67.222.222
print(net.dns.getdnsserver(1)) -- nil
net.dns.setdnsserver("8.8.8.8", 0)
net.dns.setdnsserver("192.168.1.252", 1)
print(net.dns.getdnsserver(0)) -- 8.8.8.8
print(net.dns.getdnsserver(1)) -- 192.168.1.252
Resolve a hostname to an IP address. Doesn’t require a socket like net.socket.dns()
.
net.dns.resolve(host, function(ip))
host
hostname to resolvefunction(sk, ip)
callback called when the name was resolved. Don’t use sk
, it’s a socket used internally to resolve the hostname.nil
net.dns.resolve("www.google.com", function(sk, ip)
if (ip == nil) then print("DNS fail!") else print(ip) end
end)
Sets the IP of the DNS server used to resolve hostnames. Default: resolver1.opendns.com (208.67.222.222). You can specify up to 2 DNS servers.
net.dns.setdnsserver(dns_ip_addr, dns_index)
dns_ip_addr
IP address of a DNS serverdns_index
which DNS server to set (range 0~1). Hence, it supports max. 2 servers.nil