- /************************************************************************/
- /* recver */
- /************************************************************************/
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <WinSock2.h>
- using namespace std;
- #pragma comment(lib, "Ws2_32")
- #define MCASTADDR "233.0.0.5" //本例使用的多播组地址。
- int _tmain(int argc, _TCHAR* argv[])
- {
- int nRet;
- //fun(0x8899, 0x1100);
- WSAData wsaData;
- nRet = ::WSAStartup(MAKEWORD(2,2), &wsaData);
- SOCKET sock = ::WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0,
- WSA_FLAG_OVERLAPPED | WSA_FLAG_MULTIPOINT_C_LEAF
- | WSA_FLAG_MULTIPOINT_D_LEAF);
- BOOL bFlag = TRUE;
- nRet = ::setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*) &bFlag, sizeof(BOOL));
- sockaddr_in sockAddr;
- memset(&sockAddr, 0, sizeof(sockaddr_in));
- sockAddr.sin_family = AF_INET;
- sockAddr.sin_addr.S_un.S_addr = INADDR_ANY;
- sockAddr.sin_port = htons(5050);
- nRet = ::bind(sock, (struct sockaddr*) &sockAddr, sizeof(sockaddr_in));
- if (SOCKET_ERROR == nRet)
- {
- cout<<WSAGetLastError()<<endl;
- }
- int nTtl = 1;
- DWORD cbRet;
- BOOL bLoopBack = TRUE;
- nRet = ::WSAIoctl(sock, SIO_MULTICAST_SCOPE, &nTtl,sizeof(int), NULL, 0, &cbRet, NULL, NULL);
- nRet = ::WSAIoctl(sock, SIO_MULTIPOINT_LOOPBACK, &bLoopBack, sizeof(bLoopBack), NULL, 0, &cbRet, NULL,NULL);
- sockaddr_in remote;
- memset(&remote,0,sizeof(remote));
- remote.sin_family = AF_INET;
- remote.sin_addr.s_addr = inet_addr(MCASTADDR);
- remote.sin_port = htons(5050);
- SOCKET sockM = WSAJoinLeaf(sock,(sockaddr*)&remote,sizeof(remote), NULL,NULL,NULL,NULL,JL_BOTH);
- int nAddrLen = sizeof(sockaddr_in);
- sockaddr_in srcAddr;
- char *buf = new char[1024];
- for(;;)
- {
- memset(buf, 0, 1024);
- memset(&srcAddr, 0, sizeof(sockaddr_in));
- nRet = ::recvfrom(sock, buf, 1024, 0, (struct sockaddr*) &srcAddr, &nAddrLen);
- if (SOCKET_ERROR != nRet)
- {
- cout<<"The Src IP: "<<inet_ntoa(srcAddr.sin_addr)<<endl;
- cout<<"The Src Port: "<<ntohs(srcAddr.sin_port)<<endl;
- cout<<"The recv buf: "<<buf<<endl;
- cout<<"--------------------------------------------------------------"<<endl;
- }
- }
- delete [] buf;
- buf = NULL;
- closesocket(sockM);
- closesocket(sock);
- WSACleanup();
- //nRet = ::WSAAsyncSelect(sock, NULL, NULL, FD_READ);
- return 0;
- }
- /************************************************************************/
- /* sender */
- /************************************************************************/
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <WinSock2.h>
- using namespace std;
- #pragma comment(lib, "Ws2_32")
- #define MCASTADDR "233.0.0.5"
- int _tmain(int argc, _TCHAR* argv[])
- {
- int nRet;
- WSAData wsaData;
- nRet = ::WSAStartup(MAKEWORD(2,2), &wsaData);
- SOCKET sock = ::WSASocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, 0,
- WSA_FLAG_OVERLAPPED | WSA_FLAG_MULTIPOINT_C_LEAF
- | WSA_FLAG_MULTIPOINT_D_LEAF);
- BOOL bFlag = TRUE;
- nRet = ::setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*) &bFlag, sizeof(BOOL));
- int nTtl = 1;
- DWORD cbRet;
- BOOL bLoopBack = TRUE;
- nRet = ::WSAIoctl(sock, SIO_MULTICAST_SCOPE, &nTtl,sizeof(int), NULL, 0, &cbRet, NULL, NULL);
- nRet = ::WSAIoctl(sock, SIO_MULTIPOINT_LOOPBACK, &bLoopBack, sizeof(bLoopBack), NULL, 0, &cbRet, NULL,NULL);
- sockaddr_in remote;
- memset(&remote,0,sizeof(remote));
- remote.sin_family = AF_INET;
- remote.sin_addr.s_addr = inet_addr(MCASTADDR);
- remote.sin_port = htons(5050);
- SOCKET sockM = WSAJoinLeaf(sock,(sockaddr*)&remote,sizeof(remote), NULL,NULL,NULL,NULL,JL_BOTH);
- int nAddrLen = sizeof(sockaddr_in);
- char buf[10] = "wangliwei";
- nRet = ::sendto(sockM, buf, 10, 0, (sockaddr*) &remote, nAddrLen);
- if (SOCKET_ERROR != nRet)
- {
- cout<<"The remote IP: "<<inet_ntoa(remote.sin_addr)<<endl;
- cout<<"The remote Port: "<<ntohs(remote.sin_port)<<endl;
- cout<<"The Send buf: "<<buf<<endl;
- cout<<"--------------------------------------------------------------"<<endl;
- }
- closesocket(sockM);
- closesocket(sock);
- WSACleanup();
- return 0;
- }