Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有ethernetshield和boost服务器的Adruino可以';t连接_Boost_Arduino_Boost Asio_Shared Ptr - Fatal编程技术网

带有ethernetshield和boost服务器的Adruino可以';t连接

带有ethernetshield和boost服务器的Adruino可以';t连接,boost,arduino,boost-asio,shared-ptr,Boost,Arduino,Boost Asio,Shared Ptr,我正在编写一个tcp/ip pc/arduino项目。Arduino有一个ethernetshield,用作客户端。PC机运行boost并利用asio库作为客户端 当我尝试连接到服务器时,无法建立连接。服务器有一个网络适配器,该适配器具有192.168.1.1的静态适配器。Arduino的IP地址为192.168.1.2。这两个电缆直接通过UTP电缆连接。两者都使用端口5000 对于Arduino代码,我使用了一个示例程序来进行测试,但是失败了。设置如下所示: // Enter the IP a

我正在编写一个tcp/ip pc/arduino项目。Arduino有一个ethernetshield,用作客户端。PC机运行boost并利用asio库作为客户端

当我尝试连接到服务器时,无法建立连接。服务器有一个网络适配器,该适配器具有192.168.1.1的静态适配器。Arduino的IP地址为192.168.1.2。这两个电缆直接通过UTP电缆连接。两者都使用端口5000

对于Arduino代码,我使用了一个示例程序来进行测试,但是失败了。设置如下所示:

// Enter the IP address of the server you're connecting to:
IPAddress server(192,168,1,1); 

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 23 is default for telnet;
// if you're using Processing's ChatServer, use  port 10002):
EthernetClient client;

void setup() {
   // start the Ethernet connection:
   Ethernet.begin(mac, ip);
   // Open serial communications and wait for port to open:
Serial.begin(9600);
 while (!Serial) {
  ; // wait for serial port to connect. Needed for Leonardo only
}


 // give the Ethernet shield a second to initialize:
 delay(1000);
 Serial.println("connecting...");

 // if you get a connection, report back via serial:
 if (client.connect(server, 5000)) {
   Serial.println("connected");
 } 
 else {
   // if you didn't get a connection to the server:
   Serial.println("connection failed");
 }
}
PC服务器代码也相当简单,在类构造函数中,我执行以下操作:

    cout << "Setting up server" << endl;
    // Protocol and port
     boost::asio::ip::tcp::endpoint Endpoint(boost::asio::ip::tcp::v4(), 5000);

    // Create acceptor
    boost::asio::ip::tcp::acceptor Acceptor(IOService, Endpoint);

    // Create socket
    SmartSocket Sock(new boost::asio::ip::tcp::socket(IOService));

    cout << "Before accept..." << endl;

     // Waiting for client
         Acceptor.accept(*Sock);

    cout << "Server set up" << endl;

cout服务器和客户端不通信的原因可能有很多种。由于代码中存在漏洞,防火墙会阻止消息、NIC设置或设备无法连接。这比你想象的要普遍得多

要确保连接正常,请尝试从Ardunio上ping电脑

确保asio PC服务器代码正常。尝试像HTTP请求一样与它建立本地TCP连接。例如,类型<代码>http://127.0.0.1:5000/hello
在电脑的web浏览器url中。这将在本地主机上向端口5000上的服务器发送GET请求,该端口至少应在电脑控制台上打印“服务器设置”

如果没有,请尝试在PC服务器代码和请求中使用端口80(标准TCP端口)而不是5000。如果这样做有效,那么很可能是防火墙阻止了您的端口,或者您的Adruino代码中有一个bug

但是,如果这不起作用,那么您需要仔细查看服务器代码。asio示例应该适合您


另外,听从@Igor的建议,在您的电脑上安装
Wireshark
。它可能是免费的,但在调试网络问题时却是无价的。

是否可以使用boost连接到带ethernetshield的arduino?如果“带ethernetshield的arduino”接受TCP连接,您可以使用boost.Asio建立它们。使用一些嗅探器(比如Wireshark)查看客户端和服务器之间发生了什么。
typedef boost::shared_ptr<boost::asio::ip::tcp::socket> SmartSocket;