Guest User

Untitled

a guest
Feb 18th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. public class Servidor extends Thread {
  2.  
  3. static final int port = 49000;
  4. Socket skCliente;
  5.  
  6. public Servidor(Socket sCliente) {
  7. skCliente = sCliente;
  8. }
  9.  
  10. public static void main(String[]args){
  11. try {
  12.  
  13. ServerSocket skServidor = new ServerSocket(port);
  14. System.out.println("Servidor escuchando en el puerto " + port);
  15.  
  16. while(true){
  17. Socket skCliente = skServidor.accept();
  18. System.out.println("¡¡¡Cliente Conectado!!!");
  19. new Servidor(skCliente).start();
  20.  
  21. }
  22.  
  23.  
  24.  
  25. } catch (Exception e) {
  26. System.out.println("Error de conexión");
  27. }
  28. }
  29.  
  30. @Override
  31. public void run() {
  32. try {
  33. OutputStream aux = skCliente.getOutputStream();
  34. DataOutputStream flujo_salida = new DataOutputStream(aux);
  35.  
  36. InputStream auxi = skCliente.getInputStream();
  37. DataInputStream flujo_entrada = new DataInputStream(auxi);
  38.  
  39. flujo_salida.writeUTF("¡¡¡Conexión satisfactoria!!!");//Escribe en el Stream para que lo lea el cliente
  40. String comando;
  41. boolean correcto = false;
  42.  
  43. do {
  44.  
  45. flujo_salida.writeUTF("Introduce nombre del archivo: ");
  46. comando=flujo_entrada.readUTF();
  47. if(comando.equals("dam")){
  48. File archivo = new File ("C:\Users\Dani\Desktop\dam.txt");
  49. FileReader fr = new FileReader (archivo);
  50. BufferedReader br = new BufferedReader(fr);
  51. String linea = br.readLine();
  52. flujo_salida.writeUTF("true");
  53. flujo_salida.writeUTF(linea);
  54. correcto = true;
  55.  
  56.  
  57.  
  58.  
  59.  
  60. }else{
  61. flujo_salida.writeUTF("false");
  62. correcto = false;
  63.  
  64.  
  65.  
  66.  
  67. }
  68.  
  69. } while (correcto == false);
  70. flujo_entrada.close();
  71. flujo_salida.close();
  72.  
  73. System.out.println("Cliente desconectado");
  74. skCliente.close();
  75.  
  76. } catch (Exception e) {
  77. System.out.println(e.getMessage());
  78. }
  79. }
  80.  
  81. }
  82.  
  83. public class Cliente {
  84.  
  85.  
  86. public static void main(String[] args) throws IOException {
  87.  
  88. final String ip = "localhost";
  89. final int port = 49000;
  90. String resultado = "false";
  91.  
  92. Scanner sc = new Scanner(System.in);
  93. Socket sCliente = new Socket(ip, port);
  94. //System.out.println("Me he conectado al servidor");
  95.  
  96. InputStream aux = sCliente.getInputStream();
  97. DataInputStream flujo_entrada = new DataInputStream(aux);
  98.  
  99. OutputStream auxi = sCliente.getOutputStream();
  100. DataOutputStream flujo_salida = new DataOutputStream(auxi);
  101.  
  102. System.out.println(flujo_entrada.readUTF());
  103. System.out.print(flujo_entrada.readUTF());
  104.  
  105. String archivo = sc.nextLine();
  106. flujo_salida.writeUTF(archivo);
  107. resultado = flujo_entrada.readUTF();
  108.  
  109. do{
  110. if(resultado.equals("false")){
  111.  
  112.  
  113. System.out.println("No existe el archivo");
  114. System.out.print("Introduzca el nombre del archivo: ");
  115. archivo = sc.nextLine();
  116. flujo_salida.writeUTF(archivo); }
  117.  
  118.  
  119. if(resultado.equals("true")){
  120. System.out.println("Lectura del archivo solicitado: ");
  121. System.out.println(flujo_entrada.readUTF());
  122. System.out.println(flujo_entrada.readUTF());
  123. }
  124. }while (resultado.equals("false"));
  125. sCliente.close();
  126. }
  127.  
  128. }
Add Comment
Please, Sign In to add comment