Subscriber to socket in python [closed]
Hi everyone I would like to implement a python node that subscribes to a topic (odom in my case) and retransmits part of the message on a socket. I have already implemented such a thing:
#!/usr/bin/env python
import rospy
from std_msgs.msg import String
import socket
import sys
from nav_msgs.msg import Odometry
import struct
from time import sleep
def callback(msg):
string=str(msg.pose.pose.position.x)+"\n"+str(msg.pose.pose.position.y)+"\n"
try:
connOdom.send(string.encode())
print (string)
except socket.error:
print ("Error client lost")
sleep( 2 )
def listener():
rospy.init_node('serverOdom', anonymous=True)
rospy.Subscriber('odom',Odometry, callback)
rospy.spin()
if __name__ == '__main__':
serverOdom = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
serverOdom.bind(('', 5005))
except socket.error as msg:
print('# Bind failed. ')
sys.exit()
serverOdom.listen(10)
connOdom, addrOdom = serverOdom.accept()
listener()
the problem is that when the client is closed it can no longer reestablish the connection once it has restarted. and i don't know how to fix this. does anyone have any idea? Thanks in advance
if with "client" you are referring to the socket -- so not the ROS side -- then this would not be a ROS problem and I would recommend to not limit yourself to support resources which are ROS specific.
As-is, this reads like a generic Python socket issue, which I would recommend you post about on Stack Overflow or another forum dedicated to such topics.
Ok, I was in doubt as to whether to ask here or on another forum. thanks