pack all fields into one string buffer
Posted: Thu Dec 05, 2013 3:14 am
I want to send a message via TCP to remote server. This message consists of a few fields, some fields are sting, some are number. My old way is to pack one field and then send it. e.g.
But I want to pack all fields in one string buffer and send them one time. How to do this?
Code: Select all
(define (send-message type content)
;; send SOH
(unless (net-send (self 4) SOH) (throw-error (string "send soh failed: " (net-error))))
;; send length
(unless (net-send (self 4) (hex-str (+ 10 (length content)) 4)) (throw-error (string "send length failed: " (net-error))))
;; send type
(unless (net-send (self 4) type) (throw-error (string "send type failed: " (net-error))))
;; send content
(unless (net-send (self 4) content) (throw-error (string "send content failed: " (net-error))))
;; send checksum
(let (checksum-value (cal-checksum (total2 (append type content))))
(unless (net-send (self 4) (hex-str checksum-value 2)) (throw-error (string "send checksumfailed: " (net-error)))))
;; send ETX
(unless (net-send (self 4) ETX) (throw-error (string "send etx failed: " (net-error)))))