This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

BSD socket Udp multicast

Hello,

do anyone have experience with BSD socket? I am using LPC1788. What i want to do is to receive a string or data from certain IpAddress with ist port.
Example: I want to receive data only from IP: 239,127.0.1 Port 60001 and IP: 239,127.0.2 Port 60002.
Look at the code example:

uint8_t uni_ip1[IP4_ADDR_LEN] = {192,168,50,73};
uint8_t mcast_ip1[IP4_ADDR_LEN] = {239,127,0,1};
uint8_t mcast_ip2[IP4_ADDR_LEN] = {239,127,0,2};
void bsd_thread(void const *arg)
{
        SOCKADDR_IN addr;
        SOCKADDR_IN addr2;
        SOCKADDR_IN recaddr;

        int sock,res,addrlen;
        int sock2;
        int bind_status;
        int port_num=1001;
        char dbuf[200];
        char sendebuf[10] = "HELLO";
        sock = socket(AF_INET,SOCK_DGRAM,0);
        printf("sock_status = %i \n", sock);
        addr.sin_port        = htons(port_num);
        addr.sin_family      = PF_INET;

        addr.sin_addr.s_b1 = uni_ip1[0];
        addr.sin_addr.s_b2 = uni_ip1[1];
        addr.sin_addr.s_b3 = uni_ip1[2];
        addr.sin_addr.s_b4 = uni_ip1[3];

        bind_status = bind (sock, (SOCKADDR *)&addr, sizeof(addr));
        printf("bind_status = %i \n", bind_status);

        //Here i start to bind multicast adress with port
        sock2 = socket(AF_INET,SOCK_DGRAM,0);
        printf("sock_status2 = %i \n", sock);
        addr2.sin_port        = htons(60001);
        addr2.sin_family      = PF_INET;

        addr2.sin_addr.s_b1 = mcast_ip1[0];
        addr2.sin_addr.s_b2 = mcast_ip1[1];
        addr2.sin_addr.s_b3 = mcast_ip1[2];
        addr2.sin_addr.s_b4 = mcast_ip1[3];

        bind_status = bind (sock2, (SOCKADDR *)&addr2, sizeof(addr2));
        printf("bind_status2 = %i \n", bind_status);
        printf("addr2.sin_port = %i \r\n",ntohs(addr2.sin_port));
        printf("addr2[%i . %i . %i . %i], \n",addr2.sin_addr.s_b1,addr2.sin_addr.s_b2,addr2.sin_addr.s_b3,addr2.sin_addr.s_b4);

        /*Here i join into 2 multicast adresse*/
        igmp_join(0,mcast_ip1);
        igmp_join(0,mcast_ip2);

        while(1)
        {

                while(1)
                {
                        addrlen = sizeof (addr);
                        res = recvfrom (sock, dbuf, sizeof (dbuf), MSG_DONTWAIT , (SOCKADDR *)&recaddr, &addrlen);
                        printf("recvfrom = %i \r\n", res);
                        if(res >0 )
                        {
                                printf("receive good \r\n");
                                printf("%s\n",dbuf);
                                printf("recvfrom = %i \r\n", res);
                        }

                        res = sendto (sock, (char *)&sendebuf, strlen(sendebuf), 0, (SOCKADDR *)&addr, sizeof (addr));
                        printf("sendto = %i \r\n", res);
                        if (res > 0)
                        {
                                printf("send good \r\n");
                        }
                        osDelay (100);
                        printf("\n");
                        break;
                }
        }
}
#define BSDTHREAD_STACKSIZE (1024)
osThreadDef (bsd_thread, osPriorityNormal , 1, BSDTHREAD_STACKSIZE);



The Problem is, if i send data to mcast_ip1 port 60001, i get the data, and if i send to ,mcast_ip2 port 60001 I also get the data. Even i only Bind between mcast_ip1 and port 60001.
Can anyone help how can i get the data only from mcast_ip1 port 60001 and mcast_ip2 port 60002.
Thank you for helps

Best Regards

Parents
  • Hello,

    do anyone have experience with BSD socket? I am using LPC1788. What i want to do is to receive a string or data from certain IpAddress with its port.
    I want to receive data from two IP-Adress and Port-Number separately:
    IP: 239,127.0.1 Port 60001
    IP: 239,127.0.2 Port 60002
    Look at the code example:

    uint8_t uni_ip1[IP4_ADDR_LEN] = {192,168,50,73};
    uint8_t mcast_ip1[IP4_ADDR_LEN] = {239,127,0,1};
    uint8_t mcast_ip2[IP4_ADDR_LEN] = {239,127,0,2};
    void bsd_thread(void const *arg)
    {
            SOCKADDR_IN addr;
            SOCKADDR_IN addr2;
            SOCKADDR_IN recaddr;
    
            int sock,res,addrlen;
            int sock2;
            int bind_status;
            int port_num=1001;
            char dbuf[200];
            char sendebuf[10] = "HELLO";
            sock = socket(AF_INET,SOCK_DGRAM,0);
            printf("sock_status = %i \n", sock);
            addr.sin_port        = htons(port_num);
            addr.sin_family      = PF_INET;
    
            addr.sin_addr.s_b1 = uni_ip1[0];
            addr.sin_addr.s_b2 = uni_ip1[1];
            addr.sin_addr.s_b3 = uni_ip1[2];
            addr.sin_addr.s_b4 = uni_ip1[3];
    
            bind_status = bind (sock, (SOCKADDR *)&addr, sizeof(addr));
            printf("bind_status = %i \n", bind_status);
    
            //Here i start to bind multicast adress with port
            sock2 = socket(AF_INET,SOCK_DGRAM,0);
            printf("sock_status2 = %i \n", sock);
            addr2.sin_port        = htons(60001);
            addr2.sin_family      = PF_INET;
    
            addr2.sin_addr.s_b1 = mcast_ip1[0];
            addr2.sin_addr.s_b2 = mcast_ip1[1];
            addr2.sin_addr.s_b3 = mcast_ip1[2];
            addr2.sin_addr.s_b4 = mcast_ip1[3];
    
            bind_status = bind (sock2, (SOCKADDR *)&addr2, sizeof(addr2));
            printf("bind_status2 = %i \n", bind_status);
            printf("addr2.sin_port = %i \r\n",ntohs(addr2.sin_port));
            printf("addr2[%i . %i . %i . %i], \n",addr2.sin_addr.s_b1,addr2.sin_addr.s_b2,addr2.sin_addr.s_b3,addr2.sin_addr.s_b4);
    
            /*Here i join into 2 multicast adresse*/
            igmp_join(0,mcast_ip1);
            igmp_join(0,mcast_ip2);
    
            while(1)
            {
    
                    while(1)
                    {
                            addrlen = sizeof (addr);
                            res = recvfrom (sock, dbuf, sizeof (dbuf), MSG_DONTWAIT , (SOCKADDR *)&recaddr, &addrlen);
                            printf("recvfrom = %i \r\n", res);
                            if(res >0 )
                            {
                                    printf("receive good \r\n");
                                    printf("%s\n",dbuf);
                                    printf("recvfrom = %i \r\n", res);
                            }
    
                            res = sendto (sock, (char *)&sendebuf, strlen(sendebuf), 0, (SOCKADDR *)&addr, sizeof (addr));
                            printf("sendto = %i \r\n", res);
                            if (res > 0)
                            {
                                    printf("send good \r\n");
                            }
                            osDelay (100);
                            printf("\n");
                            break;
                    }
            }
    }
    #define BSDTHREAD_STACKSIZE (1024)
    osThreadDef (bsd_thread, osPriorityNormal , 1, BSDTHREAD_STACKSIZE);
    

    to receive from both IP-Multicastaddress, i used the function:
    igmp_join(0,mcast_ip1);
    igmp_join(0,mcast_ip2);

    The problem is, i receive data from both IP_adress with the port-Number 60001 and it doesn't matter which IP-Adress i used to send data.

    Can anyone help how can i get the data only from specific IP-Adress and PORT-Number, Which the Data are sent to.
    Thank you for help

    Best Regards

Reply
  • Hello,

    do anyone have experience with BSD socket? I am using LPC1788. What i want to do is to receive a string or data from certain IpAddress with its port.
    I want to receive data from two IP-Adress and Port-Number separately:
    IP: 239,127.0.1 Port 60001
    IP: 239,127.0.2 Port 60002
    Look at the code example:

    uint8_t uni_ip1[IP4_ADDR_LEN] = {192,168,50,73};
    uint8_t mcast_ip1[IP4_ADDR_LEN] = {239,127,0,1};
    uint8_t mcast_ip2[IP4_ADDR_LEN] = {239,127,0,2};
    void bsd_thread(void const *arg)
    {
            SOCKADDR_IN addr;
            SOCKADDR_IN addr2;
            SOCKADDR_IN recaddr;
    
            int sock,res,addrlen;
            int sock2;
            int bind_status;
            int port_num=1001;
            char dbuf[200];
            char sendebuf[10] = "HELLO";
            sock = socket(AF_INET,SOCK_DGRAM,0);
            printf("sock_status = %i \n", sock);
            addr.sin_port        = htons(port_num);
            addr.sin_family      = PF_INET;
    
            addr.sin_addr.s_b1 = uni_ip1[0];
            addr.sin_addr.s_b2 = uni_ip1[1];
            addr.sin_addr.s_b3 = uni_ip1[2];
            addr.sin_addr.s_b4 = uni_ip1[3];
    
            bind_status = bind (sock, (SOCKADDR *)&addr, sizeof(addr));
            printf("bind_status = %i \n", bind_status);
    
            //Here i start to bind multicast adress with port
            sock2 = socket(AF_INET,SOCK_DGRAM,0);
            printf("sock_status2 = %i \n", sock);
            addr2.sin_port        = htons(60001);
            addr2.sin_family      = PF_INET;
    
            addr2.sin_addr.s_b1 = mcast_ip1[0];
            addr2.sin_addr.s_b2 = mcast_ip1[1];
            addr2.sin_addr.s_b3 = mcast_ip1[2];
            addr2.sin_addr.s_b4 = mcast_ip1[3];
    
            bind_status = bind (sock2, (SOCKADDR *)&addr2, sizeof(addr2));
            printf("bind_status2 = %i \n", bind_status);
            printf("addr2.sin_port = %i \r\n",ntohs(addr2.sin_port));
            printf("addr2[%i . %i . %i . %i], \n",addr2.sin_addr.s_b1,addr2.sin_addr.s_b2,addr2.sin_addr.s_b3,addr2.sin_addr.s_b4);
    
            /*Here i join into 2 multicast adresse*/
            igmp_join(0,mcast_ip1);
            igmp_join(0,mcast_ip2);
    
            while(1)
            {
    
                    while(1)
                    {
                            addrlen = sizeof (addr);
                            res = recvfrom (sock, dbuf, sizeof (dbuf), MSG_DONTWAIT , (SOCKADDR *)&recaddr, &addrlen);
                            printf("recvfrom = %i \r\n", res);
                            if(res >0 )
                            {
                                    printf("receive good \r\n");
                                    printf("%s\n",dbuf);
                                    printf("recvfrom = %i \r\n", res);
                            }
    
                            res = sendto (sock, (char *)&sendebuf, strlen(sendebuf), 0, (SOCKADDR *)&addr, sizeof (addr));
                            printf("sendto = %i \r\n", res);
                            if (res > 0)
                            {
                                    printf("send good \r\n");
                            }
                            osDelay (100);
                            printf("\n");
                            break;
                    }
            }
    }
    #define BSDTHREAD_STACKSIZE (1024)
    osThreadDef (bsd_thread, osPriorityNormal , 1, BSDTHREAD_STACKSIZE);
    

    to receive from both IP-Multicastaddress, i used the function:
    igmp_join(0,mcast_ip1);
    igmp_join(0,mcast_ip2);

    The problem is, i receive data from both IP_adress with the port-Number 60001 and it doesn't matter which IP-Adress i used to send data.

    Can anyone help how can i get the data only from specific IP-Adress and PORT-Number, Which the Data are sent to.
    Thank you for help

    Best Regards

Children
No data