-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatServer.java
More file actions
33 lines (30 loc) · 1.32 KB
/
Copy pathChatServer.java
File metadata and controls
33 lines (30 loc) · 1.32 KB
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
import java.io.*;
import java.net.*;
public class ChatServer {
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(12345)) {
System.out.println("Saiganesh is waiting for Shyam...");
Socket socket = serverSocket.accept();
System.out.println("Shyam connected!");
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
String message = input.readLine();
System.out.println("Shyam: " + message);
if ("Hi Saiganesh!".equalsIgnoreCase(message)) {
output.println("Yes, Sairam Shyam! Yela Unnavu?");
}
while (true) {
message = input.readLine();
if (message.equalsIgnoreCase("exit")) break;
System.out.println("Shyam: " + message);
System.out.print("Saiganesh: ");
message = console.readLine();
output.println(message);
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}