备份以下ros系统中的udp通讯代码,自取
/**
* @file udp_communication_node.cpp
* @brief ROS node for UDP communication
*
* This node implements UDP communication to continuously receive data from a remote source.
* It is designed to run directly on a ROS system.
*/
#include <iostream>
#include <boost/asio.hpp>
#include <ros/ros.h>
using boost::asio::ip::udp;
int main(int argc, char** argv)
{
ros::init(argc, argv, "udp_receiver_node");
ros::NodeHandle nh;
try {
boost::asio::io_context io_context;
udp::socket socket(io_context, udp::endpoint(udp::v4(), 12369));
while (ros::ok()) {
char data[1024];
udp::endpoint sender_endpoint;
size_t length = socket.receive_from(boost::asio::buffer(data, 1024), sender_endpoint);
std::cout << "Received data from " << sender_endpoint.address().to_string() << ": " << data << std::endl;
}
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
}
return 0;
}
linux下端口被占用及解除方法
参考来自解除占用
ros下udp通讯代码
一、问题
ros节点重复运行udp收取代码时候,端口被占用无法运行,只能换端口重新编译十分麻烦
二、解决办法
1、查看端口是否被占用:
netstat -anp |grep 8888 //查看8888端口的占用情况,这里的8888需要换成你的接受发送端口
出现如下情况说明被占用:
2、查看占用此端口的进程PID
lsof -i :8888 #8888是换成你的端口号
结果如下:
其中第二列为PID入
3、杀死进程
kill -9 3494004 //4110为进程PID
可以看到成功结果:
至此端口已解除占用啦