hola me gustaria saber como enviar un paquete de un servidor a varios clientes al mismo tiempo en el protocolo udp.
gracias de antemano.
tego estos codigos me gustaria saber como modificarlos para enviar a n clientes:
servidor
/********************************************************/
package servidorudp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.UnknownHostException;
public class Servidorudp {
public static void main(String[] args) throws UnknownHostException, IOException {
DatagramSocket s= null;
DatagramPacket recibep;
byte buffer[]=new byte[100];
int puertorecep = 4567;
try {
s = new DatagramSocket(puertorecep);
} catch (Exception e) {
System.out.println("error- "+e.toString());
}
System.out.println("esperando mensajes");
while(true){
recibep=new DatagramPacket(buffer, 100);
try {
s.receive(recibep);
} catch (Exception e) {
System.out.println("error- "+e.toString());
}
String datos=new String(recibep.getData());
System.out.println(recibep.getAddress()+":"+recibep.getPort()+"mensaje: "+datos+" "+recibep.getLength());
}
}
}
cliente
/********************************************************/
package cliente_udp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class cliente {
public static void main(String[] args) throws UnknownHostException, IOException {
DatagramSocket s= null;
DatagramPacket enviap;
int puertoenvio=4567;
int puertorecep=4325;
byte buffer[]=new byte[100];
try {
s=new DatagramSocket(puertorecep);
} catch (Exception e) {
System.out.println("error"+e.toString());
System.exit(-1);
}
InetAddress ip=null;
ip=InetAddress.getByName("localhost");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String a;
while(true){
System.out.println("texto: ");
a=br.readLine();
a.getBytes(0,a.length(),buffer,0);
enviap=new DatagramPacket(buffer, a.length(),ip,puertoenvio);
try {
s.send(enviap);
} catch (Exception e) {
System.out.println("error- "+e.toString());
break;
}
}
}
}