Using TCP Sockets with the Icon language |
|
TCP sockets are a standard way of connecting to services on the Internet. I asked Gregg Townsend of the Icon project if it were possible to use sockets without resorting to the cumbersome iconc translator, and here is his reply.
From gmt@cs.arizona.edu Wed Apr 10 16:54:12 1996
Date: Wed, 10 Apr 1996 15:33:00 -0700
From: Gregg Townsend <gmt@cs.arizona.edu>
Message-Id: <9604102233.AA32654@hawk.CS.Arizona.EDU>
To: icon-project@cs.arizona.edu, js@sphinx.cs.nmt.edu
Subject: Re: Sockets?
Status: RO
X-Status:
There's a TCP socket interface in the "cfuncs" section of
the Icon program library. It requires dynamic loading,
which works on Suns, Alphas, and SGI machines. See
http://www.cs.arizona.edu/icon/library/ccfuncs.html for
documentation.
Using it is simple if you've installed Icon 9.1 from the
binary starter kits we distribute. Assuming that $FPATH is
set as indicated by the installation process, an Icon
program just has to say
link cfunc
to be able to call tconnect().
I've written small programs that talk to news servers and
web servers using tconnect(). Below is a very simple telnet
example. It also uses fpoll(), another C function.
There's nothing in place now for writing a socket *server*;
only the function for connecting to an existing socket.
Gregg Townsend / gmt@CS.Arizona.EDU / +1 520 621 4325 / 32 13 45N 110 57 16W
Computer Science / Univ of Arizona / 1040 E 4th St / Tucson AZ 85721-0077
############################################################################
#
# File: tnet.icn
#
# Subject: Program to talk to telnet port
#
# Author: Gregg M. Townsend
#
# Date: August 16, 1994
#
############################################################################
#
# Usage: tnet hostname portnumber
#
# This is a VERY simple telnet client. It connects to a remote port
# and exchanges data between the port and the terminal. The port is
# read and echoed to the terminal until the port is quiet for 200 msec;
# then one line from the terminal is sent to the port. This process
# repeats until an EOF is read from either source.
#
# Some interesting port numbers can usually be found in /etc/services.
# For example, network news is read from a news server using port 119.
#
# This program does not work under Irix because poll(2) always returns 1.
#
############################################################################
link cfunc
procedure main(args)
local h, p, f, s
h := args[1] | &host # default is current host
p := integer(args[2]) | 13 # default is port 13 (time of day)
f := tconnect(h, p) | stop("can't connect to port ", p, " of ", h)
fpoll(f, 2000) # wait up to 2 sec for initial response
repeat {
while fpoll(f, 200) do # read characters from port until timeout
writes(reads(f)) | { write("EOF"); break break }
writes("\n> ") # issue prompt
s := read() | break # read line from terminal
seek(f) # enable switch from input to output
flush(f) # workaround for Dec Alpha bug
write(f, s) # write terminal input to port
seek(f) # enable switch from output to input
}
end