Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Servidor extends Thread {
- static final int port = 49000;
- Socket skCliente;
- public Servidor(Socket sCliente) {
- skCliente = sCliente;
- }
- public static void main(String[]args){
- try {
- ServerSocket skServidor = new ServerSocket(port);
- System.out.println("Servidor escuchando en el puerto " + port);
- while(true){
- Socket skCliente = skServidor.accept();
- System.out.println("¡¡¡Cliente Conectado!!!");
- new Servidor(skCliente).start();
- }
- } catch (Exception e) {
- System.out.println("Error de conexión");
- }
- }
- @Override
- public void run() {
- try {
- OutputStream aux = skCliente.getOutputStream();
- DataOutputStream flujo_salida = new DataOutputStream(aux);
- InputStream auxi = skCliente.getInputStream();
- DataInputStream flujo_entrada = new DataInputStream(auxi);
- flujo_salida.writeUTF("¡¡¡Conexión satisfactoria!!!");//Escribe en el Stream para que lo lea el cliente
- String comando;
- boolean correcto = false;
- do {
- flujo_salida.writeUTF("Introduce nombre del archivo: ");
- comando=flujo_entrada.readUTF();
- if(comando.equals("dam")){
- File archivo = new File ("C:\Users\Dani\Desktop\dam.txt");
- FileReader fr = new FileReader (archivo);
- BufferedReader br = new BufferedReader(fr);
- String linea = br.readLine();
- flujo_salida.writeUTF("true");
- flujo_salida.writeUTF(linea);
- correcto = true;
- }else{
- flujo_salida.writeUTF("false");
- correcto = false;
- }
- } while (correcto == false);
- flujo_entrada.close();
- flujo_salida.close();
- System.out.println("Cliente desconectado");
- skCliente.close();
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- }
- }
- public class Cliente {
- public static void main(String[] args) throws IOException {
- final String ip = "localhost";
- final int port = 49000;
- String resultado = "false";
- Scanner sc = new Scanner(System.in);
- Socket sCliente = new Socket(ip, port);
- //System.out.println("Me he conectado al servidor");
- InputStream aux = sCliente.getInputStream();
- DataInputStream flujo_entrada = new DataInputStream(aux);
- OutputStream auxi = sCliente.getOutputStream();
- DataOutputStream flujo_salida = new DataOutputStream(auxi);
- System.out.println(flujo_entrada.readUTF());
- System.out.print(flujo_entrada.readUTF());
- String archivo = sc.nextLine();
- flujo_salida.writeUTF(archivo);
- resultado = flujo_entrada.readUTF();
- do{
- if(resultado.equals("false")){
- System.out.println("No existe el archivo");
- System.out.print("Introduzca el nombre del archivo: ");
- archivo = sc.nextLine();
- flujo_salida.writeUTF(archivo); }
- if(resultado.equals("true")){
- System.out.println("Lectura del archivo solicitado: ");
- System.out.println(flujo_entrada.readUTF());
- System.out.println(flujo_entrada.readUTF());
- }
- }while (resultado.equals("false"));
- sCliente.close();
- }
- }
Add Comment
Please, Sign In to add comment