It is indeed possible to do this. There are limitations however. Since the Winsock control is a "licensed control" one must pass a runtime license to the script host.
This works fine for an HTA or even an HTML page, because there is a special IE mechanism for doing it.
WSH has no such facility however. This means that such a script, whether WScript or CScript, will only run on machines that have a developer
license installed, usually by installing Visual Studio or VB.
Don't give up though. I have had excellent luck employing the free Dimac w3Sockets component. Go to Dimac
, select w3Sockets from the list in the left nav-area of the page under Free Products
While designed for use from an ASP page, it works great in a .vbs or .wsf file. You didn't say what the remote server type is. w3Sockets supports Telnet as well as bare TCP sockets though. If you need to talk to an HTTP server, you'll need to send the HTTP protocol manually over a bare socket. But the Winsock control would leave you with that issue anyway.
I'm making the assumption that you want a blocking socket. w3Sockets can only
operate in this fashion. For simple applications this is a boon. The Winsock control can't do this, and you must write event-handling logic to deal with its async nature.
I'm sure there are other free alternatives as well. I use this one because I need to deal with Telnet off and on, and contrary to a lot of sample code, Telnet <> TCP. Most Telnet servers will spew a lot of Telnet option negotiation sequences and such (and expect same back). w3Sockets can (optionally) deal with this for you "behind the scenes."
Trivial sample, dimac.wsf
<object id="w3sock" progid="socket.tcp" />
w3sock.DoTelnetEmulation = True
w3sock.TelnetEmulation = "TTY"
w3sock.Host = "localhost:2023"
w3sock.SendLine "prompt $LREADY$G"
Here I tell w3Sockets I want to do Telnet, terminal type "TTY" (dumb as possible), and I log on.
Then I set the Windows Telnet server's command prompt to <READY> (by using $LREADY$G) because there will be > symbols in the text I want to capture.
Then I send a dir
command, and wait for the command prompt to tell me I have it all. Then I grab the results from the buffer and display it.
If you prefer to write old-style .vbs files, you'll need something like:Dim w3sock
Set w3sock = CreateObject("socket.tcp")