Programming Environment for Numerical Computing

Socket

The socket namespace includes BSD-like network functions:

s = socket.new(af,type,proto)

-- Create socket where:
-- af: network family (socket.AF_INET by default)
-- type: socket type (socket.SOCK_STREAM by default)
-- proto: Protocol (socket.IPPROTO_TCP by default)
-- Returns the socket identifier

ok = socket.bind(s, addr, port)

-- Bind socket s, where:
-- s: socket identifier
-- addr: address (usually addr = socket.INADDR_ANY)
-- port: port to bind to
-- Returns true on success

ok = socket.listen(s, backlog)

-- Listen on socket, where:
-- s: socket identifier
-- backlog: maximum queue length
-- Returns true on success

ok = socket.connect(s, addr, port)

-- Connect , where:
-- s: socket identifier
-- addr: address
-- port: port to connect to
-- Returns true on success

sa = socket.accept(s, addr, port)

-- Accept connection and create new socket:
-- s: socket identifier
-- Returns the new socket identifier sa

ok = socket.timeout(s, to)

-- Set the recv and send timeout, where:
-- s: socket identifier
-- timeout in milliseconds
-- Returns true on success

ok = socket.setsockopt(s, opt, val)

-- Set socket option, where:
-- s: socket identifier
-- option to set (ex: socket.SO_SNDTIMEO)
-- val: option value
-- Returns true on success

ip = socket.getpeername(s)

-- Get the socket s peer ip address
-- s: socket identifier
-- Returns peer ip address

hn = socket.gethostbyaddr(s, addr)

-- Get the host name for ip address
-- s: socket identifier
-- addr: ip address
-- Returns host name

ha = socket.gethostbyname(s, name)

-- Get the ip address for host name
-- s: socket identifier
-- name: host name
-- Returns ip address

ha = socket.getsockname(s)

-- Get the the socket name, where:
-- s: socket identifier
-- name: host name
-- Returns socket name

ok = socket.send(s, data, flags)

-- Send data, where:
-- s: socket identifier
-- data: data to send
-- flags: optional flags
-- Returns true on success

ok = socket.sendto(s, addr, port, data, flags)

-- Send data, where:
-- s: socket identifier
-- addr: address
-- port: port to send to
-- data: data to send
-- flags: optional flags
-- Returns true on success

data = socket.recv(s, datasize, flags)

-- Receive data, where:
-- s: socket identifier
-- datasize: data size to be received
-- flags: optional flags
-- Returns received data

data = socket.recvfrom(s, addr, port, datasize, flags)

-- Receive data from, where:
-- s: socket identifier
-- addr: address
-- port: port to receive from
-- datasize: data size to be received
-- flags: optional flags
-- Returns received data

errf = socket.iserr(s)

-- Get the error flag:
-- s: socket identifier
-- Returns true if error flag set

errm = socket.geterr(s)

-- Get the error message:
-- s: socket identifier
-- Returns the error message, if any

socket.shutdown(s)

-- Shutdown socket, where:
-- s: socket identifier

socket.delete(s)

-- Delete socket and free resources, where:
-- s: socket identifier

Example:

Copy the following script in the editor and click Run (or press F12 on Windows and Linux)

-- BSD Socket
cls()

s = socket.new(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
ip = socket.gethostbyname(s, "www.debian.org")
socket.connect(s, ip, 80)
socket.send(s, "HEAD / HTTP/1.0\r\n\r\n")
data = socket.recv(s, 1024)
print(data)
socket.delete(s)

 

 Copyright(C) 2010-2024 Prof. Sidi HAMADY