Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 写入字节数组时读取循环中的套接字InputStream_Java_Sockets - Fatal编程技术网

Java 写入字节数组时读取循环中的套接字InputStream

Java 写入字节数组时读取循环中的套接字InputStream,java,sockets,Java,Sockets,这是您在发送文本数据时通常执行的操作 // Receiver code while (mRun && (response = in.readLine()) != null && socket.isConnected()) { // Do stuff } // Sender code printWriter.println(mMessage); printWriter.flush(); 但是当使用DataOutputSt

这是您在发送文本数据时通常执行的操作

// Receiver code
while (mRun && (response = in.readLine()) != null && socket.isConnected()) {
    // Do stuff                 
}

// Sender code
printWriter.println(mMessage);
printWriter.flush();
但是当使用
DataOutputStream#write(byte[])
发送
byte[]
时,如何在
循环接收发送的数据时编写

我所发现的只是这个,但它不会循环,所以我猜它只会在第一条发送的消息上运行:

int length = in.readInt();
byte[] data = new byte[length];
in.readFully(data);
我怎样才能做到这一点

PS:是的,我是套接字编程新手

编辑:我每3到5秒发送一个字节数组。这就是我到目前为止得到的

// In the client side, in order to send byte[]. This is executed each 3 seconds.
if(out != null) {
    try {
        out.writeInt(encrypted.length);
        out.write(encrypted);
        out.writeInt(0);
        out.flush();
        return true;

    } catch (IOException e) {
        e.printStackTrace();

        return false;
    }
}


 // In the serverside, in order to receive byte[] sent from client (also executed 3 to 5 seconds due to bytes being sent at said rate.  "client" being the Socket instance.
while(true && client.isConnected()) {

    byte[] data = null;

    while(true) {
        int length = in.readInt();
        if(length == 0)
            break;

        data = new byte[length];
        in.readFully(data);
    }

    if(data != null) {
        String response = new String(data);

        if(listener != null) {
            listener.onMessageReceived(response);
        }
    }
}

假设您正在尝试处理一个消息流,听起来您缺少的是一种指定(在流中)消息大小(或结束位置)的方法

我建议您在每条消息之前写一个前缀,指定长度:

output.writeInt(data.length);
output.write(data);
然后在阅读时:

while (true)
{
    int length = input.readInt();
    byte[] buffer = new byte[length];
    input.readFully(buffer, 0, length);
    // Process buffer
}

您还需要找到一种检测输入结束的方法<据我所知,code>DataInputStream
没有一种干净的方法来检测这一点。有多种选择—最简单的方法可能是写出长度为0的消息,如果读取长度为0,则中断循环。

您是否尝试过该代码?为什么它不起作用?你的问题不是很清楚,因为我不知道while循环的表达式是什么,我还是不明白。我猜你什么时候做
bufferedReader.readLine()
它一直在等待换行符。那么你的意思是我应该循环直到一个字符序列被满足吗?@ChristopherFrancisco:不,我根本没有这么说-如果你在写一个字节数组,就没有字符序列这样的东西。您可以使用字节序列,但通常最好只使用长度前缀。我明白了。当长度为0时,我应该将
if
语句放在哪里来中断循环?在
readFully()
?好的,现在,在发送部分,我将
output.writeInt(0)
放在
output.write(byte[])之后。
对吗?@ChristopherFrancisco:好的,你会把它放在你所有的循环之外——基本上是在你发送完所有消息之后。(如果您只发送一条消息,则不需要任何这些…)