MessageClient.java 8.78 KiB
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);
}
}
}