Newlisp TCP communication skills

Source: Internet
Author: User

How to send byte Arrays

Net-send only accepts string as the parameter, that is, only strings can be sent.

Later I discovered that this is only the API interface, and binary data can still be sent through strings, for example:

> (net-send socket "\019\000")2

You only need to add the \ escape symbol in front of it.

How to send 16-bit integers and send them in the big-Endian order

The answer is pack. See the following example:

> (set 'socket (net-connect "localhost" 8889))5> (set 'size (pack ">u" 19))"\000\019"> (net-send socket size)2

"> U" specifies that this is an unsigned short and is in the big-Endian byte order. 19 will be converted to "\ 000 \ 019"

Note that the low address here starts from the left, that is, the high byte 0 of the integer 19 is located on the lowest address, so it is indeed big-Endian.

How to obtain the bytes at the specified position in the received binary data

The answer is address and +. There are two auxiliary functions below:

(define (pick-byte buffer offset)  (get-char (+ (address buffer) offset)))(define (pick-char buffer offset)  (char (pick-byte buffer offset)))

How to use it?

  (unless (net-receive socket message 13) (quit-for-error))  (println (pick-byte message 0))  (println (pick-char message 1))

First, 13 bytes are read from the TCP connection and stored in the message symbol. Then, use address to obtain the actual address of the message in the memory, and use + to obtain the memory address of the nth byte. In this case, the byte obtained can be converted to ASCII characters using char.

How to obtain consecutive bytes in the received binary data
The following two functions are used to obtain consecutive bytes and a continuous string. The former returns list, and the latter returns string.

;; extract the bytes from buffer in [start, end) and return it as a list(define (pick-bytes buffer start end)  (let (l '())    (dotimes (x (- end start))     (push (pick-byte buffer (+ start x)) l -1))));; extract the string from buffer in [start, end) and return it as a string(define (pick-string buffer start end)  (let (l "")    (dotimes (x (- end start))     (push (pick-char buffer (+ start x)) l -1))    ))

In my program, take four consecutive characters, which is a hexadecimal literal constant. The following converts it to an integer using an int.

  (unless (net-receive socket message 13) (quit-for-error))  (println (int (append "0x" (pick-string message 1 5))))

It is parsed as "000d". After 0x is added, it becomes "0x000d". After int Function Conversion, it becomes 13.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.