[CPP]View Plaincopy
- Send Side
- #include <iostream>
- #include <stdio.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <string.h>
- 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" <<endl;
- return false;
- }
- const int opt = 1;
- //Set the socket to be a broadcast type,
- int nb = 0;
- NB = setsockopt (sock, Sol_socket, So_broadcast, (char *) &opt, sizeof (opt));
- if (nb = =-1)
- {
- cout<<"Set socket error ..." <<endl;
- 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 message from broadcast address
- char smsg[] = {"abcdef"};
- int ret=sendto (sock, smsg, strlen (smsg), 0, (sockaddr*) &addrto, Nlen);
- if (ret<0)
- {
- cout<<"Send error ..." <<ret<<endl;
- }
- Else
- {
- printf ("OK");
- }
- }
- return 0;
- }
[CPP]View Plaincopy
- Receiving End Http://blog.csdn.net/robertkun
- #include <iostream>
- #include <stdio.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <string.h>
- Using namespace std;
- int main ()
- {
- Setvbuf (stdout, NULL, _IONBF, 0);
- Fflush (stdout);
- //Bind address
- struct sockaddr_in Addrto;
- Bzero (&addrto, sizeof (struct sockaddr_in));
- addrto.sin_family = af_inet;
- ADDRTO.SIN_ADDR.S_ADDR = htonl (Inaddr_any);
- Addrto.sin_port = htons (6000);
- //Broadcast address
- struct sockaddr_in from;
- Bzero (&from, sizeof (struct sockaddr_in));
- from.sin_family = af_inet;
- FROM.SIN_ADDR.S_ADDR = htonl (Inaddr_any);
- From.sin_port = htons (6000);
- int sock =-1;
- if ((sock = socket (af_inet, SOCK_DGRAM, 0)) = = = 1)
- {
- cout<<"Socket error" <<endl;
- return false;
- }
- const int opt = 1;
- //Set the socket to be a broadcast type,
- int nb = 0;
- NB = setsockopt (sock, Sol_socket, So_broadcast, (char *) &opt, sizeof (opt));
- if (nb = =-1)
- {
- cout<<"Set socket error ..." <<endl;
- return false;
- }
- if (bind (sock,struct sockaddr *) & (Addrto), sizeof (struct sockaddr_in)) = =-1)
- {
- cout<<"bind error ..." <<endl;
- return false;
- }
- int len = sizeof (sockaddr_in);
- char smsg[100] = {0};
- While (1)
- {
- //Receive messages from broadcast address
- int Ret=recvfrom (sock, smsg, 0, (struct sockaddr*) &from, (socklen_t*) &len);
- if (ret<=0)
- {
- cout<<"read error ..." <<sock<<endl;
- }
- Else
- {
- printf ("%s\t", smsg);
- }
- Sleep (1);
- }
- return 0;
- }
Since the test can be successful under the Linux virtual machine, the premise is to set the host in the same network segment, there is not forget to shut down the Linux firewall. You can set it using the Setup command.
(I can only send the test when the reception, tortured me for half a day, and then think of the Linux firewall problem.) )
For network card configuration of virtual machines, it is recommended to choose bridging mode. The NAT mode is restricted and may not receive broadcast messages.
Refer to the article online for specific reference.
I wish you success.
Small example of UDP sending and receiving broadcast messages under Linux system