-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatBob.java
87 lines (78 loc) · 2.54 KB
/
ChatBob.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package week3;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class ChatBob {
public static void main(String[] args) {
Thread clienThread = new Thread(() ->{
EncryptDecryptBob encryptDecrypt = new EncryptDecryptBob();
Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
try {
socket = new Socket("10.66.48.86",9123);
System.out.println("Connected");
dataInputStream = new DataInputStream(System.in);
dataOutputStream = new DataOutputStream(socket.getOutputStream());
} catch (UnknownHostException e) {
System.out.println(e);
}
catch (IOException e) {
System.out.println(e);
}
String lineString = "";
while (!lineString.equals("over")) {
try {
lineString = dataInputStream.readLine();
lineString = encryptDecrypt.encrypt(lineString);
dataOutputStream.writeUTF(lineString);
} catch (IOException e) {
System.out.println(e);
}
}
try {
dataInputStream.close();
dataOutputStream.close();
socket.close();
System.out.println("Connection Closed");
} catch (IOException e) {
System.out.println(e);
}
});
Thread serverThread = new Thread(()->{
try {
EncryptDecryptBob encryptDecrypt = new EncryptDecryptBob();
Socket socket = null;
try (ServerSocket serverSocket = new ServerSocket(5000)) {
DataInputStream dataInputStream = null;
System.out.println("Server Started");
System.out.println("Waiting for the client....");
socket = serverSocket.accept();
System.out.println("Client Accepted");
dataInputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
String lineString= "";
while (!lineString.equals("over")) {
try {
lineString = dataInputStream.readUTF();
lineString = encryptDecrypt.decrypt(lineString);
System.out.println(lineString);
} catch (IOException e) {
System.out.println(e);
}
}
socket.close();
dataInputStream.close();
}
System.out.println("Connection Closed");
} catch (IOException e) {
System.out.println(e);
}
});
serverThread.start();
clienThread.start();
}
}