简单的服务器/客户端增强示例不起作用
学习boost,并编译了他们的日间服务器客户端例子。 由于我无法使用示例中的端口 13,因此我仅更改了服务器和客户端示例中的端口号。 服务器运行正常,但客户端似乎无法连接,并且没有给出错误。
客户端的输入数据是“127.0.0.1”。
服务器:
#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
std::string make_daytime_string()
{
using namespace std; // For time_t, time and ctime;
time_t now = time(0);
return ctime(&now);
}
int main()
{
try
{
boost::asio::io_service io_service;
tcp::endpoint endpoint(tcp::v4(), 8087);
tcp::acceptor acceptor(io_service, endpoint);
for (;;)
{
tcp::iostream stream;
acceptor.accept(*stream.rdbuf());
stream << "test" << make_daytime_string();
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
和客户端:
#include <iostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: daytime_client <host>" << std::endl;
return 1;
}
tcp::iostream s(argv[1], 8087);
std::string line;
std::getline(s, line);
std::cout << line << std::endl;
}
catch (std::exception& e)
{
std::cout << "Exception: " << e.what() << std::endl;
}
return 0;
}
Learning boost, and compiled their daytime server client example. Since I cant use port 13 that is in the example I only changed the port numbers in the server and client example. Server runs fine, but the client doesnt connect it seems, and no error is given.
Input data for the client is "127.0.0.1".
Server:
#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
std::string make_daytime_string()
{
using namespace std; // For time_t, time and ctime;
time_t now = time(0);
return ctime(&now);
}
int main()
{
try
{
boost::asio::io_service io_service;
tcp::endpoint endpoint(tcp::v4(), 8087);
tcp::acceptor acceptor(io_service, endpoint);
for (;;)
{
tcp::iostream stream;
acceptor.accept(*stream.rdbuf());
stream << "test" << make_daytime_string();
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
And the client:
#include <iostream>
#include <string>
#include <boost/asio.hpp>
using boost::asio::ip::tcp;
int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: daytime_client <host>" << std::endl;
return 1;
}
tcp::iostream s(argv[1], 8087);
std::string line;
std::getline(s, line);
std::cout << line << std::endl;
}
catch (std::exception& e)
{
std::cout << "Exception: " << e.what() << std::endl;
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对我有用的是将创建端点的方式从 更改为
第
一种方法创建 0.0.0.0 的端点,它在 Mac OS X 上工作正常,但在 Windows 上给出“无效”消息(XP,使用 MSVC 2008 构建) )。
我不介意知道为什么会有这种差异,但至少它有效。
What worked for me was to change the way I create the endpoint from
to
The first method creates an endpoint of 0.0.0.0 which works fine on Mac OS X, but gives the "not valid" message on Windows (XP, building with MSVC 2008).
I wouldn't mind knowing WHY the difference, but at least it works.
有几件事可以帮助您调试此问题:
另外,要检查的一件事是服务器是否绑定到 127.0.0.1 或外部界面。 尝试使用外部接口的 IP 地址而不是 127.0.0.1。 在 Windows 中使用 ipconfig 进行检查,在 Linux 中使用 ifconfig 进行检查。
A few things would help to debug this for you:
Also, one thing to check is whether the server is binding to 127.0.0.1 or the external interface. Try using the IP address of your external interface instead of 127.0.0.1. Check this in windows using ipconfig and in linux using ifconfig.
嗯,一切都适用于 1_36 boost 版本和 msvc 2005 编译器。
检查您的防火墙设置。
Hmm, all works on 1_36 boost version and msvc 2005 compiller.
Check your firewall settings.
port 选项接受一个字符串,它可能是服务的名称,如“daytime”,然后它将查找相应的端口,或者显式查找端口,但它必须是一个字符串:
tcp::iostream s(argv [1],“8087”);
The port option takes a string, which may be the name of the service, as "daytime", and then it will look up the corresponding port, or explicitly the port, but it must be a string:
tcp::iostream s(argv[1], "8087");