-
Notifications
You must be signed in to change notification settings - Fork 3
/
Client1.java
58 lines (49 loc) · 1.86 KB
/
Client1.java
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
55
56
57
58
//LEC THREE
//SIMMILAR TO Client.java
import java.net.*;
import java.io.*;
import java.util.*;
public class Client1 {
public static void main(String[] args) {
Socket socket = null;
InputStreamReader inputStreamReader = null;
OutputStreamWriter outputStreamWriter = null;
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
socket = new Socket("localhost", 1234);
inputStreamReader = new InputStreamReader(socket.getInputStream());
outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());
bufferedReader = new BufferedReader(inputStreamReader);
bufferedWriter = new BufferedWriter(outputStreamWriter);
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Msg: ");
String msg = scanner.nextLine();
bufferedWriter.write(msg);
bufferedWriter.newLine();
bufferedWriter.flush();
System.out.println("From Server :" + bufferedReader.readLine());
if (msg.equalsIgnoreCase("BYE"))
break;
}
} catch (Exception e) {
// TODO: handle exception
} finally {
try {
if (inputStreamReader != null)
inputStreamReader.close();
if (outputStreamWriter != null)
outputStreamWriter.close();
if (bufferedReader != null)
bufferedReader.close();
if (bufferedWriter != null)
bufferedWriter.close();
if (socket != null)
socket.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
}