Java - InputStream - 测试输入
我分两步将数据发送到服务器: 1) 我将使用 byte[4] 发送的内容的长度 2)数据。
服务器监听数据的确切长度(先发送)然后回复。
所以我监听InputStream并尝试获取数据。
我的问题: 无论我在做什么,我只得到我发送的流,但服务器肯定会发送一个新字符串。
看来我不能等待 -1 (字符串结尾),因为程序会超时,并且我确信服务器不会发送任何类似的内容。
因此,我使用 inputStream.available() 来查找缓冲区中剩余多少字节。
一旦我在读取所有数据后发送 inputStream.read() ,它将因“网络空闲超时”而超时。
但我需要监听 inputStream 以确保我没有丢失信息。
为什么我只收到自己发送的信息,而不收到服务器发送的信息? 如何收听新项目的连接?
这是我的代码:
private void sendData (byte[] sendBytes){
try {
os.write(sendBytes);
os.flush();
} catch (IOException ex) {
}
}
请帮忙 总谐波失真
I am sending data to a server in two steps:
1) Length of what I will send using byte[4]
2) Data.
The server listens to the exact length of the data (shipped first) and then replies.
So I listen to the InputStream and try to get the data.
My Problem:
Whatever I am doing I am getting only the stream I send, but the server definatly sends a new string.
It seems I cannot wait for a -1 (end of string), as the program would time out and I am sure the server does not send anything alike.
Therefore I am using inputStream.available() to find out how many bytes are left in the buffer.
Once I am sending inputStream.read() after reading all the data it will time out with "Network idle timeout".
But I need to listen to the inputStream to make sure I am not missing information.
Why am I only receiving the information I send and not what is send by the server?
How can I listen to the connection for new items coming in?
Here is my code:
private void sendData (byte[] sendBytes){
try {
os.write(sendBytes);
os.flush();
} catch (IOException ex) {
}
}
Please help
THD
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是您通常从阅读器读取所有数据的方式(直到另一端关闭):
//此行上的数据将包含从服务器接收的所有代码
This is how you normally read all data from a reader (until the other end closes):
//data will on this line contain all code received from the server