sockets in lisp 
Author Message
 sockets in lisp

        Hello all,

        I wanted to try to make a simple client server aplication with
        TCP sockets but I was unable to find information on the subject.
        The books I have don't talk about it and I have tried to find some
        web pages but didn't get anything. I've also tried to look in
        deja news but the serach results didn't give me a clear picture
        of the subject. I was able to realise that there must be something
        like open-tcp-stream and then work like a normal stream but I
        think there is more to the subject. If somebody would be kind
        enough to point me in some direction I would be apreciate.

        Thanks !
        Jorge

-= Jorge Tavares =-

-= CT2HFF=- -= http://www.*-*-*.com/ ~jast =-



Sun, 21 Apr 2002 03:00:00 GMT  
 sockets in lisp
* Jorge Tavares
| I wanted to try to make a simple client server application with TCP
| sockets but I was unable to find information on the subject.

  if you're working with Allegro CL 5.0.1 Lite for Windows, you may want to
  visit www.franz.com and look at their support documentation.  according
  to these files, it includes the Allegro CL socket library.  I'm not sure
  the 5.0 Lite edition did.  if it didn't, grab the new version.  (if you
  use the Personal or Student edition of Allegro CL, these do not contain
  the socket library.)

| The books I have don't talk about it and I have tried to find some web
| pages but didn't get anything.

  there should be documentation coming with your CD or download that
  explains this in detail.  look for doc/cl/socket.htm in your Allegro CL
  installation directory.  it contains all you need to know, including
  descriptions of all the functions involved and links to their pages.

#:Erik



Sun, 21 Apr 2002 03:00:00 GMT  
 sockets in lisp

Quote:

>    Hello all,

>    I wanted to try to make a simple client server aplication with
>    TCP sockets but I was unable to find information on the subject.
>    The books I have don't talk about it and I have tried to find some
>    web pages but didn't get anything. I've also tried to look in
>    deja news but the serach results didn't give me a clear picture
>    of the subject. I was able to realise that there must be something
>    like open-tcp-stream and then work like a normal stream but I
>    think there is more to the subject. If somebody would be kind
>    enough to point me in some direction I would be apreciate.

>    Thanks !
>    Jorge

Access to sockets is not part of the ANSI CL standard, so the details
of this will vary between implementations, although the general
principle (that you describe above) will be similar.  Erik has already
posted hints for ACL, here are some more for other systems:

a) Get cl-http, which would include implementation specific code for
   listening and opening on sockets for nearly all implementations
   (see http://www.ai.mit.edu/projects/iiip/doc/cl-http/home-page.html)

b) For CMUCL, this is the skeleton of code that is needed to listen on
   a socket and spawn functions that handle connections (this uses
   multi-processing, so will only work in this way on iA32):

(defun ip-address-string (address)
  (format nil "~D.~D.~D.~D"
          (ldb (byte 8 24) address)
          (ldb (byte 8 16) address)
          (ldb (byte 8 8)  address)
          (ldb (byte 8 0)  address)))

(defun http-listener (port server)
  (let ((fd (ext:create-inet-listener port)))
    (unless fd (error "Cannot bind port ~D." port))
    (unwind-protect
         (progn
           (setf (process-name *current-process*)
                 (format nil
                         "HTTP connection listener on port ~D with server ~A"
                         port server))
           (format t "~&;;; Started lisp connection listener on ~
                port ~d for server ~A~%" port server)
           (loop
               ;; Wait for new connection
               (process-wait-until-fd-usable fd :input)
               #+CLASH-DEBUG
               (format t "~&;;; At ~D Got Connection...~%"
                       (get-internal-real-time))
               (multiple-value-bind (new-fd remote-host)
                   (ext:accept-tcp-connection fd)
                 #+CLASH-DEBUG
                 (format t "~&;;; At ~D Have Connection...~%"
                         (get-internal-real-time))
                 (let* ((host-entry (ext:lookup-host-entry remote-host))
                        (stream (sys:make-fd-stream new-fd :input t :output t))
                        (connection (make-instance 'simple-connection
                                                   :stream stream)))
                   #+CLASH-DEBUG
                   (format t "~&;;; At ~D Established Connection...~%"
                           (get-internal-real-time))
                   (make-process
                    #'(lambda ()
                        (serve-connection server connection))
                    :name (format nil "HTTP connection from ~A"
                                  (if host-entry
                                      (ext:host-entry-name host-entry)
                                      (ip-address-string remote-host))))))))
      (when fd (unix:unix-close fd)))))

(defun start-http-listener (port server)
  (make-process #'(lambda () (http-listener port server))))

(defun initialize-clash (&optional (idle-process mp::*initial-process*))
  (setf mp::*idle-process* idle-process))

Regs, Pierre.

--

  "One smaller motivation which, in part, stems from altruism is Microsoft-
   bashing." [Microsoft memo, see http://www.opensource.org/halloween1.html]



Sun, 21 Apr 2002 03:00:00 GMT  
 sockets in lisp

Quote:

>         Hello all,

>         I wanted to try to make a simple client server aplication with
>         TCP sockets but I was unable to find information on the subject.
>         The books I have don't talk about it and I have tried to find some
>         web pages but didn't get anything. I've also tried to look in
>         deja news but the serach results didn't give me a clear picture
>         of the subject. I was able to realise that there must be something
>         like open-tcp-stream and then work like a normal stream but I
>         think there is more to the subject. If somebody would be kind
>         enough to point me in some direction I would be apreciate.

Hi,

As Erik said, you should find good examples on Franz's site. You can also
choose to write your client-server application from the basic examples given
in the Allegro documentation on sockets. You'll find below very simple code
excerpts
adapted from their examples (that I used for Lisp/C++ communication):

For a server, you have to create a stream from the socket before using it:

(let ((so (socket:make-socket
      :connect :passive
      :remote-host "123.89.76.54"
      :local-host "foobar"                          ;; don't need both :-)
      :format :text
      :local-port port)))
    ...
        (let ((str-a (gensym)))
           (setq str-a (socket:accept-connection so :wait wait))
   ...
   and then use str-a as a normal stream for I/O

And on the client side you'd have something like:

...
(socket:with-pending-connect
       (mp:with-timeout (timeout (error "Connection failed"))  ;; timeout as
&optional parameter
         (let ((s (socket:make-socket
                      :connect :active
                      :remote-host "123.45.67.89"
                      :remote-port rport)))
;; rport is the port you connect to (was a fn parameter)
...
you can directly use "s" as a stream for I/O in this case

Of course, you close sockets with the "close" function: do not
forget to wrap it with an "unwind-protect". Use of unwind-protect
appears in the more extensive examples on Franz's site

(Clemens Heitzinger also posted a socket example a few days ago, using
Macintosh Common Lisp with "open-tcp-stream")

Hope this helps

Marc



Sun, 21 Apr 2002 03:00:00 GMT  
 sockets in lisp

        Hello !

        I want to thank you all for your support. You gave me plenty of
material to look for and study, it was more than I thought up. Once again,
thanks !

        Jorge

-= Jorge Tavares =-

-= CT2HFF=- -= http://student.dei.uc.pt/~jast =-



Sun, 21 Apr 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Streams & sockets in lisp (query)

2. Socket communication latency with Smalltalk talking to Allegro Lisp

3. Free Linux Lisp and Sockets

4. Does Common Lisp have a socket library?

5. Using sockets in Common LISP

6. Looking for Common Lisp with function of Socket communication

7. Multi-processing and sockets in Allegro Common Lisp

8. lisp sockets ??

9. BSD-Socket under CMU-Lisp

10. lisp sockets

11. LISP i/o with unix sockets

12. Lisp,LIsp,LISp,LISP...

 

 
Powered by phpBB® Forum Software