0

I have followed the newboston tutorials and made a chatroom with server side code and client side code. I wanted to build them together in one app.

My issue is that the client is blank, I used the standalone server from the tutorial. picture of server chat working with the client chat being a blank window But if I close the server first, all of the chat appears on the client chat, as in it is all instantly updated. client chat updated, all previous chats messages displayed. Standalone server:

package instantmessage;
import javax.swing.JFrame;


public class ServerChatroom {

    public static void main(String[] args) {
        Server runServer = new Server();
        runServer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        runServer.startServer();

    }

}

package instantmessage;


import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Server extends JFrame{

    private JTextField userText; //where you type in message
    private JTextArea chatWindow; //displays the conversation
    private ObjectOutputStream output;  //information sent to the recipent
    private ObjectInputStream input; //information being received from the recipent
    private ServerSocket server;
    private Socket connection; //the connection between you and the recipent's computer

    //setting up GUI.
    public Server(){
        super("Instant Messanger - Server"); //the piece in the brackets is the title off the app.
        userText = new JTextField();

        userText.setEditable(false);//without having a connection you can't send messages.

        userText.addActionListener(
            new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent event){
                    sendMessage(event.getActionCommand()); //sends string that was typed into text field.
                    userText.setText(""); //after text has been sent, remove the previous message sent.
                }
            }
        );
        add(userText, BorderLayout.NORTH); //add userText to GUI, make chat window top of the screen.
        chatWindow = new JTextArea();
        add(new JScrollPane(chatWindow)); //scrolling pane created for all the chats.

        setSize(450,200);
        setVisible(true);
    }

    //create the server and run the serve.
    public void startServer(){
        try{
            server = new ServerSocket(6789, 100); //Server socket. first one the server socket, the second how many clients can wait for connection.
            while(true){//the loop should run forever. The main program.
                try{//connect and allow conversation with another
                    waitForConnection();//wait for someone to connect with server.
                    setupStream(); //create the connection between another computer.
                    whileChatting(); //allow messages to be sent back and forth during chat.
                }catch(EOFException eofException){ //whenever the conversation is ended.
                    showMessage("\n Server overloaded, connection terminated by the server.");
                }finally{
                    closeServer();
                }
            }
        }catch(IOException ioException){
            ioException.printStackTrace();
        }
    }

    //wait for connection, after connection established display connection details.
    private void waitForConnection() throws IOException{
        showMessage("Waiting for connection to be established...\n"); //so user has something to see, knows program isn't frozen.
        connection = server.accept(); //accept the connection.
        showMessage("Connection established with " + connection.getInetAddress().getHostName());
    }

    //gets stream for send/receiving of data.
    private void setupStream() throws IOException{
        output = new ObjectOutputStream(connection.getOutputStream()); //create pathway for connection to other computer.
        output.flush(); //good practice. Cleans up data that was "left over"

        input = new ObjectInputStream(connection.getInputStream()); //creates pathway to receive data from other PC.
        //no flush for input, the other side has to flash.
        showMessage("Connection now active");
    }

    //sending text back and forth
    //this receives the data from the connected computer and displays it.
    private void whileChatting() throws IOException{
        String message = " You are now connected ";
        sendMessage(message);
        ableToType(true);
        do{//have conversation while client doesn't send CLIENT - END.
            try{
                message = (String) input.readObject(); //read whatever message is incoming.
                showMessage("\n" + message);
            }catch(ClassNotFoundException classNotFoundException){
                showMessage("\n user has sent garbage.");
            }

        }while(!message.equals("CLIENT - END"));
    }

    //close streams/sockets after chat ends. "Good housekeeping". Frees up memory.
    private void closeServer(){
        showMessage("\n\tTerminating connection...");
        ableToType(false);
        try{
            input.close(); //terminate stream from
            output.close(); //terminate stream to
            connection.close(); //terminate connection
        }catch(IOException ioException){
            ioException.printStackTrace();
        }
    }

    //send message to connected computer.
    private void sendMessage(String message){
        try{
            output.writeObject("SERVER - " + message); //creates an object of message and sends it along the output stream.
            output.flush(); //remove once it's finished sending.
            showMessage("\nSERVER - " + message);
        }catch(IOException ioException){
            chatWindow.append("\n error sending message"); //print out the error in the chat window that the message was not sent.
        }
    }

    //shows messages on main chat area. Updates the chatWindow so it is always current.
    private void showMessage(final String string){
        SwingUtilities.invokeLater( //creates thread that updates parts of the GUI.
                new Runnable(){
                    public void run(){
                        chatWindow.append(string);
                    }
                }        
        );
    }

    //sets the textfield editable so that the user can type in it
    private void ableToType(final boolean bool){
        SwingUtilities.invokeLater( //creates thread that updates parts of the GUI.
                new Runnable(){
                    public void run(){
                        userText.setEditable(bool);
                    }
                }        
        );
    }
}

My code for the standalone piece:

    package chatroomomni;

    import javax.swing.JFrame;

    public class ChatRoomOmni {  
        public static void main(String[] args) {
            MainMenu runProgram = new MainMenu();
            runProgram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }


package chatroomomni;

//the user has to chose to be a client or a server.

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

//layout: first button is host server, second line is a JTextLabel telling user to enter server IP, JTextField next to it for server IP, connect button next.
public class MainMenu extends JFrame{
    private JButton hostServer = new JButton("Host server");
    private JLabel labelEnterServerIP = new JLabel("Enter Server IP: ");
    private JTextField serverIPInput = new JTextField(25);
    private JButton connectServer = new JButton("Connect to server");

    //constructor
    public MainMenu(){
        super("Instant Messenger - Main Menu");

        serverIPInput.setEditable(true);

        //action listener for the host server.
        hostServer.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                        Server runServer = new Server();
                        runServer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        CloseFrame();
                        runServer.startServer();

            }
        });

        //action listener for connect to server.
        connectServer.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                if(!String.valueOf(serverIPInput.getText()).equals("")){
                    System.out.println(String.valueOf(serverIPInput.getText()));
                    Client client = new Client(String.valueOf(serverIPInput.getText()));
                    client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    CloseFrame();
                    client.startClient();
                }
            }
        });


        setLayout(new FlowLayout());

        //add items to frame.
        add(hostServer);
        add(labelEnterServerIP);
        add(serverIPInput);
        add(connectServer);


        setSize(1000, 500);
        setVisible(true);
    }

    public void CloseFrame(){
        super.dispose();
    }

}
package chatroomomni;

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Client extends JFrame{

    private JTextField userText; //where you type in message
    private JTextArea chatWindow; //displays the conversation
    private ObjectOutputStream output;  //information sent to the recipent
    private ObjectInputStream input; //information being received from the recipent
    private Socket connection; //the connection between you and the recipent's computer

    private String message;
    private String serverIP;

    public Client(String host){
        super("Instant Messenger - Client");
        serverIP = host;
        userText = new JTextField();
        userText.setEditable(false);
        userText.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent event){
                sendMessage(event.getActionCommand());
                userText.setText("");
            }
        });
        add(userText, BorderLayout.NORTH);
        chatWindow = new JTextArea();
        add(new JScrollPane(chatWindow), BorderLayout.CENTER);
        setSize(500, 400);
        setVisible(true);
    }


    //connect to the server, 
    public void startClient(){
        try{
            connectToServer();
            setupStream();
            whileChatting();
        }catch(EOFException eofException){
            showMessage("\n Client terminated the connection");
        }catch(IOException ioException){
            ioException.printStackTrace();
        }finally{
            closeClient();
        }
    }

    private void connectToServer() throws IOException{
        showMessage("Attempting to connect to server...\n");
        connection = new Socket(InetAddress.getByName(serverIP), 6789);
        showMessage("Connection established to: " + connection.getInetAddress().getHostAddress());
    }

    private void setupStream() throws IOException{
        output = new ObjectOutputStream(connection.getOutputStream()); //create pathway for connection to other computer.
        output.flush(); //good practice. Cleans up data that was "left over"

        input = new ObjectInputStream(connection.getInputStream()); //creates pathway to receive data from other PC.
        //no flush for input, the other side has to flash.
        showMessage("Connection now active");
    }

    private void whileChatting() throws IOException{
        String message = " You are now connected ";
        sendMessage(message);
        ableToType(true);
        do{//have conversation while server doesn't send SERVER - END.
            try{
                message = (String) input.readObject(); //read whatever message is incoming.
                showMessage("\n" + message);
            }catch(ClassNotFoundException classNotFoundException){
                showMessage("\n server has sent garbage.");
            }

        }while(!message.equals("SERVER - END") || !message.equals("SERVER - TERMINATE CONNECTION"));
    }

    private void closeClient(){
        showMessage("\n\tTerminating connection...");
        ableToType(false);
        try{
            input.close(); //terminate stream from
            output.close(); //terminate stream to
            connection.close(); //terminate connection
        }catch(IOException ioException){
            ioException.printStackTrace();
        }
    }

    private void sendMessage(final String message){
        try{
            output.writeObject("CLIENT - " + message); //creates an object of message and sends it along the output stream.
            output.flush(); //remove once it's finished sending.
            showMessage("\nCLIENT - " + message);
        }catch(IOException ioException){
            chatWindow.append("\n error sending message"); //print out the error in the chat window that the message was not sent.
        }
    }

    private void showMessage(final String string){
        SwingUtilities.invokeLater( //creates thread that updates parts of the GUI.
                new Runnable(){
                    public void run(){
                        chatWindow.append(string);
                    }
                }        
        );
    }

    private void ableToType(final boolean bool){
        SwingUtilities.invokeLater( //creates thread that updates parts of the GUI.
                new Runnable(){
                    public void run(){
                        userText.setEditable(bool);
                    }
                }        
        );
    }
}

     package chatroomomni;


import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Server extends JFrame{

    private JTextField userText; //where you type in message
    private JTextArea chatWindow; //displays the conversation
    private ObjectOutputStream output;  //information sent to the recipent
    private ObjectInputStream input; //information being received from the recipent
    private ServerSocket server;
    private Socket connection; //the connection between you and the recipent's computer

    //setting up GUI.
    public Server(){
        super("Instant Messanger - Server"); //the piece in the brackets is the title off the app.
        userText = new JTextField();

        userText.setEditable(false);//without having a connection you can't send messages.

        userText.addActionListener(
            new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent event){
                    sendMessage(event.getActionCommand()); //sends string that was typed into text field.
                    userText.setText(""); //after text has been sent, remove the previous message sent.
                }
            }
        );
        add(userText, BorderLayout.NORTH); //add userText to GUI, make chat window top of the screen.
        chatWindow = new JTextArea();
        add(new JScrollPane(chatWindow)); //scrolling pane created for all the chats.

        setSize(450,200);
        setVisible(true);
    }

    //create the server and run the serve.
    public void startServer(){
        try{
            server = new ServerSocket(6789, 100); //Server socket. first one the server socket, the second how many clients can wait for connection.
            while(true){//the loop should run forever. The main program.
                try{//connect and allow conversation with another
                    waitForConnection();//wait for someone to connect with server.
                    setupStream(); //create the connection between another computer.
                    whileChatting(); //allow messages to be sent back and forth during chat.
                }catch(EOFException eofException){ //whenever the conversation is ended.
                    showMessage("\n Server overloaded, connection terminated by the server.");
                }finally{
                    closeServer();
                }
            }
        }catch(IOException ioException){
            ioException.printStackTrace();
        }
    }

    //wait for connection, after connection established display connection details.
    private void waitForConnection() throws IOException{
        showMessage("Waiting for connection to be established...\n"); //so user has something to see, knows program isn't frozen.
        connection = server.accept(); //accept the connection.
        showMessage("Connection established with " + connection.getInetAddress().getHostName());
    }

    //gets stream for send/receiving of data.
    private void setupStream() throws IOException{
        output = new ObjectOutputStream(connection.getOutputStream()); //create pathway for connection to other computer.
        output.flush(); //good practice. Cleans up data that was "left over"

        input = new ObjectInputStream(connection.getInputStream()); //creates pathway to receive data from other PC.
        //no flush for input, the other side has to flash.
        showMessage("Connection now active");
    }

    //sending text back and forth
    //this receives the data from the connected computer and displays it.
    private void whileChatting() throws IOException{
        String message = " You are now connected ";
        sendMessage(message);
        ableToType(true);
        do{//have conversation while client doesn't send CLIENT - END.
            try{
                message = (String) input.readObject(); //read whatever message is incoming.
                showMessage("\n" + message);
            }catch(ClassNotFoundException classNotFoundException){
                showMessage("\n user has sent garbage.");
            }

        }while(!message.equals("CLIENT - END"));
    }

    //close streams/sockets after chat ends. "Good housekeeping". Frees up memory.
    private void closeServer(){
        showMessage("\n\tTerminating connection...");
        ableToType(false);
        try{
            input.close(); //terminate stream from
            output.close(); //terminate stream to
            connection.close(); //terminate connection
        }catch(IOException ioException){
            ioException.printStackTrace();
        }
    }

    //send message to connected computer.
    private void sendMessage(String message){
        try{
            output.writeObject("SERVER - " + message); //creates an object of message and sends it along the output stream.
            output.flush(); //remove once it's finished sending.
            showMessage("\nSERVER - " + message);
        }catch(IOException ioException){
            chatWindow.append("\n error sending message"); //print out the error in the chat window that the message was not sent.
        }
    }

    //shows messages on main chat area. Updates the chatWindow so it is always current.
    private void showMessage(final String string){
        SwingUtilities.invokeLater( //creates thread that updates parts of the GUI.
                new Runnable(){
                    public void run(){
                        chatWindow.append(string);
                    }
                }        
        );
    }

    //sets the textfield editable so that the user can type in it
    private void ableToType(final boolean bool){
        SwingUtilities.invokeLater( //creates thread that updates parts of the GUI.
                new Runnable(){
                    public void run(){
                        userText.setEditable(bool);
                    }
                }        
        );
    }
}

All the netbeans project files uploaded to zippyshare: www116(dot)zippyshare(dot)com/v/qAa5BGWn/file(dot)html (not allowed to post more than 2 links, I apologize for trying to circumvent the rule but it is relevant)

Any help would be appreciated.

Belar
  • 1
  • 2
  • Probable cause - [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/); a reasonable solution - [Worker Threads and SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) – MadProgrammer Mar 25 '16 at 21:11
  • @MadProgrammer thanks for the links, I am also going over [link](https://www.youtube.com/watch?v=O_Ojfq-OIpM) to start learning about multi-threading. Will post in a week or so if I manage to fix it/need more help. Thanks. – Belar Mar 29 '16 at 08:27
  • so I changed it so showMessage created a new thread. `private void showMessage(final String string){ chat.start(); }` And `public class ShowMessage extends SwingWorker{ @Override public Void doInBackground(){ Client.chatWindow.append("hello"); return null; } }` So now the display actually shows up as hello. But how do I do it so it can append the correct chat piece? Do I make a public String of the chat, fetch it and then clear it (or use setter/getter)? (following this: [Java JProgessbr](https://www.youtube.com/watch?v=gjRM1dZQwi0) – Belar Apr 01 '16 at 10:51

0 Answers0