4

can anyone show me an example in java to receive data from DatagramSocket and sending same data through Multicast Socket

evan
  • 1,120
  • 4
  • 13
  • 32

2 Answers2

17

Sending multicast datagrams

In order to send any kind of datagram in Java, be it unicast, broadcast or multicast, one needs a java.net.DatagramSocket:

DatagramSocket socket = new DatagramSocket();

One can optionally supply a local port to the DatagramSocket constructor to which the socket must bind. This is only necessary if one needs other parties to be able to reach us at a specific port. A third constructor takes the local port AND the local IP address to which to bind. This is used (rarely) with multi-homed hosts where it is important on which network adapter the traffic is received.

 DatagramSocket socket = new DatagramSocket();

byte[] b = new byte[DGRAM_LENGTH];
DatagramPacket dgram;

dgram = new DatagramPacket(b, b.length,
  InetAddress.getByName(MCAST_ADDR), DEST_PORT);

System.err.println("Sending " + b.length + " bytes to " +
  dgram.getAddress() + ':' + dgram.getPort());
while(true) {
  System.err.print(".");
  socket.send(dgram);
  Thread.sleep(1000);
}

Receiving multicast datagrams

One can use a normal DatagramSocket to send and receive unicast and broadcast datagrams and to send multicast datagrams. In order to receive multicast datagrams, however, one needs a MulticastSocket. The reason for this is simple, additional work needs to be done to control and receive multicast traffic by all the protocol layers below UDP.

byte[] b = new byte[BUFFER_LENGTH];
DatagramPacket dgram = new DatagramPacket(b, b.length);
MulticastSocket socket =
  new MulticastSocket(DEST_PORT); // must bind receive side
socket.joinGroup(InetAddress.getByName(MCAST_ADDR));

while(true) {
  socket.receive(dgram); // blocks until a datagram is received
  System.err.println("Received " + dgram.getLength() +
    " bytes from " + dgram.getAddress());
  dgram.setLength(b.length); // must reset length field!
}

For more Information:

Destroyica
  • 4,147
  • 3
  • 33
  • 50
Mohamed Saligh
  • 12,029
  • 19
  • 65
  • 84
  • @M-Razavi There is no such thing as a 'broadcast group'. Broadcast and multicast are different things. The question is about multicast, as is this answer. If you have a question of your own, ask it, *as a question*. Otherwise nobody will see it for four years. – user207421 Jul 03 '19 at 11:25
2

You've got that back to front. You receive multicasts through a MulticastSocket, but you don't need to send them that way: you can send them via a DatagramSocket.

See the Java Tutorial, Custom Networking trail.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    I am not sure how true the above comment holds. I wasn't able to send multicast packet via a normal DatagramSocket. Only when I started to use a MulticastSocket on the server side, it started showing me the outgoing traffic on sniffing the packets at the server end. – Binita Bharati Feb 08 '16 at 06:10
  • @BinitaBharati Anecdotes are not evidence. You need to produce some code. I've been doing what I described here for 20 years. – user207421 Jul 03 '19 at 11:23
  • Did not mean to judge your knowledge/expertise. I had faced this in my case, while working on my own project. I do have the project open sourced and on github, but, I do not think, that I would have checked in the offending code to git as well. I should have updated my comment with the non working code, which, I unfortunately did not at that point of time. – Binita Bharati Dec 12 '19 at 10:06