Article 5571 of comp.lang.lisp:
Xref: crabapple.srv.cs.cmu.edu comp.lang.lisp:5571 comp.lang.lisp.franz:145
Path: crabapple.srv.cs.cmu.edu!pt.cs.cmu.edu!rochester!news.bbn.com!usc!wupost!uunet!cme!hines
From: hines@cme.nist.gov ( pdes)
Newsgroups: comp.lang.lisp,comp.lang.lisp.franz
Subject: Foreign loaded C code that tries to connect to a socket
Message-ID: <6651@brew.cme.nist.gov>
Date: 6 Aug 91 15:52:51 GMT
Followup-To: comp.lang.lisp
Organization: National Institute of Standards & Technology, Gaithersburg, MD
Lines: 111


To all brillient Lisp and C hackers...

   First, I'll tell you what I've got and what works.  I have two C
programs that communicate via a socket.  One is a server, the other a
client.  These both work just fine, the socket connects, data can pass
back and forth over the socket, etc.

  The problem is that the client program needs to be run within Lisp;
Allegro Franz Common Lisp Rel. 3.1 (Nov. 1989) to be exact.  I had no
trouble foriegn loading the object file I'm using, and also had no
trouble assigning entry points and such.  The problem is that when I
run the program, it claims to connect just fine to the socket, but the
server program says that nobody had connected to it!  If I then try to
throw data over the socket, the lisp hangs and must be put to death.
Now, the funny thing is, if the server program is NOT executing when
the client (running within lisp) tries to access it, the client comes
back and says there is no server out there to connect to, which
inicates that it is getting far enough to see whether the server is
there or not.  Frankley, I'm baffled.  If anyone could shed some light
on this, I would be most gratefull!

This is the lisp side of things for one of the functions.  It is
supposed to perform the initial socket connection:

(load "test.o")

(ff:defforeign 'db-start :entry-point (ff:convert-to-lang "DBstart")
                         :arguments '(string)
                         :return-type :integer)

(db-start "databasename")  ;;; Connects to server program already running.

This is the C side of it:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/param.h>
#include <errno.h>
#include <sys/time.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/un.h>

#define oops(msg)    { perror(msg); exit(-1); }

#define MY_PORT 9950
#define MY_HOST "sparky"
#define LINBUF_SIZE 10240 
#define SOCKET_SIZE 1024 

#define DBREADY "DBREADY"
#define DBCLOSE "DBCLOSE"
#define SO_DONTLINGER (~SO_LINGER)

struct sockaddr_in saddr;
struct sockaddr_in addr;
struct hostent *hp;
int sock;
int connected = -1;
int i, id,  pid, status;
FILE * icad_stream;
char buffer[LINBUF_SIZE];
char buf[LINBUF_SIZE];
char * outbuf;
char * tbuf;
char * pstring;

long lisp_value();



int DBstart(lisp_string)
char * lisp_string;
{
	char * buffer;

	printf("%s\n", lisp_string);
	fflush(stdout);

        bzero( &saddr, sizeof(saddr) );
        saddr.sin_family = AF_INET;
	hp = gethostbyname(MY_HOST);
	if ( hp == NULL) oops("no such node");
	bcopy(hp->h_addr, &saddr.sin_addr, hp->h_length);
        saddr.sin_port = htons ( MY_PORT );

	sock = socket(AF_INET, SOCK_STREAM, 0);
	printf("Connected to Socket %d\n", sock);
	fflush(stdout);
	if ( sock == -1) oops("cannot connect to socket");
	if ( connect(sock, &saddr, sizeof(saddr)) != 0 )
	    {
	    printf("GPPE PDES Server May Not be Running on Node \"%s\"\n", MY_HOST);
	    fflush(stdout);
	    oops("Socket Connect Failed");
	    }
        icad_stream = fdopen(sock, "r+");
	write_socket(sock, buffer);
        fflush (icad_stream);
	connected = 1;
	return connected;
}



								Lynwood Hines
								Hines@cme.nist.gov

