Java - Pasar una conversacion a un archivo

 
Vista:
sin imagen de perfil
Val: 20
Ha disminuido su posición en 41 puestos en Java (en relación al último mes)
Gráfica de Java

Pasar una conversacion a un archivo

Publicado por Saint (12 intervenciones) el 05/10/2020 22:46:47
Hola disculpen las molestias pero quisiera saber como pasar una conversación que hice entre un cliente y un servidor, no se me da bien el uso de archivos lo he intentado pero no me sale y por eso quería saber si alguien podría ayudarme con esa parte, gracias.

El código que tengo es el siguiente:

Clase cliente
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
 
public class Cliente {
 
    private int puerto;
    private String host;
 
    public Cliente() {
    }
 
    public Cliente(int puerto, String host) {
        this.puerto = puerto;
        this.host = host;
    }
 
    public void iniciarCliente(){
 
                try {
            System.out.println("Iniciando cliente ...");
 
            Socket clienteSocket = new Socket(this.host, this.puerto);
            System.out.println("Cliente conectado exitosamente ...");
 
            PrintWriter salida = new PrintWriter(clienteSocket.getOutputStream(), true);
            BufferedReader entrada = new BufferedReader(new InputStreamReader(clienteSocket.getInputStream()));
 
            salida.println("Hola servidor, soy un cliente");
            System.out.println("1:) " + entrada.readLine());
            salida.println("Como estas");
            System.out.println("2:) " + entrada.readLine());
            salida.println("Que eres");
            System.out.println("3:) " + entrada.readLine());
            salida.println("Ya me voy");
            System.out.println("4:) " + entrada.readLine());
            salida.println("adios");
            System.out.println("5:) " + entrada.readLine());
            System.out.println("Datos enviados correctamente ...");
 
            salida.close();
            clienteSocket.close();
 
        } catch (IOException ex) {
            Logger.getLogger(Cliente.class.getName()).log(Level.SEVERE, null, ex);
        }
 
    }
 
}

Clase Servidor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
 
public class Servidor {
 
    private int puerto;
 
    public Servidor() {
    }
 
    public Servidor(int puerto) {
        this.puerto = puerto;
    }
 
    public void iniciar(){
 
        try {
 
            System.out.println("Iniciando el servidor ...");
            ServerSocket serverSocket = new ServerSocket(this.puerto);
 
            System.out.println("Esperando conexiones ...");
 
            Socket clienteSocket = serverSocket.accept();
            System.out.println("Cliente conectado exitosamente");
 
            BufferedReader entrada = new BufferedReader(new InputStreamReader(clienteSocket.getInputStream()));
            PrintWriter salida = new PrintWriter(clienteSocket.getOutputStream(), true);
 
            String inputLine;
 
            while ((inputLine = entrada.readLine()) != null) {
                if ("adios".equals(inputLine)) {
                    salida.println("Chao");
                    break;
                }
                System.out.println("Mensaje enviado por parte del cliente: " + inputLine);
                salida.println("Mensaje enviado por parte del servidor: Esto es una respuesta automatica a su mensaje");
            }
 
            clienteSocket.close();
            serverSocket.close();
            entrada.close();
 
        } catch (IOException ex) {
            Logger.getLogger(Servidor.class.getName()).log(Level.SEVERE, null, ex);
        }
 
    }
 
}

Test del cliente:

1
2
3
4
5
6
7
8
9
10
11
public class PrincipalCliente {
 
    public static void main(String[] args) {
 
        Cliente c = new Cliente(9000,"LocalHost");
 
        c.iniciarCliente();
 
    }
 
}

Test servidor:

1
2
3
4
5
6
7
8
9
public class Test {
    public static void main(String[] args) {
 
        Servidor miServidor = new Servidor(9000);
 
        miServidor.iniciar();
 
    }
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder