-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClient.java
More file actions
43 lines (39 loc) · 1.11 KB
/
ChatClient.java
File metadata and controls
43 lines (39 loc) · 1.11 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
34
35
36
37
38
39
40
41
42
43
//Ben Chin
import java.net.Socket;
import java.util.Scanner;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.IOException;
public class ChatClient{
public static void main(String[] args) throws IOException{
try(Socket socket = new Socket("cs380.codebank.xyz", 38002)){
Scanner kb = new Scanner(System.in);
InputStream in = socket.getInputStream();
InputStreamReader inR = new InputStreamReader(in, "UTF-8");
BufferedReader br = new BufferedReader(inR);
OutputStream os = socket.getOutputStream();
PrintStream out = new PrintStream(os);
System.out.print("Enter user name: ");
String username = kb.nextLine();
out.println(username);
Runnable receiver = () -> {
try{
String message;
while((message = br.readLine())!=null){
System.out.println(message);
}
System.exit(0);
}catch (IOException e){
System.out.println("Exception thrown.");
}
};
new Thread(receiver).start();
while(true){
out.println(kb.nextLine());
}
}
}
}