/** * Clase MulticastClient *@author Diego del Valle *@author Diego Gonzalez */ import java.io.*; import java.net.*; import java.util.*; import javax.swing.*; /**Clase que implementa cliente multicast que recepcionara informacion enviada por *servidor multicast, con el objetivo de dibujar en pantalla una figura rebotando *por las pantallas de los clientes */ public class MulticastClient { private static final String MCAST_ADDR = "230.0.0.1"; private static final int MCAST_PORT = 9013; private static final int DGRAM_BUF_LEN = 512; private static final int NO_CLIENT = 200; private Dibujo dibujo; private static int num_client = NO_CLIENT; /**Unico metodo de la clase que implementa a cliente aceptando conexion y recibiendo * datos de servidor */ public static void main(String[] args){ String msg = "Hello "; InetAddress group = null; ClientCoordinates crd = null; EditorFrame frame = null; try { group = InetAddress.getByName(MCAST_ADDR); } catch(UnknownHostException e) { e.printStackTrace(); System.exit(1); } try { MulticastSocket socket = new MulticastSocket(MCAST_PORT); socket.joinGroup(group); DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(), group, MCAST_PORT); System.out.println("Requesting connection to server..."); System.out.println("Sending: " + msg); socket.send(hi); boolean flag = true; while (flag) { byte[] buf = new byte[DGRAM_BUF_LEN]; DatagramPacket recv = new DatagramPacket(buf, buf.length); socket.receive(recv); byte[] data = recv.getData(); String result[] = (new String(data)).split("\\s"); if(result[0].equals("Welcome")) { if (num_client == NO_CLIENT && Integer.parseInt(result[1]) == Integer.parseInt(result[2])) { num_client = Integer.parseInt(result[1]); System.out.println("Requested connection accepted by server..."); System.out.println("Initial configuration data receiving: " + result[1] + " " + result[2] + " " + result[5] + " " + result[6] + " " + result[7] + " " + result[8] + " " + result[9] + " "+ result[10]); System.out.println("Creating window enviroment with width: " + Integer.parseInt(result[3]) + ", heigt: " + Integer.parseInt(result[4])); frame = new EditorFrame(Integer.parseInt(result[3]), Integer.parseInt(result[4])); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(Integer.parseInt(result[3]), Integer.parseInt(result[4])); frame.setLocation(Integer.parseInt(result[5]),Integer.parseInt(result[6])); frame.show(); crd = new ClientCoordinates(result[3], result[4], result[5], result[6], result[7], result[8], result[9], result[10], frame.getDibujo(), result[11]); flag = false; } } } System.out.println("...Client starting receiving data position from server..."); while (true) { byte[] buf = new byte[DGRAM_BUF_LEN]; DatagramPacket recv = new DatagramPacket(buf, buf.length); socket.receive(recv); byte[] data = recv.getData(); String result[] = (new String(data)).split("\\s"); if(result[0].equals("Welcome")) { if (num_client == Integer.parseInt(result[1])) { System.out.println("Receiving border modifying data from server: up: " + result[7] + " down: " + result[8] + " left: " + result[9] + " rigth: " + result[10]); crd.set(result[7], result[8], result[9], result[10]); } } else if(!result[0].equals("Hello")) { int heigth = (new Integer(result[1])).intValue(); int width = (new Integer(result[0])).intValue(); int circulo = (new Integer(result[2])).intValue(); crd.changeCoordinates(width, heigth, circulo); } } } catch(IOException e) { e.printStackTrace(); System.exit(2); } } }