如何创建消息协议
所以我必须创建一个像这样工作的消息协议:
codFunc arg1 arg2...
例如:
0 'hello world'
10 'user' 'password'
现在我连接发送,并使用 string.split 来读取,但出于多种原因,这不是最好的方法。
所以我的问题是,创建协议的最佳方法是什么?我应该使用哪些现有协议?
谢谢。
So I have to create a message protocol that works like this:
codFunc arg1 arg2...
ex:
0 'hello world'
10 'user' 'password'
Right now I concatenate to send, and use string.split
to read, but for several reasons, that is not the best way.
So my question is, what's the best way to create the protocol? What existing protocols should I use?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
shlex 可能很好,split 有引用空格的问题,pickle 不安全。 JSON 很好。
我喜欢用:
https://www .google.com/search?gcx=c&ix=c1&sourceid=chrome&ie=UTF-8&q=bufsock
...使用以 null 结尾的 ASCII 数据或其他内容来锚定协议的某些部分。
请记住,send() 和 receive() 之间并不总是存在一对一的关系。人们很容易对此感到自满,但它可能会导致网络负载下的可靠性问题。
shlex is probably good, split has problems with quoted whitespace, pickle is insecure. JSON is good.
I like to use:
https://www.google.com/search?gcx=c&ix=c1&sourceid=chrome&ie=UTF-8&q=bufsock
...with ASCII data that's null terminated or something, to anchor parts of the protocol.
Bear in mind that there's not always a one to one relationship between send()'s and recv()'s. It's easy to get complacent about this, but it can cause reliability problems under network load.
split 的问题在于,如果您的用户名包含空格,它将被拆分为单独的参数。
将其拆分一次以获得负责的函数编号:
将字符串解析为参数,可能使用
shlex
:
删除周围的引号:
调用您的函数:
What is wrong with split is that if your username contains white-space, it will be splited into separate arguments.
Split it one time to get responsible function number:
Parse string into arguments, maybe with
shlex
:Remove surrounding quotes:
Call your function: