Sending MAVLink command via UDP to MAVProxy

I’m trying to send MAVLink commands from my application to SITL “” to test my code. As a start I’m trying to send a “Takeoff to height 20” command. My code can send UDP packets to “”. There is something wrong with my code as MAVProxy doesn’t receive the message. I hope if anyone can tell me where the problem is. When I run
sudo tcpdump -i lo -n udp port 14551
the output is
18:42:46.734776 IP > UDP, length 41
Here is the code:

int main(){
	int udpSocket, nBytes;
	struct sockaddr_in serverAddr;
	struct sockaddr_storage serverStorage;
	socklen_t addr_size;

	mavlink_system.sysid = SYS_ID;             
	mavlink_system.compid = MAV_COMP_ID_IMU;  

	mavlink_msg_command_long_pack(mavlink_system.sysid, mavlink_system.compid, &msg, targetSysId,targetCompId, MAV_CMD_NAV_TAKEOFF, 0, 0, 0, 0, 0, 0, 0, 20);
	len = mavlink_msg_to_send_buffer(buf, &msg);

	udpSocket = socket(PF_INET, SOCK_DGRAM, 0);

	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port = htons(14551);
	serverAddr.sin_addr.s_addr = inet_addr("");
	memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

	bind(udpSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

	addr_size = sizeof serverStorage;

		sendto(udpSocket,buf,len,0,(struct sockaddr *)&serverAddr,addr_size);
	return 0;

Hi Salahuddin,

Are you armed? You can only takeoff after being armed. Also, why are you sending it to target component 214 instead of 1? That may be why MAVProxy isn’t forwarding the packet.

Hi Francisco,
Many thanks for your reply.
I’m just trying to send any command to test if I can communicate successfully with MAVProxy, therefore I tried the TakeOff command. When I send TakeOff 20 command from MAVProxy terminal, The console outputs Got MAVLink msg: COMMAND_ACK {command : 22, result : 4}. Yes it doesn’t takeoff because it is not armed but it receives the command successfully which is not the case with my application.
After what you suggested, I set target component id to 1 but still not working.
I think the problem is not with the packet itself. I think the problem is with the connection. I think my program should listen to port 14551 and communicate with whatever writes to this port. But in my case, 18:42:46.734776 IP > UDP, length 41, which doesn’t look right, right?

I don’t know Boost very well, but looking at your code again, it looks like you are doing an UDP client connecting to remote port 14551 - you actually need to do the opposite, an UDP server receiving on local port 14551, then you send packets to the port you receive from.

You can also make MAVProxy have an UDP server you can send messages to by passing the following argument to --out udpin: You can choose another port if you want to.

@OXINARF, Wow, the second suggestion worked for me. Finally. I don’t know how can I thank you :slight_smile:
MAVProxy being UDP server and my program is the client will function the same as the opposite, right? I mean nothing will change?

Yes, it should be the same.