Skip to content

Instantly share code, notes, and snippets.

@casidiablo
Created March 4, 2012 17:24
Show Gist options
  • Save casidiablo/1973987 to your computer and use it in GitHub Desktop.
Save casidiablo/1973987 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
/**
* @author cristian
*/
public class MultiServer {
public static void main(String[] args) throws IOException {
byte[] buf = new byte[256];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
MulticastSocket socket = new MulticastSocket(4445);
// las direcciones multicast rango: 224.0.0.1 a 239.255.255.255
socket.joinGroup(InetAddress.getByName("239.255.255.255"));
socket.receive(packet);
String recibido = new String(packet.getData());
System.out.println("llego paquete: " + recibido);
InetAddress address = packet.getAddress();
int port = packet.getPort();
packet = new DatagramPacket(buf, buf.length, address, port);
socket.send(packet);
socket.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment