UDP is the permanent datagram protocol. It has the same function as the TCP protocol. We use the UDP protocol when doing qq chat. The following is a small series to introduce how Linux uses UDP to send and receive broadcast messages. Get up and get to know it.
[cpp] view plaincopy
//transmitting end
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
setvbuf(stdout, NULL, _IONBF, 0);
fflush(stdout) ;
int sock = -1;
if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
cout ""<;socket error";
return false;
}
const int opt = 1;
//Set the socket Word is broadcast type,
int nb = 0;
nb = setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&opt, sizeof(opt));
if(nb == -1)
{
cout ""“set socket error. . <;
return false;
}
struct sockaddr_in addrto;
bzero(&addrto, sizeof(struct sockaddr_in));
addrto.sin_family=AF_INET;
addrto.sin_addr.s_addr=htonl(INADDR_BROADCAST);
addrto.sin_port=htons(6000);
int nlen =sizeof(addrto);
while(1)
{
sleep(1);
//Send a message from a broadcast address
char smsg[] = {“abcdef”};
int ret=sendto(sock, smsg, strlen(smsg), 0, (sockaddr*)&addrto, nlen);
if(ret`0)
{
cout ""“send error. . . #>
}
else
<
printf(“ok ”);
}
}
return 0;
}
[cpp] view plaincopy
Previous12Next Total 2 Pages
After the Linux system has been in operation for a long time, it will generate a lo
Linux system, when using the virtual machine VirtualBox to install Windows 8.1, prompt 0x000000C4 er
Cgroup full name control groups, can be used to limit, record, and isolate the physical resources of
The access control list (ACL) under Linux is mainly used to control the permissions
How to install Linux using the pidstat command
Steps to establish a Qt environment in Linux
How to solve the problem that Centos can't start
How to generate reports using Linux using the Sosreport command
Frequently Asked Questions for Linux Servers and Firewalls
Linux/Unix uses the md5sum command to compare file md5 values
How Linux looks at the last reboot time
Introduction to the syntax and parameters of the Linux system xlsclients command
Linux yum prompt ERROR: how to solve the dbus error?
Detailed examples of Linux dynamic library generation
Linux init detailed explanation
The measured Win10 preview version is installed normally using Office2003
Have you met? The 10 most prone to PC disasters (4)
How to turn off the automatic update function of Win10 system
How to solve the shellexperiencehost.exe program under Win10
How does the Win8 system manage the credentials for automatic login?
Windows 7 catches up with XP global market share