Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package servidor;
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.InetAddress;
- import java.net.ServerSocket;
- import java.net.Socket;
- @SuppressWarnings("unused")
- public class Servidor {
- private ServerSocket socketServidor;
- private Socket socketConexao;
- private int porta;
- public Servidor(int porta) {
- this.porta = porta;
- }
- public void iniciaConexaoServidor() {
- try {
- this.socketServidor = new ServerSocket(this.porta);
- } catch (IOException e) {
- System.out.println("Não conseguiu iniciar o socket do servidor");
- e.printStackTrace();
- }
- try {
- this.socketConexao = socketServidor.accept();
- } catch (IOException e) {
- System.out.println("Não conseguiu aceitar a conexao com o cliente.");
- e.printStackTrace();
- }
- }
- public void enviarArquivo(String diretorio) throws Exception {
- File arquivo = new File(diretorio);
- FileInputStream fis = new FileInputStream(arquivo);
- BufferedInputStream bis = new BufferedInputStream(fis);
- OutputStream os = this.socketConexao.getOutputStream();
- byte[] conteudo;
- long fileLength = arquivo.length();
- long current = 0;
- while(current!=fileLength){
- int size = 10000;
- if(fileLength - current >= size)
- current += size;
- else{
- size = (int)(fileLength - current);
- current = fileLength;
- }
- conteudo = new byte[size];
- bis.read(conteudo, 0, size);
- os.write(conteudo);
- System.out.println("Mandando arquivo ... "+(current*100)/fileLength+"% completo!"+"ip do cliente:"+socketConexao.getInetAddress());
- }
- os.flush();
- this.socketConexao.close();
- this.socketServidor.close();
- bis.close();
- System.out.println("Arquivo enviado com sucesso!");
- }
- }
- package Cliente;
- import java.io.BufferedOutputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.InetAddress;
- import java.net.InetSocketAddress;
- import java.net.Socket;
- import java.net.UnknownHostException;
- public class Cliente {
- private Socket socketCliente;
- int porta;
- String address;
- public Cliente(int porta, String address) {
- this.porta=porta;
- this.address=address;
- }
- public void iniciarConexaoCliente() {
- try {
- this.socketCliente = new Socket(address, porta);
- } catch (UnknownHostException e) {
- System.out.println("Não conseguiu iniciar a conexao com o servidor");
- e.printStackTrace();
- } catch (IOException e) {
- System.out.println("Não conseguiu iniciar a conexao com o servidor");
- e.printStackTrace();
- }
- }
- public void transferenciaArquivo(String diretorio) throws IOException {
- byte[] conteudo = new byte[10000];
- FileOutputStream fos = new FileOutputStream(diretorio);
- BufferedOutputStream bos = new BufferedOutputStream(fos);
- InputStream is = socketCliente.getInputStream();
- int bytesRead = 0;
- long current= 0;
- long conteudoLength = conteudo.length;
- while((bytesRead=is.read(conteudo))!=-1) {
- bos.write(conteudo, 0, bytesRead);
- current = bytesRead;
- System.out.println("Recebendo arquivo..."+(current*100)/conteudoLength+"% recebido!" +"ip do servidor:"+this.address);
- }
- bos.flush();
- socketCliente.close();
- System.out.println("Arquivo recebido!");
- }
- }
- import Cliente.Cliente;
- import servidor.Servidor;
- import java.io.IOException;
- import java.util.Scanner;
- @SuppressWarnings("unused")
- public class TransferMain {
- public static void main(String[] args) {
- String adress; //192.168.25.9
- int porta;
- String escolha;
- Scanner in = new Scanner(System.in);
- Scanner str = new Scanner(System.in);
- System.out.println("Servidor ou cliente? S ou C");
- escolha = str.nextLine();
- System.out.println("endereço ip: ");
- adress = str.nextLine();
- System.out.println("porta: ");
- porta = in.nextInt();
- if(escolha.equals("S")) {
- Servidor servidor = new Servidor(porta);
- servidor.iniciaConexaoServidor();
- String diretorioServidor = "MusicasTeste.rar";
- try {
- servidor.enviarArquivo(diretorioServidor);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- } else {
- Cliente cliente = new Cliente(porta, adress);
- try {
- cliente.iniciarConexaoCliente();
- } catch (Exception e) {
- System.out.println("Não iniciou a conexão com o Cliente.");
- e.printStackTrace();
- }
- String diretorioCliente = "MusicasTeste2.rar";
- try {
- cliente.transferenciaArquivo(diretorioCliente);
- } catch (IOException e) {
- System.out.println("Não conseguiu receber o arquivo.");
- e.printStackTrace();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment