Structural Informatics Group (SIG) logo
Home | Projects | Demos | Downloads | Publications | Local Info | About Us | New site
Go to the first, previous, next, last section, table of contents.

Embedded Network Client

xnet utilizes c functions defined in slisp/c/netseval.c, which are compiled by every application into the library libnetseval.a. If this library is linked with any C program the C program can call the functions in netseval.c to evaluate s-expressions on a remote server. The functions in netseval.c are semantically equivalent to those in seval.c (described in the section on embedding lisp in C), with the addition of functions for establishing a connection.

c/sl_client.c, when compiled with the NET_CLIENT flag, creates a network client called netsl. netsl is created whenever the net_client target is made in an application Makefile. c/client_check.c, when compiled with the NET_CLIENT flag by the check target in each application Makefile, also shows an example of an embedded network client.

Since for a network client all of slisp is located on the server, the only library that needs to be linked into a C program is libnetseval.c, which is shown in the c/Makefile for the target net_client.

A somewhat simpler example is shown below, which is essentially the same as sl_client.c, but without all the makefile flags. This is also very similar to slc, the main program described under embedding lisp in C.

# Example Makefile that searches libraries in HOME/lib
# which is where the slisp libraries are created by default.
# Search application library sl before slisp 
LIBS= -L$HOME/lib  -lnetseval 
CFLAGS= -I$HOME/src/slisp/include #location of netseval.h

netsl:netsl.o
	cc netsl.o $(LIBS) -o netsl

/******************************************************************/
/* netsl.c */

#include "netseval.h"
#include <stdio.h>

#define BUF_SIZ 4000

/* Example main program that uses the functions in netseval.h   */
/* to implement a read-eval-print loop to the terminal. 	*/
/* Functions starting with sn_ are the routines from netseval.c */
/* Note that we don't include                                   */
/* a newline in our prompt, since the slisp return	        */
/* values always end with a newline anyhow, following           */
/* the convention used in other xlisp routines.                 */

main()
{
    char inbuf[ BUF_SIZ ];
    int s;
    sl_Init();
    if (!(argc == 3)) { 
	printf("Usage: %s <hostname> <port number>\n", argv[0]);
	exit(1);
	}
    s = sn_Connect(argv[1], atoi(argv[2]), &got_err, &result, &err);
    if (got_err) {
	if (result)
	   printf("%s", result);
	printf("error: %s", err);
	printf("Can't connect to %s on port %s\n", argv[1], argv[2]);
	exit(1);
	}
    printf("%s", result);
    sn_Set_Socket(s);
    for (;;) {
        int   got_err;
        char* result, *out;
        /* Read string to evaluate from user: */
        printf("> ");
        fgets( inbuf, BUF_SIZ, stdin );
	out = sn_Eval_Str(&got_err, &result, inbuf );
	if (out != result) printf("%s", out);
	if (got_err) {
	  if (!strcmp(result, "Server timeout\n") ||
              !strcmp(result, "Connection not open\n"))  {
	    printf("%s", result);
	    exit(0);
	    }
	  else
	    printf("error: %s", result);
   	  }
	else
	  printf("%s", result);

    }
}


Go to the first, previous, next, last section, table of contents.