darmajies
darmajies

Reputation: 11

How To Transfer Data over Socket Java

I tried to send file with wifi direct on android, but only file name were sent and receive.. I follow code from here Large file transfer over java socket
here is my code
Sender

Socket socket = new Socket();
        int port = intent.getExtras().getInt(EXTRAS_GROUP_OWNER_PORT);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;
        OutputStream os = null;
        DataOutputStream dos = null;
        try {
            //socket.bind(null);
            socket.connect((new InetSocketAddress("192.168.49.1", port)), SOCKET_TIMEOUT);
            ContentResolver cr = context.getContentResolver();
            Uri uri = null;
            uri = uri.parse(FileUri);
            File myFile = new File(uri.getPath());
            byte[] mybytesarray = new byte[(int) myFile.length()];
            fis = new FileInputStream(myFile);
            bis = new BufferedInputStream(fis);
            dis = new DataInputStream(bis);
            dis.readFully(mybytesarray, 0, mybytesarray.length);
            os = socket.getOutputStream();
            dos = new DataOutputStream(os);
            dos.writeUTF(myFile.getName()); //write fileName
            dos.writeLong(mybytesarray.length); //write file length/size
            int read;
            while ((read = dis.read(mybytesarray)) > 0) {
                dos.write(mybytesarray, 0, read); 
            }
        } catch (IOException e) {
                Log.e(WiFiDirectActivity.TAG, e.getMessage());
            } finally {
            if (socket != null) {
                if (socket.isConnected()) {
                    try {
                        dos.close();
                        os.close();
                        dos.close();
                        socket.close();
                        Log.d(WiFiDirectActivity.TAG, "socket close");
                    } catch (IOException e) {
                        // Give up
                        e.printStackTrace();
                    } 


Receiver

        int current = 0;
        ServerSocket serverSocket = null;
        InputStream in = null;
        OutputStream output = null;
        DataInputStream ClientData;
        try{
        serverSocket = new ServerSocket(8988);
            Socket ClientSocket = null;
            ClientSocket = serverSocket.accept();
            in = ClientSocket.getInputStream();
            ClientData = new DataInputStream(in);
            String FileName = ClientData.readUTF();
            output = new FileOutputStream(Environment.getExternalStorageDirectory()+"/" + FileName);
            long size = ClientData.readLong();
            byte[] buffer = new byte[8192];
            int bytesRead;
            while((bytesRead = ClientData.read(buffer)) > 0){
                output.write(buffer, 0, bytesRead);
            }
            output.flush();
            in.close();
            output.close();
            ClientSocket.close();
            serverSocket.close();
            return FileName;
        } catch (IOException e) {
            Log.e(WiFiDirectActivity.TAG, e.getMessage());
            return null;
        }

Upvotes: 1

Views: 198

Answers (1)

user207421
user207421

Reputation: 311050

            while ((read = dis.read(mybytearray)) > 0) {
                dos.write(mybytearray, 0, mybytearray.length);
                Log.d(WiFiDirectActivity.TAG, "sending");
                dos.flush();
            }

and

        long size = ClientData.readLong();
        byte[] buffer = new byte[1024];
        while((size = ClientData.read(buffer)) > 0){
            output.write(buffer, 0, bytesRead);
            size = bytesRead;
        }

You've mangled this code, and you used the wrong answer in the linked question: see my numerous comments. It should be:

            while ((read = dis.read(mybytearray)) > 0) {
                dos.write(mybytearray, 0, read);
                Log.d(WiFiDirectActivity.TAG, "sending");
            }

(never flush inside a loop), and

        long size = ClientData.readLong();
        byte[] buffer = new byte[8192]; // at least
        int bytesRead;
        while((bytesRead = ClientData.read(buffer)) > 0){
            output.write(buffer, 0, bytesRead);
        }

Upvotes: 1

Related Questions