-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiChatClient.java
More file actions
66 lines (63 loc) · 1.55 KB
/
MultiChatClient.java
File metadata and controls
66 lines (63 loc) · 1.55 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cse3040_25;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
public class MultiChatClient {
static class ClientSender extends Thread{
Socket so;
DataOutputStream out;
String name;
ClientSender(Socket socket, String name){
this.so = socket;
try {
out = new DataOutputStream(socket.getOutputStream());
this.name = name;
}catch(Exception e) {}
}
public void run() {
Scanner scanner = new Scanner(System.in);
try {
if(out != null) {
out.writeUTF(name);
}
while(out != null) {
out.writeUTF("["+name+"]"+scanner.nextLine());
}
}catch(IOException e) {}
}
}
static class ClientReceiver extends Thread{
Socket socket;
DataInputStream in;
ClientReceiver(Socket socket){
this.socket = socket;
try {
in = new DataInputStream(socket.getInputStream());
}catch(IOException e) {}
}
public void run() {
while(in != null) {
try {
System.out.println(in.readUTF());
} catch(IOException e) {}
}
}
}
public static void main(String[] args) {
if(args.length != 1) {
System.out.println("usage: Java MultiChatClient username");
System.exit(0);
}
try {
String serverIp = "127.0.0.1";
Socket socket = new Socket(serverIp,7777);
System.out.println("connected to server.");
Thread sender = new Thread(new ClientSender(socket, args[0]));
Thread receiver = new Thread(new ClientReceiver(socket));
sender.start();
receiver.start();
}catch (Exception e) {}
}
}