Cody M. Lewis

Scp App


This is a network based program implement a Simple Chat Protocol, a means structuring sent data such that it can be validated and read nicely by a computer. The GUI is handled by javax swing, and I have expanded this program to use Ephemeral Diffie-Hellman and AES to encrypt the messages between the client and the server.


A TCP Socket in Java tends amount to performing out.println to send data to the other user and in.readLine to get data from the other user.

For this case I have implemented a full duplex system, so the client and server can send and recieve messages at any time, this is done using multithreading.
Sending is placed in a actionListener, which means on pressing the send button the send method is executed:

msgField = new JTextField(30);
JButton msgBtn = new JButton("Send");
msgBtn.addActionListener(new Send());
Which creates part of the GUI and the action listener
private class Send implements ActionListener {
    public void actionPerformed(ActionEvent evt) {
        try {
            String[] messages = textToMessage();
            for(String message : messages) {
                msgArea.append("Waiting for message to send...\n");
                out.println(SCP.message(address.getHostAddress(), port, message));
                msgArea.append("Message Sent: " + AES.decrypt(sessionKey.toByteArray(), message) + "\n");
        } catch(Exception e) {
            System.err.println("Error: " + e.getMessage());
And this handles the message sending

Then message reception is placed in its own thread, for asyncronous reception of messages:

protected class Recieve implements Runnable {
    private String uname;
    public Recieve(String uname) { this.uname = uname; }
    public void run() {
        try {
            while(isRecieving) {
                String recievedMessage = recieveMessage();
                if(recievedMessage == "DISCONNECT") {
                    msgArea.append("Other user disconnected\n");
                    disconnect = true;
                    isRecieving = false;
                } else if(recievedMessage == "ACKNOWLEDGE") {
                    msgArea.append("Successfully disconnected\n");
                } else {
                    msgArea.append(String.format("%s: %s\n", uname, recievedMessage));
        } catch(SCPException SCPe) {
            System.err.println("Error: " + SCPe.getMessage());
        } catch(IOException ioe) {
            System.err.println("Error: " + ioe.getMessage());
        } catch(NullPointerException npe) {
            msgArea.append("\nUnexpected cut-off from other user\n");
            disconnect = true;
            isRecieving = false;