-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChatServer.java
46 lines (34 loc) · 1.31 KB
/
ChatServer.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
package chatApp;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class ChatServer {
public static void main(String[] args) {
try {
ServerSocket serverSocket = new ServerSocket(7777);
System.out.println("Server started at 7777");
Socket socket = serverSocket.accept();
System.out.println("Client connected");
// Create data input and output streams
DataInputStream inputFromClient = new DataInputStream(socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
while (true) {
Scanner scanner =new Scanner(System.in);
String msgread = inputFromClient.readUTF();
// Print the message
System.out.println("Client: " + msgread);
System.out.print("Enter a message: ");
String msgwrite = scanner.nextLine();
// Send the message to the server
outputToClient.writeUTF(msgwrite);
if (msgread.equals("NO MORE")) {
outputToClient.writeUTF("OK");
break;
}
}
}
catch(IOException ex) {
System.err.println(ex);
}
}
}