如何修改ROS节点的随机分配方式

1)下载git@github.com:ros/ros_comm.git

2)打开ros/ros_comm/utilities/xmlrpcpp/src/XmlRpcServer.cpp

3)bool

XmlRpcServer::bindAndListen(int port, int backlog /*= 5*/)

ros  会默认 把 port = 0 传进来   如果端口为0 ,linux就会分配随机端口

然后调用 XmlRpcSocket::bind(fd, port)  

进来   这句话 又 调用了 bind(fd, (sockaddr*)&ss, ss_len)

 

修改操作  :

修改本注释下面的内容

// Bind to the specified port on the default interface

目标使 ros节点的 端口分配处于  44400 -44500 区间

 port = 44400;

 bool b_bind= false;

 while(port<44500){

   if (  XmlRpcSocket::bind(fd, port))

  {

       b_bind= true;

       break;

   }

   port++;

 }

 if(!b_bind){  

       XmlRpcUtil::error("XmlRpcServer::bindAndListen: Could not bind to specified port (%s).", XmlRpcSocket::getErrorMsg().c_str());

       this->close();

       return false;

 }

编译完成之后 ,生成libxmlrpcpp.so  ,替换到 /opt/ros/melodic/lib/下即可

PS:如果有更加精细的需要,可以为每个节点分配固定的端口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值