This time I'll try to be more helpful.

On 11 Dec 2005, at 21:34, Graham Samuel wrote:

> Sockets, do we need them

If you just need to connect to internet URLs, you can use the get  
URL, load URL, etc, calls, and not have to know anything about  
sockets. But if you want to do something more fancy, such as build  
your own web server, mail client, or completely new internet protocol  
(see below), then you probably need to know a little. (But I can tell  
you from my involvement with libUrl, you don't really need to know  
that much, at least if you're doing these kind of things in Rev.)

> and is the term just a token or does it carry some metaphorical  
> meaning?

Both of those I guess. The term "socket" is not just a Rev  
expression, but is used in all computer development environments. A  
"socket" is part of the connection that computers use to communicate  
with each other on TCP/IP networks such as the internet and on  
intranets too.

The following description/analogy may not be strictly accurate, but  
I've found it quite helpful.

Remember those old-fashioned telephone switchbords where the operator  
stuck wires into various holes on the board. Well, imagine your  
computer has a similar board inside with a lot of holes numbered from  
1 to 65000 or thereabouts. And the computer you want to connect to  
(for example, the computer that hosts www.runrev.com) has a similar  
set of holes. To connect your computer to the runrev computer, you  
can imagine connecting a wire in one of the 65000 holes in your  
computer and connecting it one of the holes in the RunRev computer.  
The "sockets" are the endpoints of the connection. However, with Rev,  
we don't really need to think about the endpoints too much, and it is  
often easier just to think about the connection itself. However, much  
as I'd prefer to script this:

   open connection to www.runrev.com

you have to script this:

   open socket to www.runrev.com #(not quite correct, but see later)

Now remember those 65000 or so holes on the board in your computer,  
and the other 65000 holes on the renRev computer. Well those holes  
are called "ports", which is easy enough to understand. So are  
sockets and ports the same? Not exactly as you can open many sockets  
to a single port (Many wires to one hole). But from a Rev  
perspective, they're pretty similar.

Unfortunately, when you want to connect to the runrev computer, you  
can't just connect to any old port (numbered hole) on the runrev  
computer. Many of the ports have specific functions, and you have to  
connect to the right one. For example, if there is a web server   
running on the runrev computer, it is most likely "linked" to port  
(hole) number 80, as that's the default for web servers.

So when you do something like this:

   get url "http://www.runrev.com/index.html"

libUrl assumes that the htpp server is running on port 80, and so  
does the following as part of its stuff:

open socket to "www.runrev.com:80"

(You *must* include the port number when you open a socket, which is  
why the example earlier was wrong.)

OK. So once you open a socket, what do you do? Well, you write data  
to the socket, and read data from it. (That's about all you can do.)  
If you're connecting to a "standard" server such as a web server, mail  
server, or ftp server, there are "protocols" which dictate exactly  
what you have to write and read. These protocols are described in  
documents called RFCs which are no fun to read but will help send you  
to sleep when you're on a long flight. (Threaten the kids with one  
for Christmas!)

So rather than wrestling with an established protocol, it's probably  
easier to learn sockets by inventing a brand new protocol. (Really.)  
But to do this, you have to build the server part of the connection  
too. However, this is amazingly easy. The key syntax to running a  
server, is the following script:

   accept connections on port <port number> with message <some handler>

But if you want to see how simple this can be to get started, you can  
try running the following "client" script. I have a Rev "server"  
running here right now  that implements a simple protocol I invented  
while writing this mail. :-)

To run the client, you need to make a stack with two fields and one  
button. Name the fields "in" and "out". Put some text in field  
"in" (keep it below 10K to save my bandwidth). Put the following  
script in the button:

on mouseUp
   put "193.109.51.205:8081" into tS
   open socket to tS with message "connected"
end mouseUp

on connected pS
   put field "in" into tData
##uncomment following line to see server script
## write "script" & cr to socket pS
   write length(tData) & cr to socket pS

   write tData to socket pS with message "readLineOne"
end connected

on readLineOne pS
   read from socket pS for one line with message "readmore"
end readLineOne

on readmore pS, pData
   if word 1 of pData is a integer then
     repeat until length(tRD) >= word 1 of pData
       read from socket pS for word 1 of pData
       put it after tRD
     end repeat
     put tRD into field "out"
   else
     put "an error occurred" into field "out"
   end if
   close socket pS
end readmore


It should return in field "out" a frequency count (number of times  
each word appears) of the text in field "in". (A kind of web service.)

If you want to see the server script instead of the frequency count,  
uncomment the line in the script above.

I'll try and keep the server running for a couple of days (barring  
power cuts).

Cheers
Dave