ROS服務端與客戶端

2021-10-24 18:03:30 字數 1904 閱讀 6582

#!/usr/bin/env python

# -*- coding: utf-8 -*-

import time, thread

import rospy

from geometry_msgs.msg import twist

from std_srvs.srv import trigger, triggerresponse

pubcommand =

false

tpub = rospy.publisher(

'/turtle1/cmd_vel'

, twist, queue_size=10)

defcommand_thread()

:while

true

:if pubcommand:

v = twist(

) v.linear.x =

0.5 v.angular.z =

0.5 tpub.publish(v)

time.sleep(

0.1)

defcallback

(req)

:global pubcommand

pubcommand =

bool(1

- pubcommand)

rospy.loginfo(

"call the callback! %s"

, pubcommand)

return triggerresponse(1,

"change!"

)def

******service()

: rospy.init_node(

'sxcservice'

) s = rospy.service(

'/turtle_command'

, trigger, callback)

print

'wait!!'

thread.start_new_thread(command_thread,()

) rospy.spin(

)if __name__ ==

'__main__'

: ******service(

)

#!/usr/bin/env python

# coding=utf-8

import rospy

from turtlesim.srv import spawn

defturtle_spawn()

:# ros節點初始化,生成節點addturtle

rospy.init_node(

'addturtle'

)# 等待'/spawn'服務,若無一直等待

rospy.wait_for_service(

'/spawn'

)try

:# 建立乙個連線名為'/spawn'的服務,資料型別為spawn

add_turtle = rospy.serviceproxy(

'/spawn'

, spawn)

# 呼叫請求服務,輸入請求引數,並得到服務端返回結果

response = add_turtle(2,

2,0,

'sxc'

)return response.name

except rospy.serviceexception, e:

print e

if __name__ ==

'__main__'

: rospy.loginfo(

"creat turtle,name: %s"

,(turtle_spawn())

)

C 服務端與客戶端

c 服務端與客戶端連線實現的由來 那麼既然乙個伺服器端口可以應對多個客戶端連線,那麼接下來我們就看一下,如何讓多個客戶端與服務端連線。如同我們上面所說的,乙個tcpclient就是乙個socket,所以我們只要建立多個tcpclient,然後再呼叫connect 方法就可以了 c 服務端與客戶端連線...

服務端與客戶端互動

搭建伺服器 伺服器端 using system.net.sockets using system.net using system.io using system.text namespace sockerservice endpoint point new ipendpoint ipaddress...

NodeJS TCP客戶端與服務端

server.js var net require net 載入網路模組 var clients 0 建立id來引用連線的每乙個客戶端 var server net.createserver function client client.write welcome client clientid 使...