Search code examples
javasocketsdatainputstream

Java Socket ] instance of DataInputStream is print continue null


I'm making Chatting-Room program using the Java Swing.

In the client side, I was saw that doesn't read message from the server side.

The writeUTF() method of the client side is very well and I'm checked readUTF and writeUTF on the server side, that was very well too.

I think the problem is code which does as "Receiver" on the client side.

In the run() method of Thread, The instance dis of the DataInputStream has continuously null value.

I'm so confusing.. Please give me some help.

The bellow is part of my client&server code.

Thanks!

Client code

RoomBackground.java

public class RoomBackground {

    private static String socket_server = "127.0.0.1";
    private static Socket chatSocket;
    private static DataOutputStream dos;
    private static DataInputStream dis;
    private ChatReceiver chatReceiver;

    public Socket getChatSocket() {
        return chatSocket;
    }

    public static DataOutputStream getDos() {
        return dos;
    }

    public RoomBackground() throws IOException {
        chatSocket = new Socket(socket_server, 7777);
        chatReceiver = new ChatReceiver();
        chatReceiver.start();

        dos = new DataOutputStream(chatSocket.getOutputStream());
        dis = new DataInputStream(chatSocket.getInputStream());
        dos.writeUTF(User.getUser().getUsername());
        dos.flush();
    }
    class ChatReceiver extends Thread {
        @Override
        public void run(){
            try {
                # PROBLEM CODE..... Allways "dis is null"
                System.out.println("dis is " + dis);

                # This line never executed.
                while(dis != null) {    

                    # some codes.....

                }  
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println(e.toString());
            } catch (BadLocationException e) {
                e.printStackTrace();
            }
        }
    }
}

RoomFrame.java

public class RoomFrame extends JFrame{

    private RoomBackground roomBackground;

    public RoomFrame(int roomId) throws IOException {
        chatField.addActionListener(new ActionListener() {
            roomBackground  = new RoomBackground();
            @Override
            public void actionPerformed(ActionEvent e) {
                String msg = chatField.getText() + "\n";
                try {
                    RoomBackground.getDos().writeUTF(msg);

                    # It works.
                    System.out.println("sent msg is " + msg);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                chatField.setText("");
            }
        });
    }
}

Now server code.

Server Background.java

public class ChatReceiver extends Thread {

    private DataInputStream in;
    private DataOutputStream out;

    public ChatReceiver(Socket chatSocket) throws IOException {

        out = new DataOutputStream(chatSocket.getOutputStream());
        in = new DataInputStream(chatSocket.getInputStream());

        nick = in.readUTF();
        addChatClient(nick, out);    
    }

    @Override
    public void run() {
        try {
            while(in!=null) {
                chatMsg = in.readUTF();

                # It works !
                System.out.println("before send" + chatMsg);
                sendMsg(chatMsg);

                # It works too!
                System.out.println("after send" + chatMsg);
            }
        }catch (IOException e) {
            removeChatClient(nick);
        }
    }
}

Solution

  • When you are starting the ChatReceiver thread in the RoomBackground the dis object is not initialized yet, that is why it is null. One solution could be to initialize the dis variable in the ChatReceiver constructor.