Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java Multicast with VPN and static route

user12263321Mar 20 2015 — edited Mar 20 2015

Hi everybody!!! I'm using Windows7 x64, jdk 1.8_40. My problem is to make receiving multicast data with from VPN with some static routes that provided by multicast data sender(code in c++ works fine but java =( ).

First of all I make VPN connection without overlaying all connectins NET tab settings -> Deselect Use default gateway on remote network.

Then I make VPN connection and get such route table:

   0.0.0.0          0.0.0.0           10.2.24.1     10.2.24.206     20
  
1.0.0.0          255.0.0.0         1.1.7.250     1.1.5.45        21
  
1.1.1.0          255.255.255.0     On-link       1.1.5.45        21
  
1.1.1.255        255.255.255.255   On-link       1.1.5.45        276
  
1.1.5.45         255.255.255.255   On-link       1.1.5.45        276
  
10.2.24.0        255.255.255.0     On-link       10.2.24.206     276
  
10.2.24.206      255.255.255.255   On-link       10.2.24.206     276
  
10.2.24.255      255.255.255.255   On-link       10.2.24.206     276
  
91.208.232.208   255.255.255.255   10.2.24.1     10.2.24.206   21
  
127.0.0.0        255.0.0.0         On-link       127.0.0.1   306
  
127.0.0.1        255.255.255.255   On-link       127.0.0.1   306
  
127.255.255.255  255.255.255.255   On-link       127.0.0.1   306
  
172.27.129.200   255.255.255.255   On-link       1.1.5.45   21
  
192.168.56.0     255.255.255.0     On-link       192.168.56.1   276
  
192.168.56.1     255.255.255.255   On-link       192.168.56.1   276
  
192.168.56.255   255.255.255.255   On-link       192.168.56.1   276
  
224.0.0.0        240.0.0.0         On-link       127.0.0.1   306
  
224.0.0.0        240.0.0.0         On-link       192.168.56.1   276
  
224.0.0.0        240.0.0.0         On-link       10.2.24.206   276
  
224.0.0.0        240.0.0.0         On-link       1.1.5.45   276
  
239.192.0.0      255.255.0.0       On-link       1.1.5.45   21
  
255.255.255.255  255.255.255.255   On-link       127.0.0.1   306
  
255.255.255.255  255.255.255.255   On-link       192.168.56.1   276
  
255.255.255.255  255.255.255.255   On-link       10.2.24.206   276
  
255.255.255.255  255.255.255.255   On-link       1.1.5.45   276

And then I'm executing this:

route add 1.1.1.0 mask 255.255.255.0
route add
239.192.0.0 mask 255.255.0.0
route add
172.27.129.200 mask 255.255.255.255 


After all this I can execute c++ program with parameters 239.192.113.3 172.27.129.200 9113

#include <winsock2.h>

#include <Ws2tcpip.h>

#include <mswsock.h>

#include <stdio.h>

#include <stdlib.h>

int join_ssm_group(int s, UINT32 group, UINT32 source, UINT32 inter) {

   struct ip_mreq_source imr;

   imr.imr_multiaddr.s_addr  = group;

   imr.imr_sourceaddr.s_addr = source;

   imr.imr_interface.s_addr  = inter;

   return setsockopt(s, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP, (char *) &imr, sizeof(imr));

}

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

  

    if (argc<3) {

        printf(" Use: %s <group> <source> <port>", argv[0]);

        return 1;

    }

  

    // Init WinSock

    WSAData data;

    WSAStartup( MAKEWORD( 2, 2 ), &data );

    //  Make socket

    int sd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);

  

    sockaddr_in Sender;

    int SenderAddrSize = sizeof( Sender );

  

    struct sockaddr_in binda;

  

    //  Bind it to listen appropriate UDP port

    binda.sin_family = AF_INET;

    binda.sin_port = htons( atoi(argv[3]));

    binda.sin_addr.s_addr = htonl(INADDR_ANY);

    bind(sd,(struct sockaddr*)&binda, sizeof(binda));

    // Join to group

    join_ssm_group( sd, inet_addr(argv[1]),

                        inet_addr(argv[2]),

                        INADDR_ANY );

      

    char        buf[65536];

    UINT32      seq;

  

    while(1) {

        int res=recvfrom(sd,(char*)buf,sizeof(buf),0, (SOCKADDR *)& Sender, &SenderAddrSize);

        seq = *(UINT32*)buf;

        printf("%12s:seq=%6d:len=%4d\n", inet_ntoa(Sender.sin_addr), seq, res);

    }

  

    return 0;

}

    while(1) {

        int res=recvfrom(sd,(char*)buf,sizeof(buf),0, (SOCKADDR *)& Sender, &SenderAddrSize);

        seq = *(UINT32*)buf;

        printf("%12s:seq=%6d:len=%4d\n", inet_ntoa(Sender.sin_addr), seq, res);

    }

  

    return 0;

}

And the code works FINE!!

But when I make this in java i get error:

Cannot assign requested address: Cannot bind java.net.BindException: Cannot assign requested address:

Cannot bind at java.net.TwoStacksPlainDatagramSocketImpl.bind0(Native Method) ~[na:1.8.0_25]

at java.net.TwoStacksPlainDatagramSocketImpl.bind0(TwoStacksPlainDatagramSocketImpl.java:107) ~[na:1.8.0_25]

at java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:94) ~[na:1.8.0_25]

at java.net.TwoStacksPlainDatagramSocketImpl.bind(TwoStacksPlainDatagramSocketImpl.java:97) ~[na:1.8.0_25]

at java.net.DatagramSocket.bind(DatagramSocket.java:392) ~[na:1.8.0_25]

at java.net.MulticastSocket.(MulticastSocket.java:172) ~[na:1.8.0_25]


try {

        final byte[] G = {(byte)239, (byte)192, (byte)113, (byte)3};

        final byte[] L = {(byte)172, (byte)27, (byte)129, (byte)200};

        int port = 9113;

        // Which address

        //String group = "239.192.113.3"; // "239.192.113.3"

        // Create the socket and bind it to port 'port'.

        MulticastSocket s = new MulticastSocket(new InetSocketAddress(InetAddress.getByAddress(L), 9113)); // "172.27.129.200"

        // join the multicast group

        s.joinGroup(InetAddress.getByAddress(G));

        // Now the socket is set up and we are ready to receive packets

        // Create a DatagramPacket and do a receive

        byte buf[] = new byte[1024];

        DatagramPacket pack = new DatagramPacket(buf, buf.length);

        s.receive(pack);

        // Finally, let us do something useful with the data we just received,

        // like print it on stdout :-)

        System.out.println("Received data from: " + pack.getAddress().toString() +

                ":" + pack.getPort() + " with length: " +

                pack.getLength());

        System.out.write(pack.getData(), 0, pack.getLength());

        System.out.println();

        // And when we have finished receiving data leave the multicast group and

        // close the socket

        s.leaveGroup(InetAddress.getByName(group));

        s.close();

    } catch (Exception ex) {

        log.error(ex.getMessage(), ex);

    }

PLEASE!!! Help!

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 17 2015
Added on Mar 20 2015
0 comments
548 views