Sun Jan 14 19:53:05 EST 2001

Plumbing for Dummies

This collects in one place a bunch of code that has been accumulating over the years for connecting various components of programs, along with some minimal explanations, notes, how-to's, and caveats. Languages include C, Perl, Java, Tcl/Tk, and Visual Basic.

Process execution

  • system()

  • exec.java
    This exercises the Runtime.exec function, which starts another process, analogous to system() in C. It reads a line at a time from the standard input and sends it to exec, printing the result.

  • fork, exec, wait, status.

    Pipes

    These are illustrated in the inetdsrv code below.

  • popen()
  • 2-way ? deadlock
  • select
  • pty
  • producer-consumer prod.c, cons.c

    Sockets

  • cli.c, srv.c
    These C client and server programs are adapted from Comer's TCP/IP survey. They are the bottom level of the standard Unix TCP/IP code that appears in a typical program that talks to the net.

    The server has to be started first; it listens on port 5194 for the client. The client reads a line from stdin, sends it to the server, reads one line back, and prints it. The server reads a line at a time as well, echoing it with a count so the effect of multiple clients can be seen. The server quits if it reads a line that says "exit".

  • inetdsrv0.c, inetdsrv.c
    These implement a server that starts a new child process for each incoming line, so in effect they do what inetd does from the system level. These were originally done for an ampl experiment, so they recognize a small set of things related to using ampl. Inetdsrv0 simply starts ampl and forwards input and output; inetdsrv handles several other commands, like cd, ls, echo, and so on as well as ampl, though once ampl has been started, there is no return. This one in particular represents an enormous security hole, so it can't be turned on blindly.

    The plumbing here is as complicated as it gets: the server forks a child to service the incoming connection. That child in turn forks a child of its own that becomes ampl via an exec. The (child) server and ampl communicate through two pipes that are established after the (second) fork but before the exec.

  • cli.java, srv.java, srv2.java
    These are Java versions of client and server, almost identical to the cli.c and srv.c above: the client read a stdin line, sends it to the server (which echos it), and prints it.

    Srv2.java is a threaded version of the server that starts a separate thread for each incoming client, so it can be carrying on conversations with several clients at the same time; srv.java must finish one conversation before it can begin another.

  • geturl.pl, quote.pl
    This is the Perl version of client-side socket code to send a request to a server and read the entire result. The quote server does a specific case, fetching stock prices from their ticker symbols (thanks to yahoo). Try "quote.pl lu".

  • nslookup.pl
    Calling nslookup from Perl; this illustrates some of the other properties of the socket module.

  • quote.tcl
    The quote server in Tcl/Tk just for completeness. It's about the simplest of the bunch. It would be easy to make a more general client, and making a server is no more complicated than adding "-server" to the socket command.

    CGI and Forms

  • basic telnet GET
    It's easy to poke at a web server with Telnet, at least to see what it's sending back. At the Unix or DOS prompt, try
    	telnet www.yahoo.com 80
    	GET /index.html HTTP/1.0
    	(extra blank line)
    

  • url1.java
    This is analogous, but puts the result in a TextArea for examination. Enter the URL as http://www.yahoo.com in the TextField at the top. This would be simpler as a pure text program without the interface.

  • geturl.pl
    This is the same thing in Perl, as above.

  • echo, etc.

  • survey.html, surv1.pl, survey.pl
    The survey is a form with several differnt kinds of fields to fill in. The first Perl program creates an HTML page that displays the values that were received; it uses the Perl CGI module.

    The second Perl program also mails them to me.

  • cookie.pl, post.html
    This shows retrieving and setting a cookie, along with everything else. This has the cleanest version of Perl for parsing and unescaping URL-encoded material.

    The submission page is a simple form that says

    <FORM>
    ACTION="http://campuscgi.princeton.edu/~bwk/surv1.cgi" METHOD=POST> 
    <input name="foo">
    <INPUT TYPE="submit" VALUE="Send" >
    </FORM> 
    

    Active Web Pages

  • javascript.txt
    Javascript

  • appcli.java
    A simple Java applet that is a client for one of the echo servers. This uses AWT; Swing would be slightly different. Check out Jwise.java.

  • VBScript, JScript
  • plug-ins, style sheets, Mozilla, etc., etc.

    DLL's

  • calling a DLL from C or C++
  • calling a DLL from Visual Basic
  • compiling a DLL

    COM

  • creating a COM object
  • accessing a COM object from C or C++
  • accessing a COM object from VB
  • accessing a COM object from Word, Excel, etc.