Cwinsock“滚动解析”
我正在尝试从服务器接收数据并解析它。
http://pastebin.com/1kjXnXwq http://pastebin.com/XpGSgRBh
一切都按原样工作,但我想解析数据,而不是仅仅抓取数据块并将其打印出来。那么有没有办法从winsock中获取数据,直到\n然后停止并将其传递给另一个要解析的函数,一旦该函数返回,继续从最后一个点读取,直到另一个\n出现并重复该过程,直到出现没有什么可以接收的吗?
应该执行此操作的函数称为 msgLoop(),位于第二个 Pastebin 行中。
i'm trying to receive data from a server and parse it.
http://pastebin.com/1kjXnXwq
http://pastebin.com/XpGSgRBh
everything works as is, but i want to parse the data instead of just grabbing blocks of it and printing it out. so is there any way to grab data from the winsock until \n then stop and pass it off to another function to be parsed and once that function returns continue reading from the last point until another \n shows up and repeate the process until there is nothing left to receive?
the function that is supposed to be doing this is called msgLoop() and is located in the second pastebin line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要从套接字读取以
\n
结尾的字符串,您必须:从套接字一次读取 1 个字节,直到遇到
\n
字节。任何未读的字节都会保留在套接字中,直到您稍后读取它们。这不是很高效,但它可以工作。创建数据缓存。当您需要一个新字符串时,首先检查缓存以查看其中是否已经存在
\n
字节。如果没有,则继续从套接字读取更大的块并将它们存储到缓存中,直到遇到\n
字节。然后处理缓存的内容直至第一个\n
字节,删除已处理的字节,并将所有剩余字节移动到缓存的前面以供以后读取。To read an
\n
-terminated string from a socket, you have to either:read from the socket 1 byte at a time until you encounter a
\n
byte. Any unread bytes are left in the socket until you read them later. This is not very efficient, but it works.create a data cache. When you need a new string, first check the cache to see if there is already a
\n
byte present in it. If not, then keep reading from the socket in larger blocks and store them into the cache until you encounter a\n
byte. Then process the contents of the cache up to the first\n
byte, remove the bytes you processed, and move any remaining bytes to the front of the cache for later reads.套接字没有内置的“readLine”方法。因此,您需要自己实现它,但这并不太棘手。我通过谷歌搜索找到了这个例子,你也许可以改进它:
http://johnnie.jerrata.com/温索克教程/
There's no built-in "readLine" method for sockets. So, you'll need to implement it yourself, but it's not too tricky. I found this example by Googling, you may be able to improve on it:
http://johnnie.jerrata.com/winsocktutorial/