Advertisement
rfop

Talya1.0

Nov 14th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.96 KB | None | 0 0
  1. import Cliente.Cliente;
  2. import servidor.Servidor;
  3.  
  4. import java.io.IOException;
  5. import java.util.Scanner;
  6. @SuppressWarnings("unused")
  7. public class Talya {
  8.  
  9.     public static void main(String[] args) {
  10.         String adress; //192.168.25.9
  11.         int porta;
  12.         String escolha;
  13.         Scanner in = new Scanner(System.in);
  14.         Scanner str = new Scanner(System.in);
  15.         System.out.println("Servidor ou cliente? S ou C");
  16.         escolha = str.nextLine();
  17.         System.out.println("endereรงo ip: ");
  18.         adress = str.nextLine();
  19.         System.out.println("porta: ");
  20.         porta = in.nextInt();
  21.         if(escolha.equals("S")) {
  22.             Servidor servidor = new Servidor(porta);
  23.             servidor.iniciaConexaoServidor();
  24.             String diretorioServidor = "TesteMusic.rar";
  25.             try {
  26.                 servidor.enviarArquivo(diretorioServidor);
  27.             } catch (Exception e) {
  28.                 // TODO Auto-generated catch block
  29.                 e.printStackTrace();
  30.             }
  31.         } else {
  32.              Cliente cliente = new Cliente(porta, adress);
  33.              try {
  34.                  cliente.iniciarConexaoCliente();
  35.              } catch (Exception e) {
  36.                  // TODO Auto-generated catch block
  37.                  e.printStackTrace();
  38.              }
  39.              String diretorioCliente = "TesteMusic2.rar";
  40.              try {
  41.                  cliente.transferenciaArquivo(diretorioCliente);
  42.              } catch (IOException e) {
  43.                  // TODO Auto-generated catch block
  44.                  e.printStackTrace();
  45.              }
  46.            
  47.         }
  48.        
  49.        
  50.        
  51.        
  52.        
  53.     }
  54.  
  55. }
  56.  
  57. package Cliente;
  58. import java.io.BufferedOutputStream;
  59. import java.io.FileOutputStream;
  60. import java.io.IOException;
  61. import java.io.InputStream;
  62. import java.net.InetAddress;
  63. import java.net.InetSocketAddress;
  64. import java.net.Socket;
  65.  
  66. public class Cliente {
  67.     private Socket socketCliente;
  68.     int porta;
  69.     String address;
  70.     public Cliente(int porta, String address) {
  71.         this.porta=porta;
  72.         this.address=address;
  73.     }
  74.     public void iniciarConexaoCliente() throws Exception {
  75.         this.socketCliente = new Socket(address, porta);
  76.     }
  77.     public void transferenciaArquivo(String diretorio) throws IOException {
  78.         byte[] conteudo = new byte[10000];
  79.        
  80.         FileOutputStream fos = new FileOutputStream(diretorio);
  81.         BufferedOutputStream bos = new BufferedOutputStream(fos);
  82.         InputStream is = socketCliente.getInputStream();
  83.    
  84.         int bytesRead = 0;
  85.        
  86.         while((bytesRead=is.read(conteudo))!=-1) {
  87.             bos.write(conteudo, 0, bytesRead);
  88.         }
  89.         bos.flush();
  90.         socketCliente.close();
  91.        
  92.         System.out.println("Arquivo recebido!");
  93.     }
  94.    
  95. }
  96.  
  97. package servidor;
  98. import java.io.BufferedInputStream;
  99.  
  100. import java.io.File;
  101. import java.io.FileInputStream;
  102. import java.io.FileNotFoundException;
  103. import java.io.IOException;
  104. import java.io.OutputStream;
  105. import java.net.InetAddress;
  106. import java.net.ServerSocket;
  107. import java.net.Socket;
  108.  
  109.  
  110.  
  111. @SuppressWarnings("unused")
  112. public class Servidor {
  113.     private ServerSocket socketServidor;
  114.     private Socket socketConexao;
  115.     private int porta;
  116.    
  117.     public Servidor(int porta) {
  118.         this.porta = porta;
  119.     }
  120.     public void iniciaConexaoServidor() {
  121.        
  122.         try {
  123.             this.socketServidor = new ServerSocket(this.porta);
  124.         } catch (IOException e) {
  125.             System.out.println("Nรฃo conseguiu iniciar o socket do servidor");
  126.             e.printStackTrace();
  127.         }
  128.         try {
  129.             this.socketConexao = socketServidor.accept();
  130.         } catch (IOException e) {
  131.             System.out.println("Nรฃo conseguiu aceitar a conexao com o cliente.");
  132.             e.printStackTrace();
  133.         }
  134.     }
  135.     public void enviarArquivo(String diretorio) throws Exception {
  136.         File arquivo = new File(diretorio);
  137.         FileInputStream fis = new FileInputStream(arquivo);
  138.         BufferedInputStream bis = new BufferedInputStream(fis);
  139.         OutputStream os = this.socketConexao.getOutputStream();
  140.        
  141.         byte[] conteudo;
  142.         long fileLength = arquivo.length();
  143.         long current = 0;
  144.        
  145.         while(current!=fileLength){
  146.             int size = 10000;
  147.             if(fileLength - current >= size)
  148.                 current += size;    
  149.             else{
  150.                 size = (int)(fileLength - current);
  151.                 current = fileLength;
  152.             }
  153.             conteudo = new byte[size];
  154.             bis.read(conteudo, 0, size);
  155.             os.write(conteudo);
  156.             System.out.println("Mandando arquivo ... "+(current*100)/fileLength+"% completo!");
  157.         }  
  158.        
  159.         os.flush();
  160.         this.socketConexao.close();
  161.         this.socketServidor.close();
  162.         bis.close();
  163.         System.out.println("Arquivo enviado com sucesso!");
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement