Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package servidorsatelite;
- import java.io.IOException;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.net.SocketException;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.Random;
- /**
- *
- * @author DAM205
- */
- public class ServidorSatelite extends Thread {
- static ServerSocket servidor;
- static final int puerto = 6000;
- static int conexionesActuales = 0;
- static Socket tabla[] = new Socket[10];
- static Parametros parametrosEstacion = new Parametros();
- static Parametros parametrosRover = new Parametros();
- Random coordenadaNEstacion = new Random();
- Random coordenadaEEstacion = new Random();
- Random coordenadaNRover = new Random();
- Random coordenadaERover = new Random();
- int numeroNEstacion, numeroEEstacion;
- int numeroNRover, numeroERover;
- Calendar instante;
- public static void main(String[] args) throws IOException {
- servidor = new ServerSocket(puerto);
- System.out.println("- Servidor satélite operativo -");
- System.out.println("-------------------------------n");
- ServidorSatelite generadorCoordenadas = new ServidorSatelite();
- Thread hiloCoordenadas = new Thread(generadorCoordenadas);
- hiloCoordenadas.start();
- while (conexionesActuales <= 10) {
- Socket s = new Socket();
- try {
- s = servidor.accept();
- } catch (SocketException ns) {
- break;
- }
- tabla[conexionesActuales] = s;
- conexionesActuales++;
- HiloServidor hiloServidor = new HiloServidor(s, parametrosEstacion, parametrosRover);
- hiloServidor.start();
- }
- if(!servidor.isClosed())
- try{
- servidor.close();
- }catch (IOException ioe){
- ioe.printStackTrace();
- }
- System.out.println("Servidor satelite deshabilitado");
- System.out.println("-------------------------------n");
- }
- public void run() {
- while (true) {
- instante = Calendar.getInstance();
- Date momenActual = instante.getTime();
- parametrosEstacion.setInstante(momenActual);
- parametrosRover.setInstante(momenActual);
- System.out.println(parametrosEstacion.getInstante());
- numeroNEstacion = coordenadaNEstacion.nextInt(50);
- parametrosEstacion.setNumeroN(numeroNEstacion);
- System.out.println("Estación punto N: " + parametrosEstacion.getNumeroN());
- numeroEEstacion = coordenadaEEstacion.nextInt(50);
- parametrosEstacion.setNumeroE(numeroEEstacion);
- System.out.println("Estación punto E: " + parametrosEstacion.getNumeroE());
- numeroNRover = coordenadaERover.nextInt(50);
- parametrosRover.setNumeroN(numeroNRover);
- System.out.println("Rover punto N: " + parametrosRover.getNumeroN());
- numeroERover = coordenadaERover.nextInt(50);
- parametrosRover.setNumeroE(numeroERover);
- System.out.println("Rover punto E: " + parametrosRover.getNumeroE());
- System.out.println("");
- try {
- Thread.sleep(5000);
- } catch (Exception e) {
- }
- }
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package servidorsatelite;
- import java.io.*;
- import java.net.*;
- /**
- *
- * @author DAM205
- */
- public class HiloServidor extends Thread {
- Socket socket = null;
- Parametros paramEstacion = new Parametros();
- Parametros paramRover = new Parametros();
- ObjectOutputStream fsalida;
- public HiloServidor(Socket s, Parametros estacion, Parametros rover) {
- socket = s;
- paramEstacion = estacion;
- paramRover = rover;
- }
- public void run() {
- while (true) {
- try {
- System.out.println("Comunico con: " + socket.toString());
- EnviarObjetos(paramEstacion);
- try {
- Thread.sleep(5000);
- } catch (Exception e) {
- }
- } catch (Exception e) {
- e.printStackTrace();
- break;
- }
- }
- }
- private void EnviarObjetos(Parametros paramEstacion) throws IOException {
- int i;
- for (i = 0; i < ServidorSatelite.conexionesActuales; i++) {
- Socket s1 = ServidorSatelite.tabla[i];
- try {
- fsalida = new ObjectOutputStream(s1.getOutputStream());
- fsalida.writeObject(paramEstacion);
- } catch (SocketException se) {
- se.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package servidorsatelite;
- import java.io.Serializable;
- import java.util.Date;
- @SuppressWarnings("serial")
- /**
- *
- * @author DAM205
- */
- public class Parametros implements Serializable {
- int numeroN, numeroE;
- Date instante;
- public Parametros() {
- super();
- }
- public Parametros(int numeroN, int numeroE, Date instante) {
- super();
- this.numeroN = numeroN;
- this.numeroE = numeroE;
- this.instante = instante;
- }
- public int getNumeroN() {
- return numeroN;
- }
- public void setNumeroN(int numeroN) {
- this.numeroN = numeroN;
- }
- public int getNumeroE() {
- return numeroE;
- }
- public void setNumeroE(int numeroE) {
- this.numeroE = numeroE;
- }
- public Date getInstante() {
- return instante;
- }
- public void setInstante(Date instante) {
- this.instante = instante;
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package servidorclientebase;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.net.Socket;
- /**
- *
- * @author iberd
- */
- public class ServidorClienteBase {
- /**
- * @param args the command line arguments
- * @throws java.io.IOException
- * @throws java.lang.ClassNotFoundException
- */
- public static void main(String[] args) throws IOException, ClassNotFoundException {
- String host = "localhost";
- int puerto = 6000;
- Parametros dato = new Parametros();
- System.out.println("Estación base operativa");
- System.out.println("-----------------------n");
- Socket cliente = new Socket(host, puerto);
- //Flujo entrada para objetos
- ObjectInputStream perEnt = new ObjectInputStream(cliente.getInputStream());
- while (true){
- //Se recibe un objeto
- dato = (Parametros) perEnt.readObject();
- }
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package servidorclientebase;
- import java.io.Serializable;
- import java.util.Date;
- @SuppressWarnings("serial")
- /**
- *
- * @author DAM205
- */
- public class Parametros implements Serializable {
- int numeroN, numeroE;
- Date instante;
- public Parametros() {
- super();
- }
- public Parametros(int numeroN, int numeroE, Date instante) {
- super();
- this.numeroN = numeroN;
- this.numeroE = numeroE;
- this.instante = instante;
- }
- public int getNumeroN() {
- return numeroN;
- }
- public void setNumeroN(int numeroN) {
- this.numeroN = numeroN;
- }
- public int getNumeroE() {
- return numeroE;
- }
- public void setNumeroE(int numeroE) {
- this.numeroE = numeroE;
- }
- public Date getInstante() {
- return instante;
- }
- public void setInstante(Date instante) {
- this.instante = instante;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement