Boost,通过网络使用tcp发送文件,首选方法? [英] Boost, sending files over the network using tcp, prefered method?

查看:308
本文介绍了Boost,通过网络使用tcp发送文件,首选方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文档中的boost示例中,tcp:iostream用于非常简单地通过网络发送流。在其他示例中,write()用于向套接字写入数据,而更多的代码涉及。

In the boost examples in the documentation, tcp:iostream is used to very simply send streams over the network. In other examples write() is used to write data to the socket instead with a bit more code involved.

这两种方法有什么区别?利弊?是否还有别的东西应该使用?

What is the difference between those 2 methods? Pros and cons? Is there something else that should be used instead ?

推荐答案

我从来没有使用过boost API, ;)

I've never used the boost API, so reader beware... ;)

tcp :: iostream似乎允许你使用流式接口与套接字进行交互。这种方法抽象了与套接字编程相关的复杂性,所以最好是特别是如果你是新的套接字编程。这对于基于TCP的数据共享有很大的意义。如果您进行非常简单的数据交换,例如请求/响应,这是特别方便。

The tcp::iostream appears to allow you to interact with the socket with a stream-like interface. This approach abstracts the complexities associated with socket programming, so it would be preferable especially if you are new to socket programming. It makes a lot of sense for TCP-based data sharing. It is especially convenient if you are doing very simple data exchanges, such as request/response.

但是,有些情况下,您需要对数据交换进行较低级别的控制。如果您的接收者同时收到一堆消息,您可能更喜欢从套接字读取每个消息,而不是在iostream的事实之后处理它们。在发件人方面,如果您的消息被结构化为对象,通常更容易发送对象,而不是首先将对象转换为流。在这种情况下,读/写功能将是更可取的。

However, there are cases where you need lower-level control over the data exchange. If your receiver receives a bunch of messages at the same time, you may prefer to read each message from the socket instead of processing them after the fact from the iostream. On the sender side, if your messages are structured as objects, it is often easier just to send the object instead of first converting the object to a stream. The read/write functionality would be preferable in this case.

从我自己的(非boost)套接字编程经验,我通常喜欢处理较低级别的函数,因为它给我更多的灵活性,即使它稍微更复杂。我希望有所帮助。

From my own (non-boost) socket programming experience, I usually prefer dealing with the lower-level functions since it gives me more flexibility even though it is slightly more complex. I hope that helps.

这篇关于Boost,通过网络使用tcp发送文件,首选方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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