13.1. Opening a Socket

[<<<] [>>>]

Networking in ScriptBasic is very simple. As you can open a file on the local disk you can open a service on a remote machine and print into it and get characters from it.

When you want to open a file to a service on a remote machine you should specify the name or the IP number of the remote machine and the port number in the open statement as a file name and you should open the file with mode socket. For example:

OPEN "www.digital.com:80" FOR socket AS 1

will open a connection to the web server of the machine named wwww.digital.com . The port number is 80 in this example, which is the usual port of a web server. You can also write:

open "16.193.50.33:80" for socket as 1

specifying the IP number of the machine. (The IP number listed in this documentation is an example and may not be the IP number of any server.) After opening a socket you can print into the channel and read from it using the commands print, printnl, line input and the function input. To retrieve a simple web page the following sample program can be used:

on error goto ErrorLabel
 open "16.193.50.33:80" for socket as 1
 print#1,"GET http://localhost/ HTTP/1.0\n\n"

while not eof(1) line input#1,a print a wend close 1 stop ErrorLabel: print "The web server 16.193.50.33 on port 80 is not reachable\n"

The open statement generates an error in case the remote machine is not reachable or if the domain name can not be resolved. If the IP number is specified instead of the name of the remote machine ScriptBasic does not query the DNS system, but tries to connect to the remote machine immediately. This way you can connect to remote machines a bit faster but you risk that the machine IP number is changed. A machine is usually identified by its name. The IP number is a low-level identification that reflects the topological location of the machine on the Internet. If the host is moved from one service provider to another or some other technology changes or developments make it necessary the IP number of the machine is going to change. Therefore it is recommended to use the name of the machine if possible.


[<<<] [>>>]