组播编程

组播编程(含一个端口加入多个组播)

1**. 组播客户端**

.h文件

#pragma  once

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <ZYF_SRC/osal/OSAL.cpp>

#ifdef WIN32
    #include <winsock.h>//ip_mreq
    //#include <WINSOCK2.H>
    #pragma comment(lib, "Wsock32.lib")//ws2_32.lib
    #define close closesocket
    #define LOCAL_IP "192.168.0.235"//修改自己的ip
    typedef int socklen_t;
#else
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <unistd.h>//close()
#endif


#define  GROUP_IP1  "239.255.0.2"//224.0.0.0-239.255.255.255
#define  GROUP_IP2  "239.255.0.3"//224.0.0.0-239.255.255.255
#define  PORT       8058

.cpp文件


#include "struct.h"

//int datalen;

struct sockaddr_in localSock;
struct ip_mreq group1, group2;


int GetSOCK_DGRAM(unsigned short port)
{
    SOCKET sd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sd < 0){
        perror("Opening datagram socket error");
        exit(1);
    } else
        printf("Opening datagram socket....OK.\n");

    {
       //此处代码用于端口复用
        int reuse = 1;
        if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0){
            perror("Setting SO_REUSEADDR error");
            close(sd);
            exit(1);
        } else
            printf("Setting SO_REUSEADDR...OK.\n"); 
    }


    int ret = 0;

    memset((char *) &localSock, 0, sizeof(localSock));
    localSock.sin_family = AF_INET;
    localSock.sin_port = port;
    localSock.sin_addr.s_addr = INADDR_ANY;
    if(bind(sd, (struct sockaddr*)&localSock, sizeof(localSock))){
        perror("Binding datagram socket error");
        close(sd);
        exit(1);
    } else
    {
        printf("Binding datagram socket...OK.\n");
    }   

    group1.imr_multiaddr.s_addr = inet_addr(GROUP_IP1);//相当于组名
    group1.imr_interface.s_addr = inet_addr(LOCAL_IP);//自己的ip
    if(setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group1, sizeof(group1)) < 0){
        perror("Adding multicast group1 error");
        close(sd);
        exit(1);
    } else
        printf("Adding multicast group1...OK.\n");

    group2.imr_multiaddr.s_addr = inet_addr(GROUP_IP2);//相当于组名
    group2.imr_interface.s_addr = inet_addr(LOCAL_IP);//自己的ip
    if(setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group2, sizeof(group2)) < 0){
        perror("Adding multicast group2 error");
        close(sd);
        exit(1);
    } else
        printf("Adding multicast group2...OK.\n");

    //datalen = recvSize;

    return sd;
}

int InitSocket()
{   
#ifdef WIN32
    int Error;
    WORD VersionRequested;
    WSADATA WsaData;
    VersionRequested=MAKEWORD(2,2);
    Error=WSAStartup(VersionRequested,&WsaData); //启动WinSock2
    if(Error!=0)
    {
        return 0;
    }
    else
    {
        if(LOBYTE(WsaData.wVersion)!=2||HIBYTE(WsaData.wHighVersion)!=2)
        {
            WSACleanup();
            return 0;
        }       
    }
#endif


    return 0;
}

int main(int argc, char *argv[])
{
    printf("UdeClientStart().group1=%s,group2=%s,port=%d,locol=%s,build=%s\n", GROUP_IP1,GROUP_IP2, PORT, LOCAL_IP, __TIME__);
    InitSocket();
    SOCKET sd = GetSOCK_DGRAM(htons(PORT));
    struct sockaddr_in from;
    int fromlen = sizeof(from); 
    int nRecvLen = 1;
    const int FILE_LEN = 1024*10;
    //char* bufWrite = new char[FILE_LEN];
    const int recvSize = 1024*1000;
    unsigned char* databuf = new unsigned char[recvSize];

    while(1)//nRecvLen > 0
    {
        nRecvLen = recvfrom(sd, (char*)databuf, recvSize, 0, (struct sockaddr*)&from, (socklen_t*)&fromlen) ;//read
        if (nRecvLen > 0)
        {           
        //  continue;
        }

        printf("sendIp = %s,recvBuf=%s\n", inet_ntoa(from.sin_addr), databuf);

    }

    return 0;
}

  1. 组播服务端
    .cpp文件
#include "../struct.h"



int InitSocket()
{   
#ifdef WIN32
    int Error;
    WORD VersionRequested;
    WSADATA WsaData;
    VersionRequested=MAKEWORD(2,2);
    Error=WSAStartup(VersionRequested,&WsaData); //启动WinSock2
    if(Error!=0)
    {
        return 0;
    }
    else
    {
        if(LOBYTE(WsaData.wVersion)!=2||HIBYTE(WsaData.wHighVersion)!=2)
        {
            WSACleanup();
            return 0;
        }       
    }
#endif

    return 0;
}


int main (int argc, char *argv[ ])
{
    InitSocket();

    //设置socket
    struct in_addr localInterface;
    struct sockaddr_in groupSock;
    char bufSend[BUFSIZ];
    int sd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sd < 0) 
    {
        perror("Opening datagram socket error");
        exit(1);
    }   
    memset((char *) &groupSock, 0, sizeof(groupSock));
    groupSock.sin_family = AF_INET;
    groupSock.sin_addr.s_addr = inet_addr(GROUP_IP1);//GROUP_IP 第一个字节不超过239 后面3个字节用来区别分组 不是掩码
    groupSock.sin_port = htons(PORT);
    localInterface.s_addr = inet_addr(LOCAL_IP);//自己的ip,可以和目标不在一个网,但设置不同的网段。

    //设置组播
    if(setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, (char *)&localInterface, sizeof(localInterface)) < 0) 
    {
        perror("Setting local interface error");
        exit(1);
    }
    else
    {
        printf("Setting the local interface...OK\n");
    }


    for (int i=0;;i++)
    {       
        //Send(bufSend, sizeof(bufSend));               
        sprintf(bufSend, "%d", i);
        sendto(sd, bufSend, sizeof(bufSend), 0, (struct sockaddr*)&groupSock, sizeof(groupSock));       
        printf("group=%s,locolip=%s,send=%s\n", GROUP_IP1, LOCAL_IP, bufSend);
        Sleep(1000);                         
    }   
    return 0;
}


阅读更多
文章标签: 组播-C++
个人分类: C++
想对作者说点什么? 我来说一句

如何使用vc实现组播

2009年05月08日 26KB 下载

没有更多推荐了,返回首页

不良信息举报

组播编程

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭