The concept of a broadcast package
Broadcast packets are typically used for two reasons: 11 applications want to find a resource on the local network, and the application has no prior knowledge of the address of the resource.
2 Some important features, such as routing requirements, send their information to all the neighboring machines found.
The destination address of the broadcast information depends on the network on which this information will be broadcast. A shorthand address is supported in the Internet domain for broadcast-inaddr_broadcast. Because the
A datagram socket interface must be bundled with a broadcast before, so all received broadcast messages have the sender's address and port.
Broadcast communication is a non-connected communication that does not need to establish a connection before communication. You do not need listen and accept, but you need to bind a socket to receive broadcasts.
? Sending of broadcast packets
Create socket
Setting up sockets, such as setting timeouts, allowing broadcasts, etc.
Binds the socket. You must bind a socket before using the broadcast. This step is optional, and if not, the system is automatically bound to an unused port.
Send a broadcast. The port number of the broadcast is consistent with the port number that the receiver binds to
The process of sending and receiving broadcast packets using the UDP protocol.
If we are going to 192.168.0.X, the subnet mask is: Send broadcast packets in 255.255.255.0 subnet.
The steps are as follows:
1. Initialize the Winsock library.
2. Create a socket of type Sock_diram.
3. Set the socket's properties to allow it to broadcast.
4. Sending packets to 192.168.0.255
5. Receive broadcast packets of your own broadcasts.
6. Close the socket
7. Release the network library.
Here are some things to note:
1. The receiver must know the broadcast party's slogan and then bind this port number to receive it correctly.
2. The receiver's socket does not need to be set to broadcast properties.
3. The bound IP can not use "127.0.0.1", you can use the real IP address or inaddr_any. Otherwise, the receive fails.
#include"stdafx.h"#include <WinSock2.h> #include <Windows.h> #include <string.h>#pragmaComment (lib, "Ws2_32.lib")voidAutocleanup () {WSACleanup ();}int_tmain (intARGC, _tchar* argv[]) {WORD wversionrequested; Wsadata Wsadata; Wversionrequested=makeword (2,2);intRet ret = WSAStartup (wversionrequested,&wsadata);intSock = socket (af_inet, SOCK_DGRAM,0);intBC =1;//allow broadcast messages to be sent intSo_broadcast = TRUE; ret = setsockopt (sock, Sol_socket, So_broadcast, (Char*) &so_broadcast,sizeof(So_broadcast)); Sockaddr_in addr; addr.sin_family = af_inet;//use Internet Protocol, IP protocolAddr.sin_addr. S_un. S_ADDR = htonl (Inaddr_any); Addr.sin_port = htons (2526); This step is optional if you are simply sending a broadcast. You can send broadcasts without binding//ret = bind (sock, (struct sockaddr *) &addr, sizeof (addr)); structSockaddr_in b_addr; b_addr.sin_family = af_inet; B_addr.sin_addr. S_un. S_addr =htonl (inaddr_broadcast); B_addr.sin_port = htons (2527);Charbuff[ -] ="Hello, world!."; while(1) {ret = sendto (sock, Buff, strlen (buff),0, (structsockaddr*) &b_addr,sizeof(B_ADDR)); printf"send ...%d\n", WSAGetLastError ()); Sleep ( the); } closesocket (sock); Atexit (Autocleanup);return 0;}
? receive broadcast Packets
The receiver must know the port number of the broadcast party and then bind the same port number to receive it correctly. The simple truth is that if you do not bind to a port, it does not know where to receive the data.
//Send.cpp:Defines the entry point for the console application.//#include"stdafx.h"#include <WinSock2.h> #include <Windows.h> #include <string.h>#pragmaComment (lib, "Ws2_32.lib")voidAutocleanup () {WSACleanup ();}int_tmain (intARGC, _tchar* argv[]) {WORD wversionrequested; Wsadata Wsadata; Wversionrequested=makeword (2,2); WSAStartup (Wversionrequested,&wsadata); Socket sock = socket (af_inet, SOCK_DGRAM,0);structSockaddr_in addr; addr.sin_family = af_inet; Addr.sin_addr. S_un. S_ADDR = htonl (Inaddr_any);//This port will be the same as the broadcast port .Addr.sin_port = htons (2527); Bind (sock, (structSOCKADDR *) &addr,sizeof(addr));structSockaddr_in from;intLen =sizeof( from);intRetCharbuff[ -]; while(1) {ret = recvfrom (sock, Buff, the,0, (structSOCKADDR *) & from, &len);if(Ret >0) {Buff[ret] =0; printf"%s\n", buff); printf"%s%d\n", Inet_ntoa ( from. sin_addr), Ntohs ( from. Sin_port)); }} closesocket (sock); Atexit (Autocleanup);return 0;}