Windows Sockets 发送字符串流
我正在尝试通过 TCP 套接字连接发送字符串流。问题是 Windows 套接字只接受 const char 数据数组。使用套接字发送字符串流数据类型的最佳和最有效的方法是什么?
char *szMessage="Hello World";
send(Socket,szMessage,strlen(szMessage),0);
如果 Windows 本机套接字库不足以完成这项工作,我愿意学习不同的套接字库。另外,我也刚刚开始使用 Boost C++ 库进行线程处理,它们的套接字库看起来更先进一些,但我不确定它是否支持发送字符串流。
提前致谢
I’m trying to send a stringstream through a TCP socket connection. The problem is that windows sockets only takes a const char data array. What would be the best and most efficient way to send a stringstream data type using sockets?
char *szMessage="Hello World";
send(Socket,szMessage,strlen(szMessage),0);
I’m willing to learn a different sockets library if windows native sockets library is not sufficient for this job. Also I’ve also just started using Boost C++ libraries for threading and their sockets library looks a little more advanced but I’m unsure if it supports sending stringstream’s.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
stringstream
提供了检索const char*
的函数,请参阅 文档:stringstream
provides functions to retrieve aconst char*
, see the documetation:套接字 API 的目的是发送字节。 C++ 中的字节数据类型是
char
。要通过套接字发送更复杂的数据类型,您需要一个协议。 Windows 提供(DCE)RPC。许多人使用 SOAP。还有很多其他的。
stringsrteam
包含字符。如果您控制连接的两端,则字符和字节之间的差异对您来说可能并不重要。如果不这样做,这可能非常重要,因为除非您小心确保两端都使用相同的字符编码,否则两端可能在解释上存在分歧。The purpose of a socket API is to send bytes. The byte datatype in C++ is
char
.To send a more complex datatype across a socket, you need a protocol. Windows provides (DCE) RPC. Many people use SOAP. There are lots of others.
A
stringsrteam
contains characters. If you control both ends of the connection, the difference between characters and bytes may not be important to you. If you do not, it can be very important, since the two ends can disagree on the interpretation unless you are careful to make sure that both use the same character encoding.