-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClient.java
More file actions
134 lines (114 loc) · 4.01 KB
/
Copy pathChatClient.java
File metadata and controls
134 lines (114 loc) · 4.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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import java.net.Socket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.swing.*;
//update chat clien tarray in while
public class ChatClient {
static BufferedReader in;
static BufferedWriter out;
static ObjectInputStream userObjectInput;
static ObjectOutputStream userObjectOutput;
static ArrayList<User> userArray = new ArrayList<User>();
static User newUser;
static String user;
static privateChat[] whispers = new privateChat[50];
public static void main(String[] args) {
try {
Display menu = new Display();
menu.setVisible(true);
startTheChat();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "PLEASE NOTE: Server Terminated, messages will no longer send!");
}
}
public static void startTheChat() throws Exception {
// Requires user to input there ip address to allow for connection
// identification
// Returns value entered by client
String ipAddress = JOptionPane.showInputDialog(null, "Enter Server IP Address:", "IP Address Required!",
JOptionPane.PLAIN_MESSAGE);
Socket sock = new Socket(ipAddress, 8000);
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
userObjectOutput = new ObjectOutputStream(sock.getOutputStream());
userObjectInput = new ObjectInputStream(sock.getInputStream());
int index = 0;
// Takes the user input and sends it to the server
while (true) {
String str = in.readLine();
if (str.equals("Name is Required!")) {
user = JOptionPane.showInputDialog(null,
"Enter a unique Name:",
"Name Required!",
JOptionPane.PLAIN_MESSAGE);
out.write(user);
out.newLine();
out.write(ipAddress);
out.newLine();
out.flush();
} else if (str.equals("Sorry, this name has been taken!")) {
user = JOptionPane.showInputDialog(null,
"Enter another Name:",
"Name Already Exists!",
JOptionPane.WARNING_MESSAGE);
out.write(user);
out.newLine();
out.write(ipAddress);
out.newLine();
out.flush();
} else if (str.startsWith("YOURNAME")) {
// Ignores string YOURNAME and isolates userName only
Display.userListField.setText("");
userArray = (ArrayList<User>) userObjectInput.readObject();
Display.displayUsername.setText("You are logged in as: " + str.substring(8) + "\n");
} else if (str.startsWith("update")) {
String recvd = str.substring(6);
String[] users = new String[50];
users = recvd.split(",");
Display.userListField.setText("");
for (String name : users) {
if (name.startsWith("[") && name.endsWith("]")) {
Display.userListField.append(name.substring(1, name.length() - 1) + "\n");
} else if (name.endsWith("]")) {
Display.userListField.append(name.substring(0, name.length() - 1) + "\n");
} else if (name.startsWith("[")){
Display.userListField.append(name.substring(1) + "\n");
} else {
Display.userListField.append(name + "\n");
}
}
} else if (str.startsWith("@")) {
//open chat menu if doesn't already exist
//send msg so that it displays on both users
// -> find user's chat menu in array
String privMsg = str.substring(1);
String[] splitMsg = privMsg.split(":");
String privUser = splitMsg[0];
str = splitMsg[1];
for (User toSend : ChatServer.userArray) {
if (privUser.equals(toSend.name)) {
String privIP = toSend.getIP();
Socket temp = new Socket(privIP, 8000);
}
}
} else {
Display.chatField.append(str + "\n");
}
}
}
}