Newer
Older
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class MessageClient extends JFrame implements ActionListener {
private Container content;
private JLabel usernameLabel;
private JTextField username;
private JButton join;
private JButton connect;
private JTextField chat;
private JTextPane messages;
private JScrollPane messageBoard;
private JTextField messageBar;
private JButton send;
private boolean openSession;
private InetAddress ip = InetAddress.getByName("schming.webredirect.org");
private int listenPort;
private Socket socket;
public MessageClient() throws Exception{
super("Message client"); //set up window and content pane
content = new Container();
this.setContentPane(content);
this.setSize(720,720);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
usernameLabel = new JLabel("Username:"); //set up all the GUI components
usernameLabel.setSize(100,30);
usernameLabel.setLocation(60,10);
content.add(usernameLabel);
username = new JTextField();
username.setSize(400,30);
username.setLocation(135,10);
content.add(username);
connect = new JButton();
connect.setText("Connect");
connect.setSize(100,30);
connect.setLocation(560,10);
connect.addActionListener(this);
connect.setActionCommand("toggleConnect");
content.add(connect);
chat = new JTextField();
chat.setSize(475,30);
chat.setLocation(60,50);
chat.setEnabled(false);
content.add(chat);
join = new JButton();
join.setText("Join");
join.setSize(100,30);
join.setLocation(560,50);
join.addActionListener(this);
join.setActionCommand("toggleJoin");
join.setEnabled(false);
content.add(join);
messages = new JTextPane();
messages.setEditable(false);
messageBoard = new JScrollPane(messages);
messageBoard.setSize(600,500);
messageBoard.setLocation(60, 110);
content.add(messageBoard);
messageBar = new JTextField();
messageBar.setSize(475,30);
messageBar.setLocation(60,625);
messageBar.setEnabled(false);
content.add(messageBar);
send = new JButton();
send.setText("Send");
send.setSize(100,30);
send.setLocation(560,625);
send.addActionListener(this);
send.setActionCommand("send");
send.setEnabled(false);
content.add(send);
this.addWindowListener(new WindowAdapter(){ //add code to run on closing
@Override
public void windowClosing(WindowEvent e) { //if there is still an active session, send a request to end it to the server
if (openSession){
try {
String response = sendRequest("close/" + listenPort);
System.out.println(response);
socket.close();
} catch (Exception ex){
throw new RuntimeException(ex);
}
}
}
});
openSession = false; //initialise a flag as to whether or not there is an open session
listenPort = 1025; //find the first open port
boolean free = false;
while (!free){
try{
ServerSocket test = new ServerSocket(listenPort);
test.close();
free = true;
} catch (Exception e){
listenPort++;
}
}
}
public String sendRequest(String request) throws Exception{ //method to send a request to the server
socket = new Socket(ip, 2001);
PrintWriter sender = new PrintWriter(socket.getOutputStream());
sender.println(request);
sender.flush();
BufferedReader listener = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String response = listener.readLine();
socket.close();
return response;
}
public void actionPerformed(ActionEvent e){ //code to handle all button presses
try {
String text = messages.getText();
if (e.getActionCommand().equals("toggleConnect")) { //if it is the connect button
if (connect.getText().equals("Connect")) { //if we are currently unconnected
if (username.getText().equals("") || username.getText().contains("/")){ //check if the username is invalid, if so inform the user
messages.setText(text + "Invalid username\n");
} else { //else, send a request, print the response, enable/disable all relevant buttons and toggle the button to disconnect
String response = sendRequest("open/" + listenPort + "/" + username.getText());
messages.setText(text + response + "\n");
chat.setEnabled(true);
join.setEnabled(true);
username.setEnabled(false);
connect.setText("Disconnect");
}
}
else { //else we are connected, so send a disconnect request, enable/disable all relevant buttons and toggle the button to connect
String response = sendRequest("close/" + listenPort);
messages.setText(text + response + "\n");
chat.setEnabled(false);
join.setEnabled(false);
messageBar.setEnabled(false);
send.setEnabled(false);
username.setEnabled(true);
chat.setText("");
messageBar.setText("");
join.setText("Join");
connect.setText("Connect");
}
} else if (e.getActionCommand().equals("toggleJoin")) { //same as previous but for joining a chat
if (join.getText().equals("Join")) {
if (chat.getText().equals("") || chat.getText().equals("NotInAChat") || username.getText().contains("/")){
messages.setText(text + "Invalid chat name\n");
} else {
String response = sendRequest("join/" + listenPort + "/" + chat.getText());
messages.setText(text + response + "\n");
messageBar.setEnabled(true);
send.setEnabled(true);
chat.setEnabled(false);
join.setText("Leave");
openSession = true;
}
}
else {
String response = sendRequest("leave/" + listenPort);
messages.setText(text + response + "\n");
messageBar.setEnabled(false);
send.setEnabled(false);
chat.setEnabled(true);
messageBar.setText("");
join.setText("Join");
openSession = false;
}
} else if (e.getActionCommand().equals("send")) { //if it is the send button, check if the message is valid, if so send a request, else inform the user it is invalid
if (messageBar.getText().contains("/")) {
messages.setText(text + "Message contains banned characters\n");
} else {
sendRequest("message/" + listenPort + "/" + messageBar.getText());
}
}
} catch (Exception ex){
throw new RuntimeException(ex);
}
}
public JTextPane getMessages(){ //getter for the message text pane
return messages;
}
public int getListenPort(){ //getter for the listen port number
return listenPort;
}
public static void main(String[] args) throws Exception {
MessageClient window = new MessageClient(); //open a new window and a server socket o listen for responses
ServerSocket listenSocket = new ServerSocket(window.getListenPort());
window.show();
while (true){ //continuously listen for the next response, printing it when it comes through
Socket socket = listenSocket.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String message = reader.readLine();
socket.close();
String messages = window.getMessages().getText();
messages += message + "\n";
window.getMessages().setText(messages);
}
}
}