Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sockets in lisp

14 views
Skip to first unread message

Jorge Tavares

unread,
Nov 3, 1999, 3:00:00 AM11/3/99
to

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 =-
-= ja...@student.dei.uc.pt =- -= 0936 6669824 =-
-= CT2HFF=- -= http://student.dei.uc.pt/~jast =-


Erik Naggum

unread,
Nov 3, 1999, 3:00:00 AM11/3/99
to
* 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

Pierre R. Mai

unread,
Nov 3, 1999, 3:00:00 AM11/3/99
to
Jorge Tavares <ja...@student.dei.uc.pt> writes:

> 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.

--
Pierre Mai <pm...@acm.org> PGP and GPG keys at your nearest Keyserver
"One smaller motivation which, in part, stems from altruism is Microsoft-
bashing." [Microsoft memo, see http://www.opensource.org/halloween1.html]

Marc Cavazza

unread,
Nov 3, 1999, 3:00:00 AM11/3/99
to
Jorge Tavares wrote:

> 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


Jorge Tavares

unread,
Nov 3, 1999, 3:00:00 AM11/3/99
to

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 !

0 new messages