|
- #include "sockLib.h"
- #include "iostream.h"
- #include "vxWorks.h"
- #include "inetLib.h"
- #include "taskLib.h"
- #define MCastAddr "234.5.6.7"
- #define MaxLine 1024
- void main()
- {
- struct sockaddr_in local;
- int recvsocket;
- struct ip_mreq mcast;
- recvsocket=socket(AF_INET,SOCK_DGRAM,0);
- local.sin_family=AF_INET;
- local.sin_port=htons(2009);
- local.sin_addr.s_addr=htonl(INADDR_ANY);
- mcast.imr_interface.s_addr=htonl(INADDR_ANY);
- mcast.imr_multiaddr.s_addr=inet_addr(MCastAddr);
- bind(recvsocket,(struct sockaddr*)&local,sizeof(local));
- setsockopt(recvsocket,IPPROTO_IP,IP_ADD_MEMBERSHIP,(char*)&mcast,sizeof(mcast));
- char RecvBuffer[MaxLine]={0};
- int i=1;
- while(1)
- {
- int n=recvfrom(recvsocket,RecvBuffer,MaxLine,0,NULL,NULL);
- RecvBuffer[n]='\0';
- cout<<"receive:"<
- }
- }
复制代码
为何程序在recvfrom老是不阻塞,一直的死循环输出。我并没有send数据给它啊。
发送程序为:
- #include
- #include
- #define MCastAddr "234.5.6.7"
- #define MaxLine 1024
- void main()
- {
- WSADATA wsadata;
- int ret=WSAStartup(MAKEWORD(1,0),&wsadata);
- if(ret!=0)
- return;
-
- struct sockaddr_in mcastaddr;
- SOCKET sendsocket;
-
- sendsocket=socket(AF_INET,SOCK_DGRAM,0);
-
- mcastaddr.sin_family=AF_INET;
- mcastaddr.sin_port=htons(2009);
- mcastaddr.sin_addr.s_addr=inet_addr(MCastAddr);
- char SendBuffer[MaxLine]="hello";
- for(int i=0;i<1000;i++)
- {
- cout<
- int n=sendto(sendsocket,SendBuffer,strlen(SendBuffer),0,(struct sockaddr *) &mcastaddr,sizeof(mcastaddr));
- Sleep(500);
- }
- }
复制代码
发送端在windows系统下,接收端在vxworks系统下。
|
|