簡體   English   中英

Java Async Task / Socket 連接問題

[英]Java Async Task / Socket connection problem

我在 Java 中遇到異步任務問題:

我有一個移動應用程序將圖像發送到我的服務器,然后從該服務器接收另一個圖像,然后通過套接字/端口接收 0 到 100 之間的 7 個 int 值。 所以我有一個 AsnycTask,我在 DoInBackground 方法中完成所有連接/發送/接收的工作。 圖像工作正常,但問題是 int[]:我有一個長度為 7 的概率 [] int-array 我將接收到的值(在 DoInBackground 中)。 然后,在 onPostExecute 我調用我自己的 function ,我需要這些值。

現在的問題是:有時這些值在那里,有時數組仍然只有 0(我確信數據不僅是零)。

這是我的 AsnyTask 代碼

    class ConnectTask extends AsyncTask<Void, Void, Void> {
                @Override
                protected Void doInBackground(Void... voids) {
                    try {
                        //Connect to my PC and send my image from my phone
                        Socket photoSocket = new Socket(IP_ADDRESS, PORT_NO);
                        DataOutputStream dos = new DataOutputStream(photoSocket.getOutputStream());
                        FileInputStream fis = new FileInputStream(pathname);
                        int size = fis.available();
                        byte[] data = new byte[size];
                        fis.read(data);
                        dos.write(data);
                        dos.flush();
                        fis.close();
                        dos.close();
                        photoSocket.close();
    
                        //now receive the other image from server
                        ServerSocket serverSocket = new ServerSocket(1235);
                        Socket incomingSocket = serverSocket.accept();
                        InputStream is = incomingSocket.getInputStream();
                        FileOutputStream fos = new FileOutputStream(Environment.getExternalStorageDirectory() + "/image_from_server.jpg");
                        BufferedOutputStream bos = new BufferedOutputStream(fos);
                        byte[] aByte = new byte[40000];
                        int bytesRead;
    
                        while ((bytesRead = is.read(aByte)) != -1) {
                            bos.write(aByte, 0, bytesRead);
                        }
    
                        is.close();
                        fos.close();
                        bos.close();
                        serverSocket.close();
                        incomingSocket.close();
    
    
                        //now Receive Values (ints)
                        ServerSocket serverSocket2 = new ServerSocket(1236);
                        Socket incomingSocket2 = serverSocket2.accept();
                        InputStream inputstream = incomingSocket2.getInputStream();
                        byte[] data2 = new byte[7];
                        inputstream.read(data2);
                        //Fill array with received ints THIS IS SOMETIMES WORKING, SOMETIMES probabilities[] STAYS FILLED WITH 0s
                        for (int i = 0; i < data2.length; i++){
                            probabilities[i] = data2[i];
                        }
    
                        serverSocket2.close();
                        incomingSocket2.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return null;
                }
    
                @Override
                protected void onPreExecute() {
                //irrelevant
                }
    
                @Override
                protected void onPostExecute(Void aVoid) {
                    File xaifile = new File(Environment.getExternalStorageDirectory() + "/image_from_server.jpg");
//...doing some UI stuff
//..................
//..................
                    //now call function where i replace picture. Since this is in the "postExecute" method, my probabilities[] should have been filled with data since its in the doInBackground method.
                    replacePicture(xaifile);
                }
            }
    
            ConnectTask connect =  new ConnectTask();
            Void[] param = null;
            //execute Async task
            connect.execute(param);

inputstream.read(data2) 可能返回 -1,這意味着沒有讀取任何字節,並且 data2 保持為零,因此如果 read 返回 -1,則處理並重試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM