IPv6-based multicast communication

Source: Internet
Author: User

Client:

// Client # include <winsock2.h> # include <stdio. h> # include <stdlib. h> # include <ws2tcpip. h> // # include "tp1_6.h" # include <iostream> # pragma comment (Lib, "ws2_32.lib") using namespace STD; # define hello_port 7905 // defines the port # define hello_group "ff02 :: 99 "//" 224.0.0.99 "//" 228.4.5.6 "// defines the multicast target address 224.0.1.0-238.255.255.255 # define buffer_size 1024 // The client must be added to the multicast group, and receive data packets in real time int main () {cout <"this is client who join to some MUL Ticast group and receive data from Multicast Source! "<Endl; wsadata WD; wsastartup (makeword (2, 2), & WD); int err =-1; // socket s; addrinfo hints; addrinfo * res = NULL; memset (& hints, 0, sizeof (hints); // hints. ai_family = pf_unspec; hints. ai_family = af_inet6; hints. ai_socktype = sock_dgram; hints. ai_protocol = ipproto_udp; // you can change the value to 0 here to enable the system to select hints. ai_flags = ai_numerichost; int rc = getaddrinfo (pcstr) "fe80: 6820: ef2d: fece: 9fb6", "6000", & hints, & res ); // parse the recipient's receiving address if (RC! = 0) {printf ("getaddrinfo fail \ n"); Return 0;} socket sockclt = socket (res-> ai_family, res-> ai_socktype, res-> ai_protocol ); /**************************/int ret; const int on = 1; // allow multiple instances of the program to run on the same machine ret = setsockopt (sockclt, sol_socket, so_reuseaddr, (char *) & on, sizeof (on); If (ret = socket_error) {wsacleanup (); cout <"error in setsockopt (so_reuseaddr):" <wsagetlasterror () <Endl; return 0;} // set The number of multicast hops should actually be placed on the sending end .. Const int routenum = 10; ret = setsockopt (sockclt, ipproto_ipv6, ipv6_multicast_hops, \ (char *) & routenum, sizeof (routenum); If (ret = socket_error) {wsacleanup (); cout <"error in setsockopt (ip_multicast_ttl):" <wsagetlasterror () <Endl; return 0 ;} /************************* // char WHT [80 ]; /* bind socket */ret = BIND (sockclt, res-> ai_addr, res-> ai_addrlen); // bind the port // cout <inet_ntop (af_inet6, (void *) (Re S-> ai_addr), WHT, 80) <Endl; If (socket_error = RET) {printf ("socket_error \ n"); Return 0;} struct defaults 6_mreq mreq6; /* join Broadcast Group */inet_ton (af_inet6, hello_group, & mreq6.1096mr _ multiaddr); mreq6.1096mr _ multiaddr. u. byte; For (INT I = 0; I <16; I ++) {printf ("% x", mreq6.1096mr _ multiaddr. u. byte [I]); // cout <mreq6.1096mr _ multiaddr. u. byte [I] <Endl;} mreq6.20.6mr _ interface = htonl (inaddr_any);/* the network interface is default * // * Add the local machine to the Broadcast Group */err = setso Ckopt (sockclt, ipproto_ipv6, scheme, (char *) & mreq6, sizeof (mreq6); If (ERR <0) {perror ("setsockopt (): Limit 6_add_membership "); return-4;} Char Buf [buffer_size + 1]; // read the content buffer pool char SRC [50]; // int ret; // read the content size sockaddr_storage SA; int iaddrlen = sizeof (SA); // The length is only 24 characters long and not long enough. So we need to change it to 32. The actual receipt is 28 :( zeromemory (& SA, sizeof (SA); While (1) {// sleep (10000 ); // clientlen = sizeof (client); // memset (& client, 0, clientlen); memset (SRC, 0, sizeof (SRC )); // cout <"can receive? "<Endl; ret = recvfrom (sockclt, Buf, sizeof (BUF), 0, (struct sockaddr *) & SA, & iaddrlen); cout <RET <Endl; if (ret = 0) // do_read sends an empty string {continue;} else if (ret = socket_error) {If (wsagetlasterror () = wsaeintr) // The error break returned by the primary thread to terminate recvfrom; cout <"error in recvfrom:" <wsagetlasterror () <Endl; // break ;} buf [RET] = '\ 0'; cout <"recieve from:" <inet_ntop (af_inet6, (void *) & SA, SRC, 50) <Endl; cout <"Received:" <Buf <Endl; cout <"----------------" <Endl ;}}

Server:

// Server # include <winsock2.h> # include <stdio. h> # include <ws2tcpip. h> // # include "tp1_6.h" # include <iostream> # pragma comment (Lib, "ws2_32.lib") using namespace STD; # define hello_port 7905 // defines the port # define hello_group "ff02: 99" // defines the user multicast address in the Link range # define buffer_size 1024int main () {cout <"IPv6: This is server for sending data to multicast address! "<Endl; wsadata WD; wsastartup (makeword (2, 2), & WD); char sendline [buffer_size + 1]; // socket s; addrinfo hints; addrinfo * res = NULL; memset (& hints, 0, sizeof (hints); // hints. ai_family = pf_unspec; hints. ai_family = af_inet6; hints. ai_socktype = sock_dgram; hints. ai_protocol = ipproto_udp; // you can change the value to 0 here to enable the system to select hints. ai_flags = ai_numerichost; int rc = getaddrinfo (pcstr) hello_group, "6000", & hints, & res); // parse the recipient's receiving address if (RC! = 0) {printf ("getaddrinfo fail \ n"); Return 0;} socket sockclt = socket (res-> ai_family, res-> ai_socktype, res-> ai_protocol ); // char WHT [80]; // cout <res-> ai_addr-> sa_data [15] <Endl; // cout <inet_ntop (af_inet6, (void *) (res-> ai_addr), WHT, 80) <Endl; while (1) {cin. getline (sendline, buffer_size); If (strncmp (sendline, "end", 3) = 0) break; sendto (sockclt, sendline, strlen (sendline), 0, res-> ai_addr, res-> ai_addrlen);} 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.