usockets:打开套接字时如何指定外部格式 [英] usockets: How do I specify the external format when I open a socket
问题描述
我正在尝试连接到Mud客户端,因此我正在使用usockets通过TCP连接。但是写完之后,我得到了解码错误的阅读。我有理由相信编码应该是ascii,或者至少使用:clrf作为行尾指示符,因为在我读到的行中,在行尾之前有一个^ M
I'm trying to connect to a mud client, so I'm using usockets to connect over tcp. But After I write I get a decoding error reading. I have reason to believe the encoding should ascii ,or least use :clrf as the end of line designator, as on the lines I read there is a ^M before the end of line
(let* ((sock (socket-connect "angalon.net" 3011))
(stream (slot-value sock 'stream)))
(format stream "guest~%")
(force-output stream)
(dotimes (i 40)
(read-line stream))
stream)
:UTF-8 stream decoding error on
#<SB-SYS:FD-STREAM
for "socket 192.168.1.39:65516, peer: 93.174.104.58:3011"
{1004129903}>:
the octet sequence #(255 251 1 80) cannot be decoded.
[Condition of type SB-INT:STREAM-DECODING-ERROR]
我可以验证流的外部格式确实是:utf-8,但问题是我如何指定套接字给我的流的外部格式?
I can verify the external format of the stream is indeed :utf-8, but the question is how I specify the external-format of the stream the socket gives me?
(let* ((sock (socket-connect "angalon.net" 3011))
(stream (slot-value sock 'stream)))
(stream-external-format stream))
;; => :UTF-8
推荐答案
只需查看源在Clozure CL后端,外部格式被硬编码为 ccl:* default-external-format *
,在我的系统中是UTF-8。 SBCL后端未指定外部格式,但可能会使用SBCL默认值(同样是UTF-8)创建套接字。我不认为没有一种简便的方法可以更改外部格式,而无需修改usocket。
Just by looking at the source for the Clozure CL backend, the external format is hard coded to ccl:*default-external-format*
which is UTF-8 in my system. The SBCL backend does not specify an external format, but it probably creates the socket with SBCL defaults, which is again UTF-8. I don't think there's a portable way to change the external format short of modifying usocket.
也就是说,您可以绑定 sb-impl: :*默认外部格式*
在调用 socket-connect $ c $之前说
:latin-1
c>:
That said, you could bind sb-impl::*default-external-format*
to say :latin-1
before calling socket-connect
:
(let* ((sb-impl::*default-external-format* :latin-1)
(sock (socket-connect "angalon.net" 3011))
(stream (slot-value sock 'stream)))
(stream-external-format stream))
;; :LATIN-1
编辑:也可以看看 FLEXI-STREAMS 。我尚未对其进行测试,但是您可以将流转换为 FLEXI-STREAM
并指定外部格式。
also take a look at FLEXI-STREAMS. I haven't tested it, but you could convert the stream to a FLEXI-STREAM
and specify an external format.
这篇关于usockets:打开套接字时如何指定外部格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!