Sockets module

Started by Jeff, January 30, 2009, 08:31:37 AM

Previous topic - Next topic

Jeff

For those of you sick of manually polling sockets when trying to write a simple server app, I wrote a Sockets module that neatly wraps both client and server operations into a couple of classes and helper functions.  Complete with examples and newlisp10 compatible.



One really neat feature is the ability to pass lisp expressions directly back and forth over a socket.  This works very much like net-eval, but lets you control what the server does with the expression.  This is extremely useful if you want to pass complex data back and forth or create your own multi-processing net-eval using the Cilk API - and, perhaps, my MP module :).



http://static.artfulcode.net/newlisp/sockets.lsp.html">//http://static.artfulcode.net/newlisp/sockets.lsp.html



Running a server

(let ((server (SocketServer "/tmp/newlisp.sock")))
  (:run-server server
    (lambda (client server , request)
      (if client
        (begin
          (setf request (:read-expr client))
          (eval request) ; not safe unless you know your client
          (:write-expr client '(println "Hello, client!"))
          (:close client))
        (println "client connection error: " (net-error))))
    nil ; no operations between connections
    (lambda (err server) (println "An error has occurred: " err))))


Connecting a client
; Connect to server process as a client
(let ((socket (Sockets:make-file-socket "/tmp/newlisp.sock")))
  (:write-expr socket '(println "Hello, server!"))
  (setf expr (:read-expr socket))
  (:close socket))
(eval expr) ; not safe unless you know your server
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code

newdep

#1
Nice work jeff.. Personaly i would take it 1 step further,

dont know if you though about it..



I see all IO's as 1 device, therefor it would be nice if there would not

be a difference in nameing between make-file-socket and make-network-socket.. but just plain  make-socket.



Where make-socket will autodetect if its a socket or a file and the user wont be bothered with it anymore..
-- (define? (Cornflakes))

Lutz

#2
On a UNIX machine you actually can use newLISP's file read/write functions for serving sockets. But the socket functions behave somewhat differently in error conditions, setting 'net-error' and frequently they allow additional parameters, like time-out etc.

Jeff

#3
newdep,



In fact, that was how I originally planned on doing it; the problem is that with local files sockets are extremely reliable, whereas with network sockets you need to deal with things like connection timeouts, outages, lag, etc. One problem complicates other problems and the scale grows.



In common lisp, I would do something like


(let ((read-line #'net-receive)) ...

...in order to shadow built-in functions, but you can do that, since built-ins are constants in newlisp.
Jeff

=====

Old programmers don\'t die. They just parse on...



http://artfulcode.net\">Artful code