Skip to content

Commit 0a3f771

Browse files
committed
Se añade loop while para mantenerse escuchando al servidor
1 parent 78bafb6 commit 0a3f771

File tree

3 files changed

+35
-20
lines changed

3 files changed

+35
-20
lines changed

KnockSockets/KnockKnockClient.java

+25-14
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,32 @@ public static void main(String[] args) throws IOException {
2626
String fromUser;
2727
String fromServer = "";
2828

29-
byte[] messageByte = new byte[10000];
30-
31-
while (in.available()>0) {
32-
int bytesRead = in.read(messageByte);
33-
fromServer += new String(messageByte, 0, bytesRead);
34-
35-
System.out.println(fromServer);
36-
fromServer = "";
37-
38-
fromUser = stdIn.readLine();
39-
if (fromUser != null) {
40-
// enviamos al server peticion del usuario
41-
out.println(fromUser);
42-
}
29+
byte[] messageByte = new byte[1000];
30+
31+
Boolean brokePipe = false;
32+
while(!brokePipe){
33+
34+
while (in.available() > 0) {
35+
System.out.println("Nuevo mensaje disponible");
36+
int bytesRead = in.read(messageByte);
37+
fromServer += new String(messageByte, 0, bytesRead);
38+
System.out.println(fromServer);
39+
fromServer = "";
40+
41+
fromUser = stdIn.readLine();
42+
if(fromUser.equals( "5")){
43+
brokePipe = true;
44+
break;
45+
}
46+
if (fromUser != null) {
47+
System.out.println("Se ha enviado petición: "+ fromUser + " al servidor");
48+
// enviamos al server peticion del usuario
49+
out.println(fromUser);
50+
out.flush();
51+
}
52+
}
4353
}
54+
System.out.println("Chao compare!");
4455
} catch (UnknownHostException e) {
4556
System.err.println("Don't know about host " + hostName);
4657
System.exit(1);

KnockSockets/KnockKnockServer.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ public static void main(String[] args) throws IOException {
2828
KnockKnockProtocol kkp = new KnockKnockProtocol();
2929
outputLine = kkp.processInput(null);
3030
out.println(outputLine);
31-
// out.flush();
31+
out.flush();
3232
outputLine = "";
3333

3434
while ((inputLine = in.readLine()) != null) {
35+
System.out.println("Ha llegado: "+ inputLine + " desde el cliente");
3536
outputLine = kkp.processInput(inputLine);
36-
System.out.println(outputLine);
37+
System.out.println("Petición procesada -> :" + outputLine);
3738
out.println(outputLine);
38-
if (outputLine.equals("Bye ma' friend!"))
39-
break;
39+
System.out.println("Respuesta enviada -> :" + outputLine);
40+
// if (outputLine.equals("Bye ma' friend!"))
41+
// break;
4042
}
4143
} catch (IOException e) {
4244
System.out.println("Exception caught when trying to listen on port "

KnockSockets/Makefile

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
javac ./*.java
1+
2+
all:
3+
javac ./*.java
24

35
clean:
46
rm ./*.class
57

68
run:
79
java EchoServer 7 &
8-
java EchoClient localhost 7
10+
java EchoClient localhost 7

0 commit comments

Comments
 (0)