Return-Path: <@glinda.oz.cs.cmu.edu:stark%saturn@sdsu.edu>
Received: from glinda.oz.cs.cmu.edu by A.GP.CS.CMU.EDU id aa00562;
          28 May 92 21:18:14 EDT
Received: from sdsu.edu by GLINDA.OZ.CS.CMU.EDU id aa15536;
          28 May 92 21:17:24 EDT
Received: from saturn.sdsu.edu by sdsu.edu; id AA02194
	sendmail 5.64/SDSU-GW-1.11 via SMTP
	Thu, 28 May 92 18:17:19 -0700 for mkant@glinda.oz.cs.cmu.edu
Received: by saturn.SDSU.EDU (4.1/SDSU-CLIENT)
	id AA23565 for delivery to mkant@glinda.oz.cs.cmu.edu; Thu, 28 May 92 18:17:18 PDT
Date: Thu, 28 May 92 18:17:18 PDT
From: "Brian D. Stark" <stark%saturn@sdsu.edu>
Message-Id: <9205290117.AA23565@saturn.SDSU.EDU>
To: mkant@GLINDA.OZ.CS.CMU.EDU
Subject: Our solution

This is what we did using AKCL, the code was written by Vince Baker.


Brian, 
	Here are the functions I used for the socket communications in lisp.
The lisp function "Clines" copies the C code to the intermediate C source code
file created by the lisp compiler. The compiler then creates an object file
that can be loaded into the interactive session of lisp. I do this with
a file called 'init.lsp' which is automatically loaded when akcl is invoked.
I've included this here also.

/* file: socket.lsp                                                         */

(Clines "
#include <stdio.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#define MAX_CHAR 80

/* The function sockinit connects to the socket on the machine specified by */
/* the argument 'host', which must be a string. Upon successful completion, */
/* it returns a file descriptor for the socket.                             */

int sockinit(host)  /* initialize socket to remote machine */ 
   object host;
   {
   struct sockaddr_in saddr;
   struct hostent *hp, *gethostbyname();
   int fd;

   /* init socket address structure */
   bzero((char *) &saddr, sizeof(saddr));
   saddr.sin_family = AF_INET;
   hp = gethostbyname(host->st.st_self);
   if(hp == NULL) {
      fprintf(stderr, \"bad host name\n\");
      return(-1);
      }
   bcopy(hp->h_addr, &saddr.sin_addr, hp->h_length);
   saddr.sin_port = 5888;

   /* create socket connection */
   fd = socket(AF_INET, SOCK_STREAM, 0);
   if(fd == -1) {
      fprintf(stderr, \"cannot open socket\n\");
      return(-2);
      }

   /* connect to remote machine */
   if(connect(fd, &saddr, sizeof(saddr)) != 0) {
      fprintf(stderr, \"cannot connect\n\");
      return(-3);
      }

   return(fd);
   }
")

/* The funtion rsend sends a string to the remote machine using the argument */
/* 'fd' for the socket file descriptor. It returns an integer that is sent   */
/* from the host machine. Note that for R-Shell communications, the command  */
/* string must end in a newline character.                                   */

(Clines "
int rsend(command, fd)
   object command;
   int fd;
   {
   char buffer[MAX_CHAR];
   int status;

   sprintf(buffer, \"%s\\n\", command->st.st_self);
   write(fd, buffer, (command->st.st_dim)+1);
   read(fd, &status, sizeof(status));
   return(status);
   }
")

/* The function defentry is a lisp function that provides an interface to the */
/* C functions.                                                               */

(defentry sockinit (object) (int sockinit))

(defentry rsend (object int) (int rsend))

/* end of file: socket.lsp                                                    */


	The following functions are included in the 'init.lsp' file that is 
automatically loaded at the start of an akcl session. First the socket.o file
is loaded, then the socket is initialized. The file descriptor 'fd' is used in
the rsend function. The function 'rshell' is used just for convenience so that
the file descriptor doesn't have to explicitly referenced with every command
to R-Shell.

/* file: init.lsp                                                             */

(load "socket")
(setq fd (sockinit "bronco"))
(defun rshell (command) (rsend command fd))

/* end of file: init.lsp                                                      */


	The C functions above are in a file called socket.lsp. This can be
compiled in a session of akcl by the function:

(compile-file "socket")

the object file socket.o is then created, and it is this file that is loaded
in the interpreter. I think there may be a way to compile the file outside
of the interpreter, but I don't know the name of the compiler. 
There you have it,
		V

