tcp/ip pc/arduino プロジェクトをプログラミングしています。Arduino にはイーサネット シールドがあり、クライアントとして機能します。PC はブーストを実行し、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;
SmartSocket は typdef です。
typedef boost::shared_ptr<boost::asio::ip::tcp::socket> SmartSocket;
サーバーを起動すると、コンソールに「Before Accept」と表示され、accept 関数で着信クライアントを待ちます。しかし、Arduinoコードを実行すると、接続に失敗しました(ardunionシリアルモニターで)。
誰かが何がうまくいかないのか考えていますか? サーバーとクライアントがお互いを認識していないようです。ファイアウォールも停止しましたが、これは役に立ちませんでした。どんなコメントも役に立ちます!