46 m_socket = socket(AF_INET, SOCK_DGRAM, 0);
51 ROS_INFO(
"Setting socket options...");
53 int result = setsockopt(
m_socket, SOL_SOCKET, SO_REUSEADDR, (
void*)&option_value,
sizeof(
int));
56 std::stringstream error;
57 error <<
"Failed to set socket option: ";
70 error <<
"ENOPROTOOPT";
76 error <<
"unknown error";
90 ROS_INFO(
"Binding socket to local address...");
94 std::stringstream error;
95 error <<
"Failed to bind socket to local address:" << strerror(errno);
101 mreq.imr_multiaddr.s_addr = inet_addr(multicast_ip.c_str());
103 ROS_INFO(
"Joining multicast group %s...", inet_ntoa(mreq.imr_multiaddr));
105 result = setsockopt(
m_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (
char *)&mreq,
sizeof(mreq));
108 std::stringstream error;
109 error <<
"Failed to set socket option: ";
122 error <<
"ENOPROTOOPT";
128 error <<
"unknown error";
135 ROS_INFO(
"Enabling non-blocking I/O");
136 int flags = fcntl(
m_socket, F_GETFL, 0);
137 result = fcntl(
m_socket, F_SETFL, flags | O_NONBLOCK);
140 std::stringstream error;
141 error <<
"Failed to enable non-blocking I/O: " << strerror(errno);
155 sockaddr_in remote_addr;
156 int addr_len =
sizeof(
struct sockaddr);
157 int status = recvfrom(
162 (sockaddr *)&remote_addr,
163 (socklen_t*)&addr_len);
166 ROS_DEBUG(
"%4i bytes received from %s:%i", status, inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port));
167 else if (status == 0)
170 HostAddr.sin_addr = remote_addr.sin_addr;
Exception class thrown by socket classes in this file.
UdpMulticastSocket(const int local_port, const std::string multicast_ip="224.0.0.1")
Creates a socket and joins the multicast group with the given address.
int recv()
Retrieve data from multicast group.
static const int MAXRECV
Maximum number of bytse that can be read at a time.
int send(const char *buf, unsigned int sz, int port)