DataInputStream显示0数据长度

时间:2013-10-06 13:06:53

标签: java

我创建了一个客户端,用于从套接字接收数据并尝试将数据存储到char数组中。 这就是我试图这样做的方式。

...     
public void run() {
        try {

           DataInputStream m_DataInputStream = new DataInputStream(s.getInputStream()); //s is socket object
           BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
            while ((msg = br.readLine()) != null) {
                System.out.println(msg+" >msg");

                try {
                    m_DataInputStream.readByte();
                    int len = available();
                    System.out.println("datalength "+len);
                    char[] c = new char[len];
                    int i = 0;
                    while (i < len) {
                        c[i] = (char) m_DataInputStream.readByte();
                        //System.out.print(c);
                        i++;
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (msg.equals("Server is Shutting Down")) {
                    break;
                }
            }
            s.close();
            br.close();
            m_DataInputStream.close();
        } catch (Exception E) {
            System.out.println(E+" :in Threadrecieve run method");
        }
    }

   public int available() {
    try {
        return m_DataInputStream.available();
    } catch (IOException e) {
        return -1;
    }
}

问题是m_DataInputStream的长度每隔一次显示0.请帮助确定我做错了什么。

0 个答案:

没有答案