Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.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
C# 如何使每个flush()工作,使每个缓冲区在C中的下一个write()之前发送_C#_Android_Sockets_Image Processing - Fatal编程技术网

C# 如何使每个flush()工作,使每个缓冲区在C中的下一个write()之前发送

C# 如何使每个flush()工作,使每个缓冲区在C中的下一个write()之前发送,c#,android,sockets,image-processing,C#,Android,Sockets,Image Processing,当我在屏幕上移动鼠标时,我正试图发送截图。因此,我将发送屏幕截图和图像字节[]的大小。在客户端,我将根据从服务器接收到的大小设置大小字节数组,并将字节[]的其余部分作为图像读取。理想情况下,每次调用write时,我需要写入的字节[]应该为空,因此我使用flush。事实上,在我收到getinputstream之后,有时我会在其中获取旧数据。我不知道为什么同花顺不起作用。有谁有更好的办法来解决这个问题吗 这是我的服务器代码,我尝试使用stream和networkstream,它可以正常工作 客户端:

当我在屏幕上移动鼠标时,我正试图发送截图。因此,我将发送屏幕截图和图像字节[]的大小。在客户端,我将根据从服务器接收到的大小设置大小字节数组,并将字节[]的其余部分作为图像读取。理想情况下,每次调用write时,我需要写入的字节[]应该为空,因此我使用flush。事实上,在我收到getinputstream之后,有时我会在其中获取旧数据。我不知道为什么同花顺不起作用。有谁有更好的办法来解决这个问题吗

这是我的服务器代码,我尝试使用stream和networkstream,它可以正常工作

客户端:

public class connection extends AsyncTask {
    private byte[] data;

    public void setByteSize(int size) {
        data = new byte[size];

    }

    public byte[] getByteSize() {
        return data;

    }

    @Override
    protected Object doInBackground(Object... arg0) {

        try {
            clientSocket = new Socket("134.129.125.126", 8080);
            // clientSocket = new Socket("134.129.125.172", 8080);
            System.out.println("client connect to server");
            input = clientSocket.getInputStream();
            System.out.println("getinputstream");
        } catch (UnknownHostException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        while (true) {
            int totalBytesRead = 0;
            int chunkSize = 0;
            int tempRead = 0;
            String msg = null;
            // byte[] data = null;

            byte[] tempByte = new byte[1024 * 1024 * 4];

            try {
                // read from the inputstream
                tempRead = input.read(tempByte);
                // tempbyte has x+5 byte

                System.out.println("Fist time read:" + tempRead);
                // convert x+5 byte into String

                String message = new String(tempByte, 0, tempRead);
                msg = message.substring(0, 5);
                int msgSize = Integer.parseInt(msg);
                data = new byte[msgSize];
                // for(int i =0;i<tempRead-5;i++)
                // {
                // data[i]=tempByte[i+5];
                // }
                for (int i = 0; i < msgSize; i++) {
                    data[i] = tempByte[i + 5];
                }
                // cut the header into imageMsg
                // String imageMsg = new String(tempByte, 5, tempRead - 5);
                // convert string into byte
                System.out.println("message head:" + msg);
                byteSize = Integer.parseInt(msg);
                System.out.println("ByteSize:" + byteSize);
                // convert String into byte[]

                totalBytesRead = tempRead - 5;
                System.out.println("total Byte Read=" + totalBytesRead);

            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            // get string for the size of image

            try {

                while (totalBytesRead != getByteSize().length) {
                    System.out.println("data length:"
                            + getByteSize().length);
                    chunkSize = input.read(getByteSize(), totalBytesRead,
                            getByteSize().length - totalBytesRead);
                    System.out.println("chunkSize is " + chunkSize);

                    totalBytesRead += chunkSize;
                    // System.out.println("Total byte read "
                    // + totalBytesRead);

                }

                System.out.println("Complete reading - total read = "
                        + totalBytesRead);
            } catch (Exception e) {

            }

            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

            System.out.println("deco");

            runOnUiThread(new Runnable() {
                public void run() {

                    image.setImageBitmap(bitmap);
                    System.out.println("setImage at less than 500");

                }
            });
        }

    }

}

不要用同花顺。你只是想通过黑客破解的方式来避免正确编写代码。我建议您只正确地编写代码:正确处理基于流的连接,并以通常的方式处理部分写入和读取,方法是检查写入和读取的返回值,并根据需要发出额外的写入和读取操作,以写入或读取所有数据。是,使用或不使用flush没有什么不同。我调试的程序,它显示当我移动鼠标非常快,有时有两个图像一起发送到客户端缓冲区,因此我不能正确读取消息头。
public class connection extends AsyncTask {
    private byte[] data;

    public void setByteSize(int size) {
        data = new byte[size];

    }

    public byte[] getByteSize() {
        return data;

    }

    @Override
    protected Object doInBackground(Object... arg0) {

        try {
            clientSocket = new Socket("134.129.125.126", 8080);
            // clientSocket = new Socket("134.129.125.172", 8080);
            System.out.println("client connect to server");
            input = clientSocket.getInputStream();
            System.out.println("getinputstream");
        } catch (UnknownHostException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        while (true) {
            int totalBytesRead = 0;
            int chunkSize = 0;
            int tempRead = 0;
            String msg = null;
            // byte[] data = null;

            byte[] tempByte = new byte[1024 * 1024 * 4];

            try {
                // read from the inputstream
                tempRead = input.read(tempByte);
                // tempbyte has x+5 byte

                System.out.println("Fist time read:" + tempRead);
                // convert x+5 byte into String

                String message = new String(tempByte, 0, tempRead);
                msg = message.substring(0, 5);
                int msgSize = Integer.parseInt(msg);
                data = new byte[msgSize];
                // for(int i =0;i<tempRead-5;i++)
                // {
                // data[i]=tempByte[i+5];
                // }
                for (int i = 0; i < msgSize; i++) {
                    data[i] = tempByte[i + 5];
                }
                // cut the header into imageMsg
                // String imageMsg = new String(tempByte, 5, tempRead - 5);
                // convert string into byte
                System.out.println("message head:" + msg);
                byteSize = Integer.parseInt(msg);
                System.out.println("ByteSize:" + byteSize);
                // convert String into byte[]

                totalBytesRead = tempRead - 5;
                System.out.println("total Byte Read=" + totalBytesRead);

            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            // get string for the size of image

            try {

                while (totalBytesRead != getByteSize().length) {
                    System.out.println("data length:"
                            + getByteSize().length);
                    chunkSize = input.read(getByteSize(), totalBytesRead,
                            getByteSize().length - totalBytesRead);
                    System.out.println("chunkSize is " + chunkSize);

                    totalBytesRead += chunkSize;
                    // System.out.println("Total byte read "
                    // + totalBytesRead);

                }

                System.out.println("Complete reading - total read = "
                        + totalBytesRead);
            } catch (Exception e) {

            }

            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

            System.out.println("deco");

            runOnUiThread(new Runnable() {
                public void run() {

                    image.setImageBitmap(bitmap);
                    System.out.println("setImage at less than 500");

                }
            });
        }

    }

}