This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Show posts Menu
(get-url "http://www.artfulcode.net")
; The data buffer
(setf data nil)
; This is called when the socket is read to read and reads into the global buffer, data.
(define (reader socket event reactor)
(if (zero? (net-peek socket))
(begin
(setf data (Reactor:get-event-storage event))
(Reactor:kill-event event)
(net-close socket))
(local (buf)
(net-receive socket buf (net-peek socket))
(Reactor:write-event-buffer event buf))))
; Write the request to the socket. Then, remove the write event and add a read event.
(define (writer socket event reactor)
(net-send socket "GET / HTTP/1.1rn")
(net-send socket "Host: www.artfulcode.netrn")
(net-send socket "Connection: closernrn")
(Reactor:kill-event event)
(Reactor:make-event reactor socket reader nil nil))
; Open a connection to the server
(setf socket (net-connect "www.artfulcode.net" 80))
; Create a reactor
(setf reactor (Reactor:make-reactor))
; Create an event handler that calls 'writer' when the socket is ready to be written to
(setf event (Reactor:make-event reactor socket nil writer nil))
; Run the reactor
(Reactor:run-reactor reactor)
; The reactor runs until it is told to stop in one of the callbacks, and then this code is run.
(println "Received " (length data) " bytes")
(context 'my-macro)
(define-macro (my-macro ...)
...)
(context 'previous-context)
(constant 'libmysqlclient "/usr/local/mysql/lib/libmysqlclient.dylib")
(import libmysqlclient "mysql_init")
(import libmysqlclient "mysql_real_connect")
(import libmysqlclient "mysql_real_query")
(import libmysqlclient "mysql_store_result")
(import libmysqlclient "mysql_free_result")
(import libmysqlclient "mysql_close")
(import libmysqlclient "mysql_num_rows")
(import libmysqlclient "mysql_field_count")
(setf sql "select * from some_table_with_lots_of_records")
(setf MYSQL (mysql_init 0))
(println "Connecting: " (mysql_real_connect MYSQL "localhost" "user" "secret" "db" 0 0 0))
(println "Querying: " (mysql_real_query MYSQL sql (+ 1 (length sql))))
(println "Storing: " (setf MYSQL_RES (mysql_store_result MYSQL)))
;; !!! This should return a number in the thousands, but always returns 0.
(println "Rows found: " (mysql_num_rows MYSQL_RES))
(println "Freeing: " (mysql_free_result MYSQL_RES))
(println "Cleaning up: " (mysql_close MYSQL))
(exit 0)
(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))))
; 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
(setf (Foo "bar") 123)