Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.24 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package servidorsatelite;
  7.  
  8. import java.io.IOException;
  9. import java.net.ServerSocket;
  10. import java.net.Socket;
  11. import java.net.SocketException;
  12. import java.util.Calendar;
  13. import java.util.Date;
  14. import java.util.Random;
  15.  
  16. /**
  17. *
  18. * @author DAM205
  19. */
  20. public class ServidorSatelite extends Thread {
  21.  
  22. static ServerSocket servidor;
  23. static final int puerto = 6000;
  24. static int conexionesActuales = 0;
  25. static Socket tabla[] = new Socket[10];
  26.  
  27. static Parametros parametrosEstacion = new Parametros();
  28. static Parametros parametrosRover = new Parametros();
  29.  
  30. Random coordenadaNEstacion = new Random();
  31. Random coordenadaEEstacion = new Random();
  32. Random coordenadaNRover = new Random();
  33. Random coordenadaERover = new Random();
  34.  
  35. int numeroNEstacion, numeroEEstacion;
  36. int numeroNRover, numeroERover;
  37.  
  38. Calendar instante;
  39.  
  40. public static void main(String[] args) throws IOException {
  41. servidor = new ServerSocket(puerto);
  42. System.out.println("- Servidor satélite operativo -");
  43. System.out.println("-------------------------------n");
  44.  
  45. ServidorSatelite generadorCoordenadas = new ServidorSatelite();
  46. Thread hiloCoordenadas = new Thread(generadorCoordenadas);
  47. hiloCoordenadas.start();
  48.  
  49. while (conexionesActuales <= 10) {
  50. Socket s = new Socket();
  51. try {
  52. s = servidor.accept();
  53. } catch (SocketException ns) {
  54. break;
  55. }
  56. tabla[conexionesActuales] = s;
  57. conexionesActuales++;
  58. HiloServidor hiloServidor = new HiloServidor(s, parametrosEstacion, parametrosRover);
  59. hiloServidor.start();
  60. }
  61.  
  62. if(!servidor.isClosed())
  63. try{
  64. servidor.close();
  65. }catch (IOException ioe){
  66. ioe.printStackTrace();
  67. }
  68.  
  69. System.out.println("Servidor satelite deshabilitado");
  70. System.out.println("-------------------------------n");
  71.  
  72. }
  73.  
  74. public void run() {
  75. while (true) {
  76. instante = Calendar.getInstance();
  77. Date momenActual = instante.getTime();
  78. parametrosEstacion.setInstante(momenActual);
  79. parametrosRover.setInstante(momenActual);
  80. System.out.println(parametrosEstacion.getInstante());
  81.  
  82. numeroNEstacion = coordenadaNEstacion.nextInt(50);
  83. parametrosEstacion.setNumeroN(numeroNEstacion);
  84. System.out.println("Estación punto N: " + parametrosEstacion.getNumeroN());
  85.  
  86. numeroEEstacion = coordenadaEEstacion.nextInt(50);
  87. parametrosEstacion.setNumeroE(numeroEEstacion);
  88. System.out.println("Estación punto E: " + parametrosEstacion.getNumeroE());
  89.  
  90. numeroNRover = coordenadaERover.nextInt(50);
  91. parametrosRover.setNumeroN(numeroNRover);
  92. System.out.println("Rover punto N: " + parametrosRover.getNumeroN());
  93.  
  94. numeroERover = coordenadaERover.nextInt(50);
  95. parametrosRover.setNumeroE(numeroERover);
  96. System.out.println("Rover punto E: " + parametrosRover.getNumeroE());
  97. System.out.println("");
  98.  
  99. try {
  100. Thread.sleep(5000);
  101. } catch (Exception e) {
  102. }
  103. }
  104. }
  105.  
  106. }
  107.  
  108. /*
  109. * To change this license header, choose License Headers in Project Properties.
  110. * To change this template file, choose Tools | Templates
  111. * and open the template in the editor.
  112. */
  113. package servidorsatelite;
  114.  
  115. import java.io.*;
  116. import java.net.*;
  117.  
  118. /**
  119. *
  120. * @author DAM205
  121. */
  122. public class HiloServidor extends Thread {
  123.  
  124. Socket socket = null;
  125. Parametros paramEstacion = new Parametros();
  126. Parametros paramRover = new Parametros();
  127.  
  128. ObjectOutputStream fsalida;
  129.  
  130. public HiloServidor(Socket s, Parametros estacion, Parametros rover) {
  131. socket = s;
  132. paramEstacion = estacion;
  133. paramRover = rover;
  134. }
  135.  
  136. public void run() {
  137. while (true) {
  138. try {
  139. System.out.println("Comunico con: " + socket.toString());
  140. EnviarObjetos(paramEstacion);
  141. try {
  142. Thread.sleep(5000);
  143. } catch (Exception e) {
  144. }
  145. } catch (Exception e) {
  146. e.printStackTrace();
  147. break;
  148. }
  149. }
  150. }
  151.  
  152. private void EnviarObjetos(Parametros paramEstacion) throws IOException {
  153. int i;
  154. for (i = 0; i < ServidorSatelite.conexionesActuales; i++) {
  155. Socket s1 = ServidorSatelite.tabla[i];
  156.  
  157. try {
  158. fsalida = new ObjectOutputStream(s1.getOutputStream());
  159. fsalida.writeObject(paramEstacion);
  160. } catch (SocketException se) {
  161. se.printStackTrace();
  162. } catch (IOException e) {
  163. e.printStackTrace();
  164. }
  165. }
  166. }
  167. }
  168.  
  169. /*
  170. * To change this license header, choose License Headers in Project Properties.
  171. * To change this template file, choose Tools | Templates
  172. * and open the template in the editor.
  173. */
  174. package servidorsatelite;
  175.  
  176. import java.io.Serializable;
  177. import java.util.Date;
  178.  
  179. @SuppressWarnings("serial")
  180.  
  181. /**
  182. *
  183. * @author DAM205
  184. */
  185. public class Parametros implements Serializable {
  186.  
  187. int numeroN, numeroE;
  188. Date instante;
  189.  
  190. public Parametros() {
  191. super();
  192. }
  193.  
  194. public Parametros(int numeroN, int numeroE, Date instante) {
  195. super();
  196. this.numeroN = numeroN;
  197. this.numeroE = numeroE;
  198. this.instante = instante;
  199. }
  200.  
  201. public int getNumeroN() {
  202. return numeroN;
  203. }
  204.  
  205. public void setNumeroN(int numeroN) {
  206. this.numeroN = numeroN;
  207. }
  208.  
  209. public int getNumeroE() {
  210. return numeroE;
  211. }
  212.  
  213. public void setNumeroE(int numeroE) {
  214. this.numeroE = numeroE;
  215. }
  216.  
  217. public Date getInstante() {
  218. return instante;
  219. }
  220.  
  221. public void setInstante(Date instante) {
  222. this.instante = instante;
  223. }
  224. }
  225.  
  226. /*
  227. * To change this license header, choose License Headers in Project Properties.
  228. * To change this template file, choose Tools | Templates
  229. * and open the template in the editor.
  230. */
  231. package servidorclientebase;
  232.  
  233. import java.io.IOException;
  234. import java.io.ObjectInputStream;
  235. import java.net.Socket;
  236.  
  237. /**
  238. *
  239. * @author iberd
  240. */
  241. public class ServidorClienteBase {
  242.  
  243. /**
  244. * @param args the command line arguments
  245. * @throws java.io.IOException
  246. * @throws java.lang.ClassNotFoundException
  247. */
  248. public static void main(String[] args) throws IOException, ClassNotFoundException {
  249. String host = "localhost";
  250. int puerto = 6000;
  251. Parametros dato = new Parametros();
  252.  
  253. System.out.println("Estación base operativa");
  254. System.out.println("-----------------------n");
  255. Socket cliente = new Socket(host, puerto);
  256.  
  257. //Flujo entrada para objetos
  258. ObjectInputStream perEnt = new ObjectInputStream(cliente.getInputStream());
  259.  
  260. while (true){
  261. //Se recibe un objeto
  262. dato = (Parametros) perEnt.readObject();
  263. }
  264. }
  265.  
  266. }
  267.  
  268. /*
  269. * To change this license header, choose License Headers in Project Properties.
  270. * To change this template file, choose Tools | Templates
  271. * and open the template in the editor.
  272. */
  273. package servidorclientebase;
  274.  
  275. import java.io.Serializable;
  276. import java.util.Date;
  277.  
  278. @SuppressWarnings("serial")
  279.  
  280. /**
  281. *
  282. * @author DAM205
  283. */
  284. public class Parametros implements Serializable {
  285.  
  286. int numeroN, numeroE;
  287. Date instante;
  288.  
  289. public Parametros() {
  290. super();
  291. }
  292.  
  293. public Parametros(int numeroN, int numeroE, Date instante) {
  294. super();
  295. this.numeroN = numeroN;
  296. this.numeroE = numeroE;
  297. this.instante = instante;
  298. }
  299.  
  300. public int getNumeroN() {
  301. return numeroN;
  302. }
  303.  
  304. public void setNumeroN(int numeroN) {
  305. this.numeroN = numeroN;
  306. }
  307.  
  308. public int getNumeroE() {
  309. return numeroE;
  310. }
  311.  
  312. public void setNumeroE(int numeroE) {
  313. this.numeroE = numeroE;
  314. }
  315.  
  316. public Date getInstante() {
  317. return instante;
  318. }
  319.  
  320. public void setInstante(Date instante) {
  321. this.instante = instante;
  322. }
  323.  
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement