[ Previous ] [ Contents ] [ Index ] [ Next ]

ns_sockopen

Overview

Connect to a remote host on the specified port

Syntax

ns_sockopen ?-nonblock | -timeout seconds? host port

Description

ns_sockopen uses socket(), bind(), and connect() calls to connect to the remote host (host) on the specified port (port). ns_sockopen returns a list of two file ids: the first one for reads and the second one for writes.

The -nonblock option specifies that ns_sockopen will return immediately, while the connect is still in progress by the operating system. If you use -nonblock, you then can call ns_sockcheck with the write file id to see if the connect was actually completed.

The -timeout option specifies how long to wait for the connect to be made in seconds before timing out.

Example

This is a simple example that fetches a page from an http server (www.aolserver.com) on port 80. The headers will be in the $headers list, and the body will be in the $page list.

    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

A more advanced example containing ns_sockopen is provided under ns_sockselect.

Top of Page

[ Previous ] [ Contents ] [ Index ] [ Next ]
Copyright © 1996 America Online, Inc.