Windows Sockets 发送字符串流

发布于 2024-12-20 02:15:00 字数 321 浏览 1 评论 0原文

我正在尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃颗糖壮壮胆 2024-12-27 02:15:00

stringstream 提供了检索 const char* 的函数,请参阅 文档

std::stringstream ss( ... );

ss.str().c_str(); // now the const char *

send(Socket,ss.str().c_str(),ss.str().length(),0);

stringstream provides functions to retrieve a const char*, see the documetation:

std::stringstream ss( ... );

ss.str().c_str(); // now the const char *

send(Socket,ss.str().c_str(),ss.str().length(),0);
死开点丶别碍眼 2024-12-27 02:15:00

套接字 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文