# UDP socket (publishing to topic)

Hello,

I would like to write an UDP socket in C++, which publishes packets (which have been sent to a certain port) to a ros topic. I already studied the ros::TransportUDP Class Reference, and found the function createIncoming(). There is kind of an example code in transport_udp.cpp (line 234-270), but since I am new to UDP and ROS, I am not sure how to create my own socket out of that (libraries I would have to include/initialization/how to adapt the code below/etc.).

Is there anyone who could help? Thanks a lot!

(something for TCP as an orientation would be okay to, but I would prefer UDP since I need to write it for the latter)

The code excerpt from transport_udp.cpp:

00234 bool TransportUDP::createIncoming(int port, bool is_server)
00235 {
00236   is_server_ = is_server;
00237
00238   sock_ = socket(AF_INET, SOCK_DGRAM, 0);
00239
00240   if (sock_ <= 0)
00241   {
00242     ROS_ERROR("socket() failed with error [%s]", last_socket_error_string());
00243     return false;
00244   }
00245
00252   {
00253     ROS_ERROR("bind() failed with error [%s]",  last_socket_error_string());
00254     return false;
00255   }
00256
00260   ROSCPP_LOG_DEBUG("UDPROS server listening on port [%d]", server_port_);
00261
00262   if (!initializeSocket())
00263   {
00264     return false;
00265   }
00266