table of contents
net(3) | AFNIX Module | net(3) |
NAME¶
net - standard networking moduleSTANDARD NETWORKING MODULE¶
The Standard Networking module is an original implementation of networking facilities for the Internet Protocol. The module features standard TCP and UDP sockets for point to point communication as well as multicast socket. Numerous functions and objects for address manipulation are also included in this module. This module is also designed to support IP version 6 with certain platforms. IP address127.0.0.1 # ipv4 localhost 0:0:0:0:0:0:0:1 # ipv6 localhost
# load network module interp:library "afnix-net" # get the localhost address const addr (afnix:net:Address "localhost") # print the ip address println (addr:get-address)
# print the ip address information of the arguments # usage: axi XNET001.als [hosts ...] # get the network module interp:library "afnix-net" # print the ip address const ip-address-info (host) { try { const addr (afnix:net:Address host true) println "host name : " (addr:get-name) println " ip address : " (addr:get-address) println " canonical name : " ( addr:get-canonical-name) # get aliases const size (addr:get-alias-size) loop (trans i 0) (< i size) (i:++) { println " alias address : " ( addr:get-alias-address i) println " alias name : " ( addr:get-alias-name i) } } (errorln "error: " what:reason) } # get the hosts for (s) (interp:argv) (ip-address-info s) zsh> axi net-0001.als localhost host name : localhost ip address : 127.0.0.1 canonical name : localhost
assert 13 (afnix:net:get-tcp-service "daytime") assert 13 (afnix:net:get-udp-service "daytime")
# get the network module interp:library "afnix-net" # get the daytime server port const port (afnix:net:get-tcp-service "daytime") # create a tcp client socket const s (afnix:net:TcpClient "localhost" port) # read the data - the server close the connection while (s:valid-p) (println (s:readln))
zsh> axi 3201.als www.afnix.org
# fetch an html page by host and page # usage: axi 3203.als [host] [page] # get the network module interp:library "afnix-net" interp:library "afnix-sys" # connect to the http server and issue a request const send-http-request (host page) { # create a client sock on port 80 const s (afnix:net:TcpClient host 80) const saddr (s:get-socket-address) # format the request s:writeln "GET " page " HTTP/1.1" s:writeln "Host: " (saddr:get-canonical-name) s:writeln "Connection: close" s:writeln "User-Agent: afnix tcp client example" s:newline # write the result while (s:valid-p) (println (s:readln)) } # get the argument if (!= (interp:argv:length) 2) (afnix:sys:exit 1) const host (interp:argv:get 0) const page (interp:argv:get 1) # send request send-http-request host page
# get the libraries interp:library "afnix-net" interp:library "afnix-sys" # get the daytime server port const port (afnix:net:get-udp-service "time") # create a client socket and read the data const print-time (host) { # create a udp client socket const s (afnix:net:UdpClient host port) # send an empty datagram s:write # read the 4 bytes data and adjust to epoch const buf (s:read 4) const val (- (buf:get-quad) 2208988800) # format the date const time (afnix:sys:Time val) println (time:format-date) ' ' (time:format-time) } # check for one argument or use localhost const host (if (== (interp:argv:length) 0) "localhost" (interp:argv:get 0)) print-time host
# create a client socket and read the data const print-time (host) { # create a udp client socket const s (afnix:net:UdpClient host port) # send an empty datagram until the socket is valid s:write # retransmit datagram each second while (not (s:valid-p 1000)) (s:write) # read the 4 bytes data and adjust to epoch const buf (s:read 4) const val (- (buf:get-quad) 2208988800) # format the date const time (afnix:sys:Time val) println (time:format-date) ' ' (time:format-time) }
# create a client socket and read the data const print-socket-info (host) { # create a tcp client socket const s (afnix:net:TcpClient host port) # print socket address and port const saddr (s:get-socket-address) const sport (s:get-socket-port) println "socket ip address : " ( saddr:get-address) println "socket canonical name : " ( saddr:get-canonical-name) println "socket port : " sport # print peer address and port const paddr (s:get-peer-address) const pport (s:get-peer-port) println "peer ip address : " ( paddr:get-address) println "peer canonical name : " ( paddr:get-canonical-name) println "peer port : " pport }
trans s (afnix:net:TcpServer) trans s (afnix:net:TcpServer 8000) trans s (afnix:net:TcpServer 8000 5) trans s (afnix:net:TcpServer "localhost" 8000) trans s (afnix:net:TcpServer "localhost" 8000 5) trans s (afnix:net:TcpServer ( Address "localhost") 8000) trans s (afnix:net:TcpServer ( Address "localhost") 8000 5)
# get the network module interp:library "afnix-net" # create a tcp server on port 8000 const srv (afnix:net:TcpServer 8000) # wait for a connection const s (srv:accept) # echo the line until the end while (s:valid-p) (s:writeln (s:readln))
zsh> telnet localhost 8000 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. hello world ^D
# get the network module interp:library "afnix-net" # this function echo a line from the client const echo-server (s) { while (s:valid-p) (s:writeln (s:readln)) } # create a tcp server on port 8000 const srv (afnix:net:TcpServer 8000) # wait for a connection while true { trans s (srv:accept) launch (echo-server s) }
trans s (afnix:net:UdpServer) trans s (afnix:net:UdpServer 8000) trans s (afnix:net:UdpServer "localhost" 8000) trans s (afnix:net:UdpServer ( Address "localhost") 8000)
# get the network module interp:library "afnix-net" # create a udp server on port 8000 const srv (afnix:net:UdpServer 8000) # wait for a connection while true { trans dg (srv:accept) dg:writeln (dg:readln) }
# wait for a datagram trans dg (s:accept) # assert datagram type assert true (datagram-p dg) # get contents length println "datagram buffer size : " (dg:get-buffer-length) # read a line from this datagram trans line (dg:readln) # send it back to the sender s:writeln line
# create an address and a tcp socket const addr (afnix:net:Address "localhost") const sid (afnix:net:TcpSocket) # connect the socket sid:connect 13 addr
# create an address and a tcp socket const addr (afnix:net:Address "localhost") const sid (afnix:net:TcpSocket) # disable the naggle algorithm sid:set-option sid:NO-DELAY true # connect the socket sid:connect 13 addr
NETWORKING REFERENCE¶
Addressaddress-p
Inheritance
Object
Constructors
Address (String)
The Address constructor create an IP address object by name. The name argument
is a string of a host name or a valid IP address representation.
Address (String Boolean)
The Address constructor create an IP address object by name and force the
reverse lookup resolution depending on the boolean flag value. The first
argument is a string of a host name or a valid IP address representation. The
second argument is a boolean flag that indicates whether or not reverse lookup
must occur during the construction.
Operators
== -> Boolean (Address)
The == operator returns true if the calling object is equal to the address
argument.
!= -> Boolean (Address)
The != operator returns true if the calling object is not equal to the address
argument.
< -> Boolean (Address)
The < operator returns true if the calling address is less than the address
object.
<= -> Boolean (Address)
The <= operator returns true if the calling address is less equal than the
address object.
> -> Boolean (Address)
The > operator returns true if the calling address is greater than the
address object.
>= -> Boolean (Address)
The <= operator returns true if the calling address is greater equal than the
address object.
++ -> Address (Address)
The ++ operator increments the calling address by one position.
Methods
resolve -> String Boolean (none)
The resolve method resolves an host name and eventually performs a reverse
lookup. The first argument is a string of a host name or a valid IP address
representation. The second argument is a boolean flag that indicates whether
or not reverse lookup must occur during the resolution.
get-name -> String (none)
The get-name method returns the original name used during the object
construction.
get-address -> String (none)
The get-address method returns a string representation of the IP address. The
string representation follows the IP version 4 or IP version 6 preferred
formats, depending on the internal representation.
get-vector -> Vector (none)
The get-vector method returns a vector representation of the IP address. The
vector result follows the IP version 4 or IP version 6 preferred format,
depending on the internal representation.
get-canonical-name -> String (none)
The get-canonical-name method returns a fully qualified name of the address. The
resulting name is obtained by performing a reverse lookup. Note that the name
can be different from the original name.
get-alias-size -> Integer (none)
The get-alias-size method returns the number of aliases for the address. The
number of aliases includes as well the primary resolved name which is located
at index 0.
get-alias-name -> String (Integer)
The get-alias-name method returns a fully qualified name of the address alias by
index. The first argument is the alias index number which must be in the alias
index range. The resulting name is obtained by performing a reverse lookup.
Note that the name can be different from the original name. Using index 0 is
equivalent to call get-canonical-name.
get-alias-address -> String
(Integer)
The get-alias-address method returns a string representation of the IP address
alias by index. The first argument is the alias index number which must be in
the alias index range. The string representation follows the IP version 4 or
IP version 6 preferred formats, depending on the internal representation.
Using index 0 is equivalent to call get-address.
get-alias-vector -> Vector (Integer)
The get-alias-vector method returns a vector representation of the IP address
alias by index. The first argument is the alias index number which must be in
the alias index range. The vector result follows the IP version 4 or IP
version 6 preferred format, depending on the internal representation. Using
index 0 is equivalent to call get-vector.
Functions
get-loopback -> String (none)
The get-loopback function returns the name of the machine loopback. On a UNIX
system, that name is localhost.
get-tcp-service -> String (Integer)
The get-tcp-service function returns the name of the tcp service given its port
number. For example, the tcp service at port 13 is the daytime server.
get-udp-service -> String (Integer)
The get-udp-service function returns the name of the udp service given its port
number. For example, the udp service at port 19 is the chargen server.
Socket
socket-p
Inheritance
InputStreamOutputStream
Constants
REUSE-ADDRESS
The REUSE-ADDRESS constant is used by the set-option method to enable socket
address reuse. This option changes the rules that validates the address used
by bind. It is not recommended to use that option as it decreases TCP
reliability.
BROADCAST
The BROADCAST constant is used by the set-option method to enable broadcast of
packets. This options only works with IP version 4 address. The argument is a
boolean flag only.
DONT-ROUTE
The DONT-ROUTE constant is used by the set-option method to control if a packet
is to be sent via the routing table. This option is rarely used with . The
argument is a boolean flag only.
KEEP-ALIVE
The KEEP-ALIVE constant is used by the set-option method to check periodically
if the connection is still alive. This option is rarely used with . The
argument is a boolean flag only.
LINGER
The LINGER constant is used by the set-option method to turn on or off the
lingering on close. If the first argument is true, the second argument is the
linger time.
RCV-SIZE
The RCV-SIZE constant is used by the set-option method to set the receive buffer
size.
SND-SIZE
The SND-SIZE constant is used by the set-option method to set the send buffer
size.
HOP-LIMIT
The HOP-LIMIT constant is used by the set-option method to set packet hop
limit.
MULTICAST-LOOPBACK
The MULTICAST-LOOPBACK constant is used by the set-option method to control
whether or not multicast packets are copied to the loopback. The argument is a
boolean flag only.
MULTICAST-HOP-LIMIT
The MULTICAST-HOP-LIMIT constant is used by the set-option method to set the hop
limit for multicast packets.
MAX-SEGMENT-SIZE
The MAX-SEGMENT-SIZE constant is used by the set-option method to set the TCP
maximum segment size.
NO-DELAY
The NO-DELAY constant is used by the set-option method to enable or disable the
Naggle algorithm.
Methods
bind -> none (Integer)
The bind method binds this socket to the port specified as the argument.
bind -> none (Integer Address)
The bind method binds this socket to the port specified as the first argument
and the address specified as the second argument.
connect -> none (Integer Address
[Boolean])
The connect method connects this socket to the port specified as the first
argument and the address specified as the second argument. A connected socket
is useful with udp client that talks only with one fixed server. The optional
third argument is a boolean flag that permits to select whether or not the
alias addressing scheme should be used. If the flag is false, the default
address is used. If the flag is true, an attempt is made to connect to the
first successful address that is part of the alias list.
open-p -> Boolean (none)
The open-p predicate returns true if the socket is open. The method checks that
a descriptor is attached to the object. This does not mean that the descriptor
is valid in the sense that one can read or write on it. This method is useful
to check if a socket has not been closed.
shutdown -> Boolean (none|Boolean)
The shutdown method shutdowns or close the connection. Without argument, the
connection is closed without consideration for those symbols attached to the
object. With one argument, the connection is closed in one direction only. If
the mode argument is false, further receive is disallowed. If the mode
argument is true, further send is disallowed. The method returns true on
success, false otherwise.
ipv6-p -> Boolean (none)
The ipv6-p predicate returns true if the socket address is an IP version 6
address, false otherwise.
get-socket-address -> Address (none)
The get-socket-address method returns an address object of the socket. The
returned object can be later used to query the canonical name and the ip
address.
get-socket-port -> Integer (none)
The get-socket-port method returns the port number of the socket.
get-socket-authority -> String
(none)
The get-socket-authority method returns the authority string in the form of an
address and port pair of the socket.
get-peer-address -> Address (none)
The get-peer-address method returns an address object of the socket's peer. The
returned object can be later used to query the canonical name and the ip
address.
get-peer-port -> Integer (none)
The get-peer-port method returns the port number of the socket's peer.
get-peer-authority -> String (none)
The get-peer-authority method returns the authority string in the form of an
address and port pair of the socket's peer.
set-option -> Boolean (constant
[Boolean|Integer] [Integer])
The set-option method set a socket option. The first argument is the option to
set. The second argument is a boolean value which turn on or off the option.
The optional third argument is an integer needed for some options.
set-encoding-mode -> none
(Item|String)
The set-encoding-mode method sets the input and output encoding mode. In the
first form, with an item, the stream encoding mode is set directly. In the
second form, the encoding mode is set with a string and might also alter the
stream transcoing mode.
set-input-encoding-mode -> none
(Item|String)
The set-input-encoding-mode method sets the input encoding mode. In the first
form, with an item, the stream encoding mode is set directly. In the second
form, the encoding mode is set with a string and might also alter the stream
transcoing mode.
get-input-encoding-mode -> Item
(none)
The get-input-encoding-mode method return the input encoding mode.
set-output-encoding-mode -> none
(Item|String)
The set-output-encoding-mode method sets the output encoding mode. In the first
form, with an item, the stream encoding mode is set directly. In the second
form, the encoding mode is set with a string and might also alter the stream
transcoing mode.
get-output-encoding-mode -> Item
(none)
The get-output-encoding-mode method return the output encoding mode.
TcpSocket
tcp-socket-p
Inheritance
Socket
Constructors
TcpSocket (none)
The TcpSocket constructor creates a new tcp socket.
Methods
accept -> TcpSocket (none)
The accept method waits for incoming connection and returns a TcpSocket object
initialized with the connected peer. The result socket can be used to perform
i/o operations. This method is used by tcp server.
listen -> Boolean (none|Integer)
The listen method initialize a socket to accept incoming connection. Without
argument, the default number of incoming connection is 5. The integer argument
can be used to specify the number of incoming connection that socket is
willing to queue. This method is used by tcp server.
TcpClient
tcp-client-p
Inheritance
TcpSocket
Constructors
TcpClient (String Integer)
The TcpClient constructor creates a new tcp client socket by host name and port
number.
TcpServer
tcp-server-p
Inheritance
TcpSocket
Constructors
TcpServer (none)
The TcpServer constructor creates a default tcp server.
TcpServer (Integer)
The TcpServer constructor creates a default tcp server which is bound on the
specified port argument.
TcpServer (Integer Integer)
The TcpServer constructor creates a default tcp server which is bound on the
specified port argument. The second argument is the backlog value.
TcpServer (String Integer)
The TcpServer constructor creates a tcp server by host name and port number. The
first argument is the host name. The second argument is the port number.
TcpServer (String Integer Integer)
The TcpServer constructor creates a tcp server by host name and port number. The
first argument is the host name. The second argument is the port number. The
third argument is the backlog.
Datagram
datagram-p
Inheritance
Socket
UdpSocket
udp-socket-p
Inheritance
Socket
Constructors
UdpSocket (none)
The UdpSocket constructor creates a new udp socket.
Methods
accept -> Datagram (none)
The accept method waits for an incoming datagram and returns a Datagram object.
The datagram is initialized with the peer address and port as well as the
incoming data.
UdpClient
udp-client-p
Inheritance
UdpSocket
Constructors
UdpClient (String Integer)
The UdpClient constructor creates a new udp client by host and port. The first
argument is the host name. The second argument is the port number.
UdpServer
udp-server-p
Inheritance
UdpSocket
Constructors
UdpServer (none)
The UdpServer constructor creates a default udp server object.
UdpServer (String|Address)
The UdpServer constructor creates a udp server object by host. The first
argument is the host name or host address.
UdpServer (String|Address Integer)
The UdpServer constructor creates a udp server object by host and port. The
first argument is the host name or host address. The second argument is the
port number.
Multicast
multicast-p
Inheritance
UdpSocket
Constructors
Multicast (String|Address)
The Multicast constructor creates a multicast socket object by host. The first
argument is the host name or host address.
Multicast (String|Address Integer)
The Multicast constructor creates a multicast socket object by host and port.
The first argument is the host name or host address. The second argument is
the port number.
2012-03-26 | AFNIX |