如何在Linux中使用C进行UDP广播? [英] How to UDP Broadcast with C in Linux?
本文介绍了如何在Linux中使用C进行UDP广播?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Linux中使用C进行UDP广播?
How to UDP Broadcast with C in Linux?
推荐答案
在许多IP协议栈中,例如Linux,此代码不起作用.您的套接字必须具有广播权限.试试这个:
In many IP stack, such as Linux, this code does not work. Your socket must have broadcast permissions. Try this:
bcast_sock = socket(AF_INET, SOCK_DGRAM, 0);
int broadcastEnable=1;
int ret=setsockopt(bcast_sock, SOL_SOCKET, SO_BROADCAST, &broadcastEnable, sizeof(broadcastEnable));
/* Add other code, sockaddr, sendto() etc. */
这篇关于如何在Linux中使用C进行UDP广播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文