简体   繁体   中英

Java Async Task / Socket connection problem

i have a Problem with Async Task in Java:

I have a mobile application that sends an image to my server, then receives another image from that server, and then receives 7 int values between 0 and 100 over socket/port. So I have an AsnycTask where i do all the connecting/sending/receiving stuff in DoInBackground-method. Image is working fine, but the problem is the int[]: I have a probabilities[] int-array of length 7where i put the received values (in DoInBackground). Then, in onPostExecute i call my own function in which i need those values.

Now the problem is: Sometimes those values are there, and sometimes the Array still has only 0s (and I know for sure that the data is not only zeros).

Here is my AsnyTask code

    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) may return -1 which means no bytes are read and data2 remains with zeros, so handle and retry if read returns -1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM