【发布时间】:2014-05-23 22:01:37
【问题描述】:
我是网络编程的新手,我尝试做回显客户端和服务器。它可以通过 localhost (127.0.0.1) 和 192.168.1.35 正常工作,但不能通过我的实际 IP。所以不可能通过 Internet 连接到我的服务器。但是,我在本地网络中检查了它并且它有效。
你可以试试client。如果连接完成,客户端会显示适当的消息。有两个客户。对于 IP 192.168.1.35 和我的实际 IP,我可以通过 such 和类似服务获得。有几个库应该与exe 位于同一目录中。
还有一个问题。我的服务器通过这些行显示什么?
tcp::endpoint ep = *iter++;
std::cout << ep << std::endl;
输出是:
[fe80::5100:812f:ad7c:a6dc%13]:0
192.168.1.35:0
我只是想获得我的 IP,而第二个是一个 IP,尽管它是本地的。但是第一个是什么?
谢谢!
我使用 MSVS 2013、Boost::Asio、Windows 7。
这是代码:
server.cpp
#ifdef WIN32
#define _WIN32_WINNT 0x0501
#include <stdio.h>
#endif
#include <iostream>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
using namespace boost::asio;
using namespace boost::posix_time;
using boost::system::error_code;
io_service service;
size_t read_complete(char * buff, const error_code & err, size_t bytes) {
if (err) return 0;
bool found = std::find(buff, buff + bytes, '\n') < buff + bytes;
// we read one-by-one until we get to enter, no buffering
return found ? 0 : 1;
}
void handle_connections() {
ip::tcp::acceptor acceptor(service, ip::tcp::endpoint(ip::tcp::v4(), 8001));
char buff[1024];
while (true) {
ip::tcp::socket sock(service);
acceptor.accept(sock);
int bytes = read(sock, buffer(buff),
boost::bind(read_complete, buff, _1, _2));
std::string msg(buff, bytes);
sock.write_some(buffer(msg));
sock.close();
}
}
int main(int argc, char* argv[]) {
using boost::asio::ip::tcp;
boost::asio::io_service io_service;
tcp::resolver resolver(io_service);
tcp::resolver::query query(boost::asio::ip::host_name(), "");
tcp::resolver::iterator iter = resolver.resolve(query);
tcp::resolver::iterator end; // End marker.
while (iter != end)
{
tcp::endpoint ep = *iter++;
std::cout << ep << std::endl;
}
handle_connections();
}
client.cpp
#ifdef WIN32
#define _WIN32_WINNT 0x0501
#include <stdio.h>
#endif
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
using namespace boost::asio;
using boost::system::error_code;
io_service service;
size_t read_complete(char * buf, const error_code & err, size_t bytes) {
if (err) return 0;
bool found = std::find(buf, buf + bytes, '\n') < buf + bytes;
// we read one-by-one until we get to enter, no buffering
return found ? 0 : 1;
}
//ip::tcp::endpoint ep(ip::address::from_string("127.0.0.1"), 8001);
ip::tcp::endpoint ep(ip::address::from_string("192.168.1.35"), 8001);
void sync_echo(std::string msg) {
msg += "\n";
ip::tcp::socket sock(service);
sock.connect(ep);
sock.write_some(buffer(msg));
char buf[1024];
int bytes = read(sock, buffer(buf), boost::bind(read_complete, buf, _1, _2));
std::string copy(buf, bytes - 1);
msg = msg.substr(0, msg.size() - 1);
std::cout << "server echoed our " << msg << ": "
<< (copy == msg ? "OK" : "FAIL") << std::endl;
sock.close();
}
int main(int argc, char* argv[]) {
// connect several clients
char* messages[] = { "John says hi", "so does James",
"Lucy just got home", "Boost.Asio is Fun!", 0 };
boost::thread_group threads;
for (char ** message = messages; *message; ++message) {
threads.create_thread(boost::bind(sync_echo, *message));
boost::this_thread::sleep(boost::posix_time::millisec(100));
}
threads.join_all();
char ch;
std::cin >> ch;
}
【问题讨论】:
-
我对 92.168.1.35 感到困惑,但现在我意识到您可能是指 192.168.1.35?
-
是的,对不起。在程序中我使用了正确的 IP。
-
This answer 包含一些与网络相关的一般问题和建议。
标签: c++ sockets tcp network-programming boost-asio