将C ++ Linux行缓冲套接字方法转换为Windows [英] Converting C++ linux line buffered socket method to windows

查看:55
本文介绍了将C ++ Linux行缓冲套接字方法转换为Windows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我(仍在)将一些Linux代码转换为在Windows上运行.我剩下的一点要整理的内容是将套接字上接收到的所有内容放入缓冲区中,一次只处理一行,而不是一次获取几行,然后必须将它们分开以便分别处理.我不确定我是否已经足够清楚地解释了这一点,但我希望任何了解这些内容的人都能知道我的意思.

在linux代码中:

Hi, I am (still) working on converting some linux code to run on windows. My one remaining bit to sort involves putting everything received on the socket into a buffer and just getting a line out at a time to process, rather than getting several lines at once and then having to seperate them in order to process them seperately. I''m not sure if I''ve explained that clearly enough but I am hoping that anyone who know about this stuff will know what I''m on about.

In the linux code:

// create the stream
FILE* m_Rx = fdopen(clientSocket, "r");
// set stream to line buffered mode.
setlinebuf( m_Rx );



然后在使用select()触发套接字上何时接收到某些内容之后:



Then after using select() to trigger when there is something received on the socket:

fgets( m_RxMsgBuf, RX_MSGBUF_LEN, m_Rx )


然后m_RxMsgBuf仅包含来自套接字接收到的内容的一行,因此一次只能处理一行.

请有人可以帮助我找到在Windows中执行此操作的最简单方法. _fdopen()不能与套接字一起使用,因此我不知道如何将套接字与流链接.我也猜想我需要使用setvbuf()而不是setlinebuf()来将流设置为行缓冲模式.

如果失败,我将尝试编写一种在套接字上调用recv()而不用打扰流的方法,然后一次从套接字的缓冲区中抓取一行.

非常感谢您的帮助.


and then m_RxMsgBuf contains just one line from whatever is received on the socket so that just one line can be processed at a time.

Please could somebody help me find the simplest way to do this in windows. _fdopen() cannot be used with sockets, so I don''t know how to link a socket with a stream. I also guess I would need to use setvbuf() instead of setlinebuf() in order to set the stream to line buffered mode.

Failing this, I will just try to write a method to call recv() on the socket without bothering with a stream, and then just grab a line from the socket''s buffer at a time.

Many thanks for any help.

推荐答案

对,Windows没有为套接字提供文件I/O样式的接口.
您需要自己解析这些字符串.

如何执行,将取决于您如何处理字符串.

一种方法是使用strchr()遍历字符串以查找"\ n"字符并从这些位置提取子字符串.

如果您使用的是CString或std :: string,则这些类具有可用于执行此操作的find()函数.
Right, Windows doesn''t provide a File I/O style interface for sockets.

You''ll need to parse those strings yourself.

How you do it, will depend on how you''re processing the strings.

One way is to walk the string using strchr() to find the ''\n'' characters and extract the substring from those positions.

If your are using CString or std::string, then those classes have find() functions that can be leveraged to do this.


这篇关于将C ++ Linux行缓冲套接字方法转换为Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆