www.pudn.com > ipv6_send-recv.rar > sendv6.cpp


#include  
#include  
#include  
#include  
#pragma comment(lib,"WS2_32.lib")//ÒýÈë¿âº¯Êý¡£  
struct IPV6_MLD{ /* MLD header */ 
	unsigned char type;       /* type of message */ 
	unsigned char code;       /* Code */ 
	unsigned short checksum;  /* checksum of MLD */ 
	unsigned short resp_time; /* max response time */ 
	unsigned short reseved;   /* reserved */ 
	unsigned char  saddr[16];    /* source address */ 
	unsigned char  daddr[16];    /* group address */ 
}; 
void fill_ip(struct in6_addr *dest_ip)  //fill dest ip 
{ 
 memset(dest_ip,0,16); 
 dest_ip->s6_addr[0]=255; 
 dest_ip->s6_addr[1]=2; 
 dest_ip->s6_addr[15]=2; 
} 
 
void main() 
{ 
  
 WSADATA wsaData; 
  char *mesg; 
 SOCKET s;  
 struct IPV6_MLD *mld; 
 struct sockaddr_in6 dest; 
 int bsend; 
 int i; 
 mesg =(char *)malloc(sizeof(struct IPV6_MLD)); 
if (WSAStartup(0x0101,&wsaData) != 0){ 
	fprintf(stderr,"WSAStartup failed: %d\n",GetLastError()); 
  } 
if((s=socket(AF_INET6,SOCK_RAW,66))==INVALID_SOCKET){ 
	fprintf(stderr,"WSAStartup failed: %d\n",GetLastError()); 
  } 
 memset(&dest,0,sizeof(dest)); 
 dest.sin6_family=AF_INET6; 
 fill_ip(&(dest.sin6_addr)); 
 mld=(struct IPV6_MLD *)mesg; 
 mld->type=130; 
 mld->code=2; 
 mld->resp_time=20; 
 memset(mld->saddr,0,16); 
 memset(mld->daddr,0,16); 
 for(i=0;i<16;i++) 
   mld->saddr[i]=48+5*i; 
 mld->daddr[0]=255; 
 mld->daddr[1]=5; 
 mld->daddr[14]=166; 
 mld->daddr[15]=255; 
 bsend=sendto(s,mesg,sizeof(struct IPV6_MLD),0,(struct sockaddr*)&dest,sizeof(dest));  
 printf("%d\n",bsend); 
 if (bsend == SOCKET_ERROR) 
	 fprintf(stderr,"sendto failed: %d\n",WSAGetLastError()); 
  system("pause"); 
}