簡體   English   中英

如何通過Socket Java傳輸數據

[英]How To Transfer Data over Socket Java

我試着將文件發送與WIFI直接在Android上,但只有文件名被送往和接受。我跟隨代碼從這里過的Java套接字大文件傳輸
這是我的代碼
寄件人

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();
                    } 


接收器

        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;
        }
            while ((read = dis.read(mybytearray)) > 0) {
                dos.write(mybytearray, 0, mybytearray.length);
                Log.d(WiFiDirectActivity.TAG, "sending");
                dos.flush();
            }

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

您已經弄亂了這段代碼,並且在鏈接的問題中使用了錯誤的答案:請參閱我的大量評論。 它應該是:

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

(切勿在循環內沖洗),以及

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

暫無
暫無

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

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