Receiving Android Socket Server Files

Asked 2 years ago, Updated 2 years ago, 87 views

I am making an app that reads mp3 files or wav files on the embedded board and sends them to my smartphone through tcp In c, the following is

struct filestate{ char state[5]; int size; char data[500] }; It consists of 512 bytes I read the file through the feed and transferred it through the write function

In the case of state, we put information that tells us that it is the actual data of the file, and size resembles information that tells us where to read the size of the data that we just sent, and the actual data is in a 500-byte array

Thread code for receiving files on Android

I think the sender sent the data correctly, but something was wrong when I received it, so I took a log on the state side, but the value of Fbody, which is the value of the state that tells me that it is the actual file information, is it possible to get mixed up? I'm wondering what to do in this case

class FReceiveThread extends Thread { boolean isLoop = true;

    public void setIsLoop(boolean isLoop)
    {
        this.isLoop = isLoop;
    }

    @Override
    public void run()
    {
        ServerSocket serverSocket = null;
        try
        {

            Log.i("SocketService", "Start Freceive");

            String state;
            int Fsize = 0;
            int Bsize = 0;
            int j = 0;
            serverSocket        = new ServerSocket(8005);
            Socket socket       = serverSocket.accept();
            //BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            DataInputStream in  = new DataInputStream(socket.getInputStream());
            FileOutputStream fos = null;
            byte[] dataBuff     = new byte[512];
            byte[] stateBuff    = new byte[5];
            byte[] sizeBuff     = new byte[4];
            byte[] BodyBuff     =new byte[500];

            while (isLoop)
            {

                Log.i("Freceive", "TopWhile");

                in.read(dataBuff,0,dataBuff.length);
                System.arraycopy(dataBuff,0,stateBuff,0,5);
                state           = new String(stateBuff);

                Log.i("Freceive", "state:" +state);

                if(state.equals("Finfo"))
                {
                    //Log.i("Freceive", "if(Finfo)" );

                    System.arraycopy(dataBuff,8,sizeBuff,0,4);

                    //Log.i("FreceiveSizeBuff", ""+sizeBuff[0]);
                    //Log.i("FreceiveSizeBuff", ""+sizeBuff[1]);
                    //Log.i("FreceiveSizeBuff", ""+sizeBuff[2]);
                    //Log.i("FreceiveSizeBuff", ""+sizeBuff[3]);

                    Fsize = ((sizeBuff[3]&0xff) << 24) | ((sizeBuff[2]&0xff) << 16) | ((sizeBuff[1]&0xff) << 8) | (sizeBuff[0]&0xff);
                     //int* psize = (intsizeBuff;
                     //Fsize=getInt(sizeBuff);

                    File StoreDir                   = new File(AppDirectory);

                    if( !StoreDir.exists() )
                    {
                        StoreDir.mkdirs();
                    }

                    Log.i("Freceive", ""+Integer.toString(Fsize));

                    file = new File(AppDirectory+"/"+messege);

                    fos = new FileOutputStream(file);

                }
                else if(state.equals("Fbody"))
                {
                    System.arraycopy(dataBuff,8,sizeBuff,0,4);

                    //Log.i("FreceiveSizeBuff", ""+sizeBuff[0]);
                    //Log.i("FreceiveSizeBuff", ""+sizeBuff[1]);
                    //Log.i("FreceiveSizeBuff", ""+sizeBuff[2]);
                    //Log.i("FreceiveSizeBuff", ""+sizeBuff[3]);

                    Bsize = ((sizeBuff[3]&0xff) << 24) | ((sizeBuff[2]&0xff) << 16) | ((sizeBuff[1]&0xff) << 8) | (sizeBuff[0]&0xff);
                    Log.i("Freceive", ""+Integer.toString(Bsize));
                    System.arraycopy(dataBuff,12,BodyBuff,0,500);
                    fos.write(BodyBuff,0,Bsize);
                    //fos.flush();
                    j +=Bsize;
                }
                else if(state.equals("Fend!"))
                {
                    Log.i("FileE","Fclose"+j);
                    j = 0;
                    //Log.i("FileE","Fclose"+j);
                    fos.close();
                }

            }


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

android socket

2022-09-21 19:33

1 Answers

I don't know if it's helpful, but please refer to it. I'll give you a tip to find it without looking at the code.

#pragma pack(1)


2022-09-21 19:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.