Module nixio.Socket
Socket Object.
Supports IPv4, IPv6 and UNIX (POSIX only) families.
Functions
getsockname () | Get the local address of a socket. |
getpeername () | Get the peer address of a socket. |
bind (host, port) | Bind the socket to a network address. |
connect (host, port) | Connect the socket to a network address. |
listen (backlog) | Listen for connections on the socket. |
accept () | Accept a connection on the socket. |
sendto (buffer, host, port, offset, length) | Send a message on the socket specifying the destination. |
send (buffer, offset, length) | Send a message on the socket. |
write (buffer, offset, length) | Send a message on the socket (This is an alias for send). |
recvfrom (length) | Receive a message on the socket including the senders source address. |
recv (length) | Receive a message on the socket. |
read (length) | Receive a message on the socket (This is an alias for recv). |
close () | Close the socket. |
shutdown (how) | Shut down part of a full-duplex connection. |
fileno () | Get the number of the filedescriptor. |
setblocking (blocking) | Set the blocking mode of the socket. |
setopt (level, option, value) | Set a socket option. |
getopt (level, option) | Get a socket option. |
Functions
- getsockname ()
-
Get the local address of a socket.
Returns:
- IP-Address
- Port
- getpeername ()
-
Get the peer address of a socket.
Returns:
- IP-Address
- Port
- bind (host, port)
-
Bind the socket to a network address.
Parameters:
- host Host (optional, default: all addresses)
- port Port or service description
Returns:
-
true
Usage:
This function calls getaddrinfo() and bind() but NOT listen().
If <em>host</em> is a domain name it will be looked up and bind() tries the IP-Addresses in the order returned by the DNS resolver until the bind succeeds.
UNIX sockets ignore the <em>port</em>, and interpret <em>host</em> as a socket path.
- connect (host, port)
-
Connect the socket to a network address.
Parameters:
- host Hostname or IP-Address (optional, default: localhost)
- port Port or service description
Returns:
-
true
Usage:
This function calls getaddrinfo() and connect().
If <em>host</em> is a domain name it will be looked up and connect() tries the IP-Addresses in the order returned by the DNS resolver until the connect succeeds.
UNIX sockets ignore the <em>port</em>, and interpret <em>host</em> as a socket path.
- listen (backlog)
-
Listen for connections on the socket.
Parameters:
- backlog Length of queue for pending connections
Returns:
-
true
- accept ()
-
Accept a connection on the socket.
Returns:
- Socket Object
- Peer IP-Address
- Peer Port
- sendto (buffer, host, port, offset, length)
-
Send a message on the socket specifying the destination.
Parameters:
- buffer Buffer holding the data to be written.
- host Target IP-Address
- port Target Port
- offset Offset to start reading the buffer from. (optional)
- length Length of chunk to read from the buffer. (optional)
Returns:
-
number of bytes written
Usage:
<strong>Warning:</strong> It is not guaranteed that all data in the buffer is written at once. You have to check the return value - the number of bytes actually written - or use the safe IO functions in the high-level IO utility module.
Unlike standard Lua indexing the lowest offset and default is 0.
- send (buffer, offset, length)
-
Send a message on the socket.
This function is identical to sendto except for the missing destination
paramters. See the sendto description for a detailed description.
Parameters:
- buffer Buffer holding the data to be written.
- offset Offset to start reading the buffer from. (optional)
- length Length of chunk to read from the buffer. (optional)
Returns:
-
number of bytes written
See also:
- write (buffer, offset, length)
-
Send a message on the socket (This is an alias for send).
See the sendto description for a detailed description.
Parameters:
- buffer Buffer holding the data to be written.
- offset Offset to start reading the buffer from. (optional)
- length Length of chunk to read from the buffer. (optional)
Returns:
-
number of bytes written
See also:
- recvfrom (length)
-
Receive a message on the socket including the senders source address.
Parameters:
- length Amount of data to read (in Bytes).
Returns:
- buffer containing data successfully read
- host IP-Address of the sender
- port Port of the sender
Usage:
<strong>Warning:</strong> It is not guaranteed that all requested data is read at once. You have to check the return value - the length of the buffer actually read - or use the safe IO functions in the high-level IO utility module.
The length of the return buffer is limited by the (compile time) nixio buffersize which is <em>nixio.const.buffersize</em> (8192 by default). Any read request greater than that will be safely truncated to this value.
- recv (length)
-
Receive a message on the socket.
This function is identical to recvfrom except that it does not return
the sender's source address. See the recvfrom description for more details.
Parameters:
- length Amount of data to read (in Bytes).
Returns:
-
buffer containing data successfully read
See also:
- read (length)
-
Receive a message on the socket (This is an alias for recv).
See the recvfrom description for more details.
Parameters:
- length Amount of data to read (in Bytes).
Returns:
-
buffer containing data successfully read
See also:
- close ()
-
Close the socket.
Returns:
-
true
- shutdown (how)
-
Shut down part of a full-duplex connection.
Parameters:
- how (optional, default: rdwr) ["rdwr", "rd", "wr"]
Returns:
-
true
- fileno ()
-
Get the number of the filedescriptor.
Returns:
-
file descriptor number
- setblocking (blocking)
-
Set the blocking mode of the socket.
Parameters:
- blocking (boolean)
Returns:
-
true
- setopt (level, option, value)
-
Set a socket option.
Parameters:
- level Level ["socket", "tcp", "ip", "ipv6"]
- option Option ["keepalive", "reuseaddr", "sndbuf", "rcvbuf", "priority", "broadcast", "linger", "sndtimeo", "rcvtimeo", "dontroute", "bindtodevice", "error", "oobinline", "cork" (TCP), "nodelay" (TCP), "mtu" (IP, IPv6), "hdrincl" (IP), "multicast_ttl" (IP), "multicast_loop" (IP, IPv6), "multicast_if" (IP, IPv6), "v6only" (IPv6), "multicast_hops" (IPv6), "add_membership" (IP, IPv6), "drop_membership" (IP, IPv6)]
- value Value
Returns:
-
true
- getopt (level, option)
-
Get a socket option.
Parameters:
- level Level ["socket", "tcp", "ip", "ipv6"]
- option Option ["keepalive", "reuseaddr", "sndbuf", "rcvbuf", "priority", "broadcast", "linger", "sndtimeo", "rcvtimeo", "dontroute", "bindtodevice", "error", "oobinline", "cork" (TCP), "nodelay" (TCP), "mtu" (IP, IPv6), "hdrincl" (IP), "multicast_ttl" (IP), "multicast_loop" (IP, IPv6), "multicast_if" (IP, IPv6), "v6only" (IPv6), "multicast_hops" (IPv6), "add_membership" (IP, IPv6), "drop_membership" (IP, IPv6)]
Returns:
-
Value