使用boost asio指定ip和port创建iostream [英] Create an iostream using boost asio specifying ip and port

查看:514
本文介绍了使用boost asio指定ip和port创建iostream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于boost asio库的问题。我成功地尝试在客户端和服务器之间创建一个套接字,这涉及创建解析器,以便指定到服务器的ip和端口(服务器只需要端口)和其他对象,但最重要的是,有必要使用写入 read_some 作为从/从套接字读取和写入的函数。
我真的很感激使用流,这是可能的boost asio,但这是奇怪的...
在几乎所有的例子使用流,创建服务器,有必要提供端口,确定,让我们来谈谈客户端...客户端,有必要使用iostream构造函数指定连接流的坐标,这里是代码:

I have a problem concerning boost asio libraries. I successfully tried to create a socket between a client and a server, this involves creation of resolvers in order to specify ip and port to the server (the server only requires port) and other objects, but, most importantly, it is necessary to use write and read_some as functions to read and write from/in the socket. I would really appreciate to use a stream, and this is possible in boost asio, but that's strange... In almost all examples using streams, to create a server it is necessary to provide port, ok, let's talk about the client... client side, it is necessary to use the iostream constructor to specify coordinates for connecting the stream, here's the code:

tcp::iostream() s(argv[1], "daytime");

好吧,我真的不明白第一个参数传递什么,真的不知道基本上,这里,我告诉:嘿流,你必须连接到这个服务器...,但如何指定该服务器的ip和端口?
注意,相反,一切都几乎是清楚的服务器端:

Well, I don't really understand what is passed in the first parameter and really don't know what daytime might ever represent... Basically, here, I'm telling: "Hey stream, you must connect to this server..." but how can I specify ip and port of that server? Note that, on the opposite, everything is almost clear server side:

boost::asio::io_service io_s;
tcp::acceptor acc(io_s, tcp::endpoint(tcp::v4(), 1950));
for (;;) {
   tcp::iostream stream;
   acc.accept(*stream.rdbuf());
   stream << "Message" << std::endl;
}

使用这个模型,我想使用

Using this model, I would like to use

stream << mymessage_to_send << std::endl;
stream >> a_string_containing_my_message;

以发送和接收。
我如何做到这一点?
非常感谢。

in order to send and receive. How can I do this? Thank you very much.

推荐答案

我写了一个客户端/服务器系统使用 Boost.Asio 。来源可在 Google代码 Client.cpp Server.cpp 。使用 Boost.Serialization 与Boost.Asio一起允许我发送任意数据结构。我必须说,这是相当令人印象深刻!

I've written a client/server system using Boost.Asio. The source is available on Google Code: Client.cpp and Server.cpp. Using Boost.Serialization together with Boost.Asio allows me to send arbitrary datastructures over the wire. I must say it is quite impressive!

这篇关于使用boost asio指定ip和port创建iostream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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