代码示例:
model.addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
textField.setText(getSum());
}
});
我有2台电脑。
从计算机A输出:已连接。本地地址:XXXXXX遥控器 地址:YYYYY(XXXXX和YYYY真实IP(与ping输出相同的IP))
从计算机B输出:已连接。本地地址: 127.0.0.1 远程 地址:YYYYY(YYYY真实IP(与ping输出相同的IP))
计算机A和B只有1个NIC
为什么我得到了127.0.0.1?我无法从IP 127.0.0.1到YYYY建立真正的连接。
如何解决?
更新:
即使Windows套接字在有问题的主机上返回127.0.0.1,请参阅下面的代码
#include "stdafx.h"
#include <boost/asio.hpp>
#include <winsock2.h>
#include <iostream>
#include <string>
int _tmain(int argc, _TCHAR* argv[])
{
boost::asio::io_service service;
auto sock_ = new boost::asio::basic_stream_socket< boost::asio::ip::tcp >(service);
if(sock_){
try {
boost::asio::ip::address_v4 ipa = boost::asio::ip::address_v4::from_string(argv[1]);
boost::asio::ip::basic_endpoint< boost::asio::ip::tcp > address(ipa, (unsigned short) atoi(argv[2]));
sock_->connect(address);
std::cout<<"connected. local address:"<<sock_->local_endpoint()<<" remote address:"<<sock_->remote_endpoint()<<std::endl;
} catch (const boost::system::system_error& e)
{
std::cout<<"ERROR:"<<e.what();
}
}
int dummy;
std::cin>>dummy;
return 0;
}
答案 0 :(得分:2)
127.0.0.1通过真实网卡路由:)连接成功从127.0.0.1到远程ip因为路由:&#39;(
答案 1 :(得分:1)
你正在混合API,编译器故障并做出错误的假设。这是代码非常简化:
<强> Live On Coliru 强>
#include <boost/asio.hpp>
#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
namespace ba = boost::asio;
using ba::ip::tcp;
ba::io_service service;
tcp::socket s(service);
if (argc<3)
return 255;
try {
auto it = tcp::resolver(service).resolve({argv[1], argv[2]});
s.connect(*it); // first resolved value
std::cout << "Connected " << s.local_endpoint() << " -> " << s.remote_endpoint() << "\n";
// demo write
ba::write(s, ba::buffer("hello world\n"));
} catch (const boost::system::system_error& e) {
std::cout << "ERROR:" << e.what() << "\n";
}
}
在线演示中,输出为:
Connected 127.0.0.1:40468 -> 127.0.0.1:6767
hello world
的确,当我连接到不在环回网络上的远程主机时,我得到:
sehe@desktop:/tmp$ ./test serverbox 6767
Connected 192.168.188.20:47338 -> 192.168.188.24:6767
我不太确定你会得到那里的环回地址,但我根本不排除你做tostring
错误的可能性。
见