From: Michael Halfmann
Subject: connection to lisp via tcp
Date: 
Message-ID: <2843d5ad.0408121214.69c69af3@posting.google.com>
Hi,
i wrote a little program to communicate with Lisp via TCP. The aim is
to connect from a Java program, send some commands to Lisp (as
Strings) and get back the evaluated result from Lisp.
This is what the code looks like:

;;-----------------------------------------------------------------------
(defvar *localport* 2824)
(defparameter *so* nil)
(defparameter *stream* nil)


(defun open-tcp-connection ()
  (format t "~%open connection called...~%")
  (setq *so* 
        (socket:make-socket
         :type        :stream
         :local-port  *localport*
         :connect     :passive
         :format      :bivalent))
  (format t "~% Accepting connections on port ~a under IP ~a which is
hostname ~a and dotted ~a~%"
    (socket:local-port *so*) (socket:local-host *so*)
(socket:ipaddr-to-hostname (socket:local-host *so*))
(socket:ipaddr-to-dotted (socket:local-host *so*)))
  (setq *stream*
        (socket:accept-connection *so*))
  (unwind-protect
      (listen-to-connection)
    (close-tcp-connection)))


(defun listen-to-connection ()
  (format t "~%listen to connection called...~%")
  (format *stream* "~s~%"
    (eval 
     (read *stream*)))
  (force-output *stream*)
  (format t "~%evaluated something...~%")
  (if *so*
      (unwind-protect
          (listen-to-connection)) 
        (format t "~%  ----- BYE BYE ----- ~%")
      ))


(defun close-tcp-connection ()
  (format t "~%close connection called...~%")
  (if *so*
      (progn 
        (close *so*)
        (setq *so* nil)
        (format t "~%connection closed.~%"))
    (format t "~% Can't find open connection~%")))
;;-----------------------------------------------------------------------

This works for some commands but after a little while i get the
following error message:

Error: "Connection reset by peer" (errno 10054) occured while reading
buffer for #<MULTIVALENT stream socket connected from MeHaniker/2824
to MeHaniker/4392 @ #x215cd8f2>.
[condition type: SOCKET-ERROR]

and the program crashes. I don't really know what that error means or
why it occurs. It only happens when i have evaluated some stuff before
and then stopped using the connection for a while (without
disconnect).

I've just started to learn Lisp so chances are quite high that i just
made a really dumb mistake somewhere.
I'd be really happy if someone could help me...
Thanks
MH
From: Espen Vestre
Subject: Re: connection to lisp via tcp
Date: 
Message-ID: <kw3c2rs9oi.fsf@merced.netfonds.no>
···············@web.de (Michael Halfmann) writes:

> This works for some commands but after a little while i get the
> following error message:
>
> Error: "Connection reset by peer" (errno 10054) occured while reading
> buffer for #<MULTIVALENT stream socket connected from MeHaniker/2824
> to MeHaniker/4392 @ #x215cd8f2>.
> [condition type: SOCKET-ERROR]

When you do network communications, connections _will_ drop and you
will get all kinds of weird errors. You may want to treat different
errors in different ways, but the _least_ you need is to wrap up
the body of listen to connection with something like:

(handler-case <your body here>
  (error (cond) (format t "~&Whoops, unexpected error: ~a~%" cond)))
-- 
  (espen)