我创建了一个客户端来从套接字接收数据并尝试将数据存储到 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。请帮助确定我做错了什么。