Winsock Transceiver Broadcast Packet "Go"

Source: Internet
Author: User
Tags htons

Winsock Transceiver Broadcast Packet

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

#include"StdAfx.h"#include <WinSock2.h>#include <Windows.h>#include <String.h>#pragma comment (lib, "Ws2_32.lib")voidAutocleanup () {WSACleanup ();}int _tmain (int argc, _tchar*Argv[]) {WORD wversionrequested; Wsadata Wsadata; Wversionrequested=makeword (2,2);IntRet ret = WSAStartup (wversionrequested,&Wsadata);int sock = socket (af_inet, SOCK_DGRAM,0);int BC =1;//Allow broadcast messages to be sentint so_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, which is IP protocol addr.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);Char buff[50] = "hello, World!while (10, (struct sockaddr*) &b_ Addr, sizeof (B_ADDR)); printf (  " ", WSAGetLastError ()); Sleep (3000return 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>#pragma comment (lib, "Ws2_32.lib")voidAutocleanup () {WSACleanup ();}int _tmain (int argc, _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 is to be consistent with the broadcast port Addr.sin_port = htons (2527); Bind (sock, (struct sockaddr *) &addr,sizeof(addr));struct SOCKADDR_INFrom;int len =sizeofFrom);IntRetChar buff[50];while (1) {ret = recvfrom (sock, Buff,49,0, (struct sockaddr *) &from, &len); if (Ret > 00" %s\n ", Buff); printf ( "%s%d\nfrom.sin_addr), Ntohs (from.sin_port)); }} closesocket (sock); Atexit (Autocleanup); return 0    

Winsock send and receive broadcast package "Go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.