C ++ socket UDP broadcast in Windows

Source: Internet
Author: User

Receive broadcast message end:

# Include "stdafx. H "# include <iostream> # include <winsock2.h> # pragma comment (Lib," ws2_32.lib ") const int max_buf_len = 255; using namespace STD; int _ tmain (INT argc, _ tchar * argv []) {word wversionrequested; wsadata; int err; wversionrequested = makeword (2, 2); err = wsastartup (wversionrequested, & wsadata! = 0) {return-1;} If (lobyte (wsadata. wversion )! = 2 | hibyte (wsadata. wversion )! = 2) {wsacleanup (); Return-1;} socket clientsock = socket (af_inet, sock_dgram, 0); If (invalid_socket = clientsock) {err = wsagetlasterror (); printf ("socket error: % d \ n", err); wsacleanup (); Return-1 ;}// create a local address and bind sockaddr_in sin; sin. sin_family = af_inet; sin. sin_addr.s_un.s_addr = htonl (inaddr_any); sin. sin_port = htons (7001); If (BIND (clientsock, (sockaddr *) & sin, sizeof (SIN ))! = 0) {err = wsagetlasterror (); printf ("BIND error: % d \ n", err); closesocket (clientsock); wsacleanup (); Return-1 ;} sockaddr_in clientaddr; int addrlen = sizeof (clientaddr); int nsize; char recvbuff [1024]; memset (recvbuff, 0, 1024); While (1) {nsize = recvfrom (clientsock, recvbuff, 1024, 0, (sockaddr *) & clientaddr, & addrlen); If (nsize = socket_error) {err = wsagetlasterror (); printf ("Recv error: % d \ n ", err); break;} recvbuff [nsize] = '\ 0'; printf (" % s \ n ", recvbuff);} closesocket (clientsock ); wsacleanup (); Return 0 ;}

Send broadcast message end:

# Include "stdafx. H "# include <iostream> # include <stdio. h >#include <winsock2.h> # pragma comment (Lib, "ws2_32.lib") using namespace STD; const int max_buf_len = 255; int _ tmain (INT argc, _ tchar * argv []) {word wversionrequested; wsadata wasdata; // pointer to the winsocket information structure int err; wversionrequested = makeword (2, 2); // initialize the Winsock library, prerequisites for using Socket: Err = wsastartup (wversionrequested, & wasdata); If (Err! = 0) {return-1;} If (lobyte (wasdata. wversion )! = 2 | hibyte (wasdata. wversion )! = 2) {wsacleanup (); Return-1;} socket conn_sock = socket (af_inet, sock_dgram, 0); If (conn_sock = invalid_socket) {err = wsagetlasterror (); printf ("socket error: % d \ n", err); closesocket (conn_sock); wsacleanup (); Return-1;} bool bopt = true; // enable the broadcast option setsockopt (conn_sock, sol_socket, so_broadcast, (char *) & bopt, sizeof (bopt); // set the address sockaddr_in saudpserv; memset (& saudpserv, 0, sizeof (saudpserv); saudpserv. sin_family = af_inet; saudpserv. sin_addr.s_un.s_addr = htonl (inaddr_broadcast); saudpserv. sin_port = htons (7001); int nsize = sizeof (sockaddr_in); char buff [max_buf_len]; memset (buff, 0, max_buf_len); int nloop = 0; char * IP = NULL; while (1) {nloop ++; sprintf (buff, "message % d", nloop); int nsendsize = sendto (conn_sock, buff, strlen (buff), 0, (sockaddr *) & saudpserv, nsize); If (nsendsize = socket_error) {err = wsagetlasterror (); printf ("sendto error: % d \ n ", err); closesocket (conn_sock); wsacleanup (); Return-1;} printf (" Send: % s \ n ", buff ); sleep (5000);} closesocket (conn_sock); wsacleanup (); Return 0 ;}

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.