-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClient.java
More file actions
30 lines (25 loc) · 1.01 KB
/
ChatClient.java
File metadata and controls
30 lines (25 loc) · 1.01 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
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class ChatClient {
private static final String SERVER_ADDRESS = "localhost";
private static final int SERVER_PORT = 12345;
public static void main(String[] args) {
try (Socket socket = new Socket(SERVER_ADDRESS, SERVER_PORT);
Scanner serverInput = new Scanner(socket.getInputStream());
OutputStream output = socket.getOutputStream();
Scanner userInput = new Scanner(System.in)) {
System.out.println(serverInput.nextLine()); // Read welcome message
while (true) {
System.out.print("You: ");
String message = userInput.nextLine();
output.write((message + "\n").getBytes());
if (serverInput.hasNextLine()) {
System.out.println(serverInput.nextLine()); // Read server response
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}