DataInputStream的Java Socket]实例是print continue null

时间:2015-12-02 09:22:22

标签: java sockets datainputstream

我正在使用Java Swing制作聊天室程序。

在客户端,我看到没有从服务器端读取消息。

客户端的writeUTF()方法非常好,我在服务器端检查readUTFwriteUTF,这也非常好。

我认为问题是代码在客户端作为“Receiver”。

在线程的run()方法中,dis的实例DataInputStream持续为空值。

我很困惑..请给我一些帮助。

下面是我的客户端和服务器代码的一部分。

谢谢!

客户代码

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("");
            }
        });
    }
}

现在服务器代码。

服务器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);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

当您在ChatReceiver中启动RoomBackground线程时,dis对象尚未初始化,这就是null的原因。一种解决方案可能是初始化dis构造函数中的ChatReceiver变量。