Skip to content

Commit 3e8c99d

Browse files
Merge pull request #448 from Sync-BR/patch-1
refactor: Use of `try-catch` blocks to capture and display communicat…
2 parents 8d965b3 + e557723 commit 3e8c99d

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

JAVA SERVER CLIENT NETWORKING(SAME DEVICE)/ServerProgram.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,32 @@ public static void main(String args[]) throws Exception {
88
PrintStream ps=new PrintStream(s.getOutputStream());
99
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
1010
BufferedReader kb=new BufferedReader(new InputStreamReader(System.in));
11-
while(true) {
12-
String str=br.readLine();
13-
String str1;
14-
if(str.equals("exit")) {
15-
break;
16-
}
17-
else {
18-
System.out.println("Client: "+str);
19-
System.out.print("Server: ");
20-
str1=kb.readLine();
11+
try {
12+
while (true) {
13+
String str = br.readLine();
14+
if (str == null || str.equals("exit")) {
15+
break;
16+
}
17+
System.out.println("Cliente: " + str);
18+
19+
System.out.print("Servidor: ");
20+
String str1 = kb.readLine();
2121
ps.println(str1);
2222
}
23+
} catch (IOException e) {
24+
System.out.println("Communication error: " + e.getMessage());
25+
e.printStackTrace();
26+
} finally {
27+
try {
28+
if (ps != null) ps.close();
29+
if (br != null) br.close();
30+
if (kb != null) kb.close();
31+
if (s != null) s.close();
32+
if (ss != null) ss.close();
33+
} catch (IOException e) {
34+
System.out.println("Error closing resources: " + e.getMessage());
35+
e.printStackTrace();
36+
}
2337
}
24-
ps.close();
25-
br.close();
26-
kb.close();
27-
s.close();
28-
ss.close();
2938
}
3039
}

0 commit comments

Comments
 (0)