====== Sockets en Java II ====== ===== Socket que no para hasta que se recibe cadena salir ===== ==== Servidor ==== package sockets2LaPelicula; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import sockets2LaPelicula.Cliente2; public void ejecutarHastaCadenaSalir() {//Se ejecuta hasta recibir cadena salir try { System.out.println("Lanzando Servidor..."); ServerSocket serverSocket = new ServerSocket(Servidor2.PUERTO); Socket socket = serverSocket.accept(); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));//Buffer entrada PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));//Buffer salida String cadenaRecibida = ""; while(!(cadenaRecibida = br.readLine()).equalsIgnoreCase("Salir")) {//Mientras no se reciba salir ejecuta esto pw.println(cadenaRecibida); pw.flush(); } }catch(IOException e){ e.printStackTrace(); } } public static void main(String[] args) { Servidor2 server = new Servidor2(); server.ejecutarHastaCadenaSalir(); } } ==== Cliente ==== package sockets2LaPelicula; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; import sockets2LaPelicula.Cliente2; import sockets2LaPelicula.Servidor2; public class Cliente2 { public void ejecutar() { try { System.out.print("Lanzando conexión...."); Socket socket = new Socket ("127.0.0.1", Servidor2.PUERTO);//Conectamos al servidor System.out.println("[OK]"); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));//Bufer de lectura PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));//Buffer de escritura String cadenaRecibida = br.readLine(); System.out.println(cadenaRecibida); pw.println("cadena recibida" + cadenaRecibida); pw.flush(); System.out.println("Fin del Cliente"); }catch(IOException e){ e.printStackTrace(); } } public void ejecutarInfinito() { try { System.out.print("Lanzando conexión...."); Socket socket = new Socket ("127.0.0.1", Servidor2.PUERTO);//Conectamos al servidor System.out.println("[OK]"); BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));//Bufer de lectura PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));//Buffer de escritura String lineaLeida =""; Scanner teclado = new Scanner(System.in); while(true) { lineaLeida = teclado.nextLine(); pw.println(lineaLeida); pw.flush(); } }catch(IOException e){ e.printStackTrace(); } } public static void main(String[] args) { Cliente2 client = new Cliente2(); client.ejecutarInfinito(); } }