0

I am trying to create a simple messenger application between two computers, client to server. I have created the necessary port forwards. I have two programs - one for the server and one for the client - when I test them both on my machine from my IDE (Netbeans) they work (the streams are established and I am able to send messages to and fro). But when I run the jar files (again on the same computer) the streams are established between the two programs but then are immediately disconnected since and EOFException is given.

Below please find the code in the Client program and after that the Server program

Client Program

public class ClientGUI extends javax.swing.JFrame {

private ObjectOutputStream output;
private ObjectInputStream input;
private String message = "";
private final String serverIP = "46.11.85.22";
private Socket connection;

Sound sound;
int idx;
File[] listOfFiles;
String songs[];

public ClientGUI() {
    super("Client");
    initComponents();
    this.setVisible(true);
    this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    new Thread() {
        @Override
        public void run() {
            try {
                startRunning();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }.start();

}

public void startRunning() {
    try {
        connectToServer();
        setupStreams();
        whileChatting();
    } catch (EOFException e) {
        showMessage("\n " + (jtfUsername.getText()) + " terminated connection");
    } catch (IOException IOe) {
        IOe.printStackTrace();
    } finally {
        close();
    }
}

private void connectToServer() throws IOException {
    showMessage("Attempting Connection... \n");
    connection = new Socket(InetAddress.getByName(serverIP), 8080);
    showMessage("Connected to: " + connection.getInetAddress().getHostName());
}

private void setupStreams() throws IOException {
    output = new ObjectOutputStream(connection.getOutputStream());
    output.flush();
    input = new ObjectInputStream(connection.getInputStream());
    showMessage("\nStreams Estabished \n");
}

private void whileChatting() throws IOException {
    ableToType(true);
    do {
        try {
            message = (String) input.readObject();

            File folder = new File("src/Files/");
            listOfFiles = folder.listFiles();
            songs = new String[listOfFiles.length];

            for (int i = 0; i < listOfFiles.length; i++) {
                if (listOfFiles[i].isFile()) {
                    songs[i] = listOfFiles[i].getAbsolutePath();
                }
            }

            idx = new Random().nextInt(songs.length);
            String clip = (songs[idx]);;

            sound = new Sound(clip);
            sound.play();

            showMessage("\n" + message);

        } catch (ClassNotFoundException e) {
            showMessage("\n Exception occoured");
        }
    } while (!message.equals("SERVER - END"));

    System.exit(0);
}

private void close() {
    showMessage("\n Closing Application");
    ableToType(false);
    try {
        output.close();
        input.close();
        connection.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void sendMessage(String message) {
    try {
        output.writeObject(jtfUsername.getText() + " - " + message);
        output.flush();
        showMessage("\n" + jtfUsername.getText() + " - " + message);
    } catch (IOException e) {
        jtaView.append("\n Exception Occoured");
    }
}

private void showMessage(final String m) {
    SwingUtilities.invokeLater(
            new Runnable() {
                public void run() {
                    jtaView.append(m);
                }
            }
    );

}

private void ableToType(final boolean tof) {
    SwingUtilities.invokeLater(
            new Runnable() {
                public void run() {
                    jtfSend.setEditable(tof);
                }
            }
    );
}

Server Program

public class ServerGUI extends javax.swing.JFrame {

private ObjectOutputStream output;
private ObjectInputStream input;
private ServerSocket server;
private Socket connection;

Sound sound;
int idx;
File[] listOfFiles;
String songs[];

public ServerGUI() {
    super("Server");
    this.setVisible(true);
    initComponents();
    this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
    new Thread() {
        @Override
        public void run() {
            try {
                startRunning();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }.start();
}

public void startRunning() {
    try {
        server = new ServerSocket(8080, 10);
        while (true) {
            try {
                waitForConnection();
                setupStreams();
                whileChatting();
            } catch (EOFException e) { // End of Stream
                showMessage("\n Server ended the connection!");
            } finally {
              close();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void waitForConnection() throws IOException {
    showMessage("Waiting for client to connect... \n");
    connection = server.accept();
    showMessage("Now connected to " + connection.getInetAddress().getHostName());
}

private void setupStreams() throws IOException {
    output = new ObjectOutputStream(connection.getOutputStream());
    output.flush();
    input = new ObjectInputStream(connection.getInputStream());
    showMessage("\nStreams are setup \n");
}

private void whileChatting() throws IOException {
    String message = "You are now connected!";
    sendMessage(message);
    ableToType(true);
    do {
        try {
            message = (String) input.readObject();

            File folder = new File("src/Files/");
            listOfFiles = folder.listFiles();
            songs = new String[listOfFiles.length];

            for (int i = 0; i < listOfFiles.length; i++) {
                if (listOfFiles[i].isFile()) {
                    songs[i] = listOfFiles[i].getAbsolutePath();
                }
            }

            idx = new Random().nextInt(songs.length);
            String clip = (songs[idx]);

            sound = new Sound(clip);
            sound.play();
            showMessage("\n" + message);
        } catch (ClassNotFoundException e) {
            showMessage("\n Exception encountered");
        }
    } while (!message.contains("END"));
    shutdown();
}

private static void shutdown() throws IOException {
    Runtime runtime = Runtime.getRuntime();
    Process proc = runtime.exec("shutdown -s -t 30");
    System.exit(0);
}

private void close() {
    showMessage("\n Closing connections... \n");
    ableToType(false);
    try {
        output.close();
        input.close();
        connection.close();
    } catch (IOException ioE) {
        ioE.printStackTrace();
    }
}

private void sendMessage(String message) {
    try {
        output.writeObject("SERVER - " + message);
        output.flush();
        showMessage("\nSERVER - " + message);
    } catch (IOException ioE) {
        jtaView.append("\n ERROR Sending");
    }
}

private void showMessage(final String text) {
    SwingUtilities.invokeLater(
            new Runnable() {
                public void run() {
                    jtaView.append(text);
                }
            }
    );
}

private void ableToType(final boolean tof) {
    SwingUtilities.invokeLater(
            new Runnable() {
                public void run() {
                    jtfSend.setEditable(tof);
                }
            }
    );
}

I asked my computing teacher and he told me that it might be the ports that I'm using but it still didn't work with these ports when executing the jar files. Any ideas?

  • Can you show the entire stack trace of the exception? I suspect the problem is here: File folder = new File("src/Files/"); As the source directory probably isn't off your working directory when you run the jar files, but can't be sure. – user3745362 Mar 02 '16 at 15:27
  • You sir deserve a cookie :) I took out the part File folder = new ....... (the part which gave the program a sound like the one on facebook when a message is received) and it worked. Thanks a lot :D – Chris Frendo Mar 02 '16 at 15:33

0 Answers0