首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

Socket如何加入组播组啊

2011-12-12 
Socket怎么加入组播组啊?最近用C#中的Socket进行通讯编码。但是发现在用Socket进行接受组播的时候,因为组播

Socket怎么加入组播组啊?
最近用C#中的Socket进行通讯编码。
但是发现在用Socket进行接受组播的时候,因为组播地址不是本地的127.0.0.1所以无法绑定。接受不料组播数据包。
代码如下:

                                IPEndPoint   ipEndPoint   =   new   IPEndPoint(System.Net.IPAddress.Parse(multiIPAddress),   mutilUdpPort);
                                udpReceiveMutilVideoSocket   =   new   Socket(System.Net.Sockets.AddressFamily.InterNetwork,   System.Net.Sockets.SocketType.Dgram,   System.Net.Sockets.ProtocolType.Udp);
                                udpReceiveMutilVideoSocket.Bind(ipEndPoint);                      

在Bind的时候,告诉我说,远程主机地址无法绑定。

我也想过用UdpClient,但是UdpClient没有延时设置,如果万一给组播地址发送组播数据包的机器挂了,我这个UdpClient不是一直在那里等待接受数据包吗?这样下来,程序就down在那里啦!

哪位用过用Socket进行接受组播数据包的,指点下啊,谢谢!本人学习C#不到10天啊,大家帮帮忙啊,项目紧急啊,必须尽快解决啦!没有办法啦!



[解决办法]
这个例子很详细,估计够你用了:

http://www.codeproject.com/cs/internet/multicast.asp

E文文章,注册可下源代码.
[解决办法]
//发送方程序 sender.c

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

#define BUFFSIZE 2048
#define IP_ADDR "224.8.8.1 "
#define DEST_PORT 8888

int main(int argc,char *argv[])
{
int sockfd;
struct sockaddr_in addr;
char szError[100];
char buf[] = "Hello, World! ";
int ttl = 255 ; // Arbitrary TTL value.

WSADATA WSAData;
WORD wVersionRequested;
wVersionRequested = MAKEWORD(2, 2);
if (WSAStartup (wVersionRequested , &WSAData) != 0)
{
printf ( "recver:Initialize Winsock error! ");
exit(1);
}
if (LOBYTE(WSAData.wVersion) != 2 || HIBYTE(WSAData.wVersion) != 2 ) {
WSACleanup( );
printf ( "setsockopt failed! Error: %d ", WSAGetLastError ());
exit(1);
}

addr.sin_family = AF_INET;
addr.sin_port = htons(DEST_PORT);
addr.sin_addr.s_addr = inet_addr(IP_ADDR);

if ((sockfd=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
printf( "sender:new a socket error!\n ");
exit(1);
}

if(setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, (char *)&ttl, sizeof(ttl))!=0)
{
printf( "error setsockopt IP_MULTICAST_TTL\n ");
exit(1);
}
/*
if (bind(sockfd,(struct sockaddr *)&recver_addr,sizeof(recver_addr)) < 0) {
closesocket(sockfd);
printf( "recver:bind socket error!\n ");
exit(1);
}
*/

if (sendto (sockfd, buf, sizeof(buf) , 0, (struct sockaddr *)&addr, sizeof(addr))


== SOCKET_ERROR)
{
wsprintf (szError, TEXT( "sendto failed! Error: %d "),
WSAGetLastError ());
MessageBox (NULL, szError, TEXT( "Error "), MB_OK);
closesocket (sockfd);
return FALSE;
}
else
{
printf( "send ok\n ");
}

// Close Sock.
closesocket (sockfd);
WSACleanup ();
return 0;
}

// 接收方程序 receivers.c

//#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>

#define BUFFSIZE 2048
#define RECV_IP_ADDR "224.8.8.1 "
#define DEST_PORT 8888

int main(int argc,char *argv[])
{
int sockfd;
int sock_reuse = 1;
struct ip_mreq multicast;
struct sockaddr_in recver_addr;
char szError[100];

int index = 0, // Integer index
iRecvLen; // Length of recv_sin
char szMessageA[1024*320]; // ASCII string
TCHAR szMessageW[1024*320]; // Unicode string

WSADATA WSAData;
WORD wVersionRequested;
wVersionRequested = MAKEWORD(2, 2);
if (WSAStartup (wVersionRequested , &WSAData) != 0)
{
printf ( "recver:Initialize Winsock error! ");
exit(1);
}
if (LOBYTE(WSAData.wVersion) != 2 || HIBYTE(WSAData.wVersion) != 2 ) {
WSACleanup( );
printf ( "setsockopt failed! Error: %d ", WSAGetLastError ());
exit(1);
}

multicast.imr_multiaddr.s_addr = inet_addr(RECV_IP_ADDR);
multicast.imr_interface.s_addr = htonl(INADDR_ANY);
recver_addr.sin_family = AF_INET;
recver_addr.sin_port = htons(DEST_PORT);
recver_addr.sin_addr.s_addr = INADDR_ANY;

if ((sockfd=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
printf( "recver:new a socket error!\n ");
exit(1);
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&sock_reuse, sizeof(sock_reuse)) < 0) {
printf( "recver : socket options set error ");
exit(1);
}

if (bind(sockfd,(struct sockaddr *)&recver_addr,sizeof(recver_addr)) < 0) {
closesocket(sockfd);
printf( "recver:bind socket error!\n ");
exit(1);
}

if (setsockopt(sockfd,IPPROTO_IP,IP_ADD_MEMBERSHIP,(char *)&multicast,sizeof(multicast)) < 0 ) {
sprintf (szError, TEXT( "setsockopt failed! Error: %d "), WSAGetLastError ());
printf( "%s\n ", szError);


closesocket(sockfd);
exit(1);
}

printf( "Receive on %s:%d\n ", RECV_IP_ADDR, DEST_PORT);
iRecvLen = sizeof (recver_addr);
memset(szMessageA, 0, sizeof(szMessageA));
// Receive data from the multicasting group server.
if (recvfrom (sockfd,
szMessageA,
sizeof(szMessageA),
0,
(struct sockaddr FAR *) &recver_addr,
&iRecvLen) == SOCKET_ERROR)
{
wsprintf (szError, TEXT( "recvfrom failed! Error: %d "),
WSAGetLastError ());
MessageBox (NULL, szError, TEXT( "Error "), MB_OK);
closesocket (sockfd);
return FALSE;
}
else
{
// Convert the ASCII string to a Unicode string.
for (index = 0; index <= sizeof (szMessageA); index++)
szMessageW[index] = szMessageA[index];

MessageBox (NULL, szMessageW, TEXT( "Info "), MB_OK);
}

// Disable receiving on Sock before closing it.
shutdown (sockfd, 0x00);

// Close Sock.
closesocket (sockfd);

WSACleanup ();
return 0;
}

[解决办法]
关注,帮顶

热点排行