ns_sockopen - Connect to a remote host on the specified port
ns_sockopen uses socket(), bind(), and connect() calls to connect to a remote hosta the specified port.
Connect to the remote host specified by host port. The command returns a list of two file-ids: the first one for read and the second one for write operations.
The -async option specifies that the command will return immediately, while the connect operation is still in progress by the operating system. If you use -async, you then can call ns_sockcheck with the write file-id to see if the connect operation was actually completed.
The -nonblock is implied by -async. It can be used to connect to the target in a blocking fashion, but it sends the socket to nonblocking, once it is connected.
The option -timeout specifies how long to wait for the connect operation to be finished. The value can be specified in the form secs?:microsecs?, or secs.fraction, or as a number with a time unit. The option -timeout is mutual exclusive with -async.
The options -localhost and -localport can be used to specify the local endpoints of the connection. In the following example, these option ensures that the outgoing connection to www.example.com originates from the specified local IP address 192.168.1.100 and port 12345, replacing effectively the ephemeral port.
% ns_sockopen ... -myaddr 192.168.1.100 -myport 12345 ... www.example.com 80
Usage Example:
set fds [ns_sockopen www.aolserver.com 80] set rid [lindex $fds 0] set wid [lindex $fds 1] puts $wid "GET /index.htm HTTP/1.0\r\n\r" flush $wid while {[set line [string trim [gets $rid]]] != ""} { lappend headers $line } set page [read $rid] close $rid close $wid