Windows UPD Broadcast package failed to send to LAN workaround

Source: Internet
Author: User
Tags htons

Unable to send broadcast packets of the computer and the host can send the broadcast of the comparison, found that can not send the host on the radio has installed virtual machine, there is also a virtual network card, all the virtual network card is closed, and then test, all normal, whether it is win7,win10 or XP.

Disable the VMware Virtual network cards, and then everything is fine.

UDP Broadcast Server program:

Boardcast_server.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <WinSock2.h> #include <Windows.h> #pragma comment (lib, "Ws2_32.lib") int _ Tmain (int argc, _tchar* argv[]) {//start socket Apiword wversionrequested = Makeword (2, 2); Wsadata wsadata;if (0! = WSAStartup (wversionrequested, &wsadata)) {printf ("WSAStartup failed with Error:%d\n", GetLastError ()); return exit_failure;} if (2! = Hibyte (wsadata.wversion) | | 2! = lobyte (wsadata.wversion)) {printf ("Socket version not supported.\n"); WSACleanup (); return exit_failure;} Socket SOCKFD = socket (af_inet, SOCK_DGRAM, IPPROTO_UDP), if (Invalid_socket = = sockfd) {printf ("Socket failed with error:% D\n ", WSAGetLastError ()); WSACleanup (); return exit_failure;} Sockaddr_in addr;memset (&addr, 0, sizeof (addr)); addr.sin_addr. S_un. s_addr = inaddr_broadcast;addr.sin_family = Af_inet;addr.sin_port = Htons (7777); BOOL bboardcast = true;if (Socket_error = = setsockopt (SOCKFD, Sol_socket, So_broadcast, (const char*) &bboardcast, siz EOF (BBOARDCAST)) {printf ("SetSockOpt failed with Error:%d\n", WSAGetLastError ()), if (invalid_socket! = sockfd) {closesocket (sockfd ); sockfd = Invalid_socket;} WSACleanup (); return exit_failure;} printf ("Server start to Boardcast...\n"), char buf[] = "This message from desktop-879imis!"; while (1) {if (Socket_error = = SendTo (sockfd, buf, sizeof (BUF), 0, (lpsockaddr) &addr, sizeof (addr))) {printf ("SendTo Failed with error:%d\n ", WSAGetLastError ()); Sleep (+); continue;} Sleep (1000);} WSACleanup (); return 0;}

  

UDP Broadcast Client program:

Boardcast_client.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <WinSock2.h> #include <Windows.h> #pragma comment (lib, "Ws2_32.lib") #define Max_buffer 256int _tmain (int argc, _tchar* argv[]) {WORD wversionrequested = Makeword (2, 2); Wsadata wsadata;if (0! = WSAStartup (wversionrequested, &wsadata)) {printf ("WSAStartup failed with Error:%d/n", GetLastError ()); return exit_failure;} if (2! = Hibyte (wsadata.wversion) | | 2! = lobyte (wsadata.wversion)) {printf ("Socket version not supported./n"); WSACleanup (); return exit_failure;} Socket sock = socket (af_inet, SOCK_DGRAM, IPPROTO_UDP), if (Invalid_socket = = sock) {printf ("Socket failed with error:%d/n" , WSAGetLastError ()); WSACleanup (); return exit_failure;} Sockaddr_in addr;memset (&addr, 0, sizeof (addr)); addr.sin_family = Af_inet;addr.sin_addr. S_un. s_addr = Inaddr_any;addr.sin_port = Htons (7777), if (Socket_error = bind (sock, (lpsockaddr) &addr, sizeof (addr))) { printf ("Bind failed with Error:%d/n", WSAGetLastError ()); if (invalid_sOcket! = sock) {closesocket (sock); sock = Invalid_socket;} WSACleanup (); return exit_failure;} Char Szbuf[max_buffer] = {0}; Sockaddr_in remote;memset (&remote, 0, sizeof (remote)), int len = sizeof (remote), while (1) {if (Socket_error = = Recvfrom (sock, Szbuf, max_buffer-1, 0, (lpsockaddr) &remote, &len)) {printf ("Recvfrom failed with Error:%d/n", WSAGetLastError ()); continue;} printf ("Recv data: <%s:%d>%s\n", Inet_ntoa (REMOTE.SIN_ADDR), Ntohs (Remote.sin_port), szbuf);} return 0;}

  

Windows UPD Broadcast package failed to send to LAN workaround

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.