-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChatClient.java
34 lines (26 loc) · 973 Bytes
/
ChatClient.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
package chatApp;
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class ChatClient {
public static void main(String[] args) {
try {
Socket socket = new Socket("localhost", 7777);
DataInputStream inputFromServer = new DataInputStream(socket.getInputStream());
DataOutputStream outputToServer = new DataOutputStream(socket.getOutputStream());
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a message: ");
String msgwrite = scanner.nextLine();
// Send the message to the server
outputToServer.writeUTF(msgwrite);
String msgread = inputFromServer.readUTF();
// Print the message
System.out.println("server: " + msgread);
}
}
catch(IOException ex) {
System.err.println(ex);
}
}
}