質問

client sends the file to server and server receives it and saves it. But when in the Client that Line(while ((len = outputFromServer.read(buf)) != -1)) comes the client stuckes i dont know why? try {

            Log.d(WiFiDirectActivity.TAG, "Opening client socket - ");

            socket.connect((new InetSocketAddress(host, port)),
                    SOCKET_TIMEOUT);

            final File f = new File(
                    Environment.getExternalStorageDirectory()
                            + "/wifip2pshared-"
                            + System.currentTimeMillis() + ".jpg");

            File dirs = new File(f.getParent());
            if (!dirs.exists())
                dirs.mkdirs();
            f.createNewFile();

            // send Data To Server
            OutputStream stream = socket.getOutputStream();
            FileInputStream file = new FileInputStream(
                    "/sdcard/samsung/Image/001" + ".jpg");
            while ((len1 = file.read(buf1)) != -1) {
                stream.write(buf1, 0, len1);
            }
            file.close();
            // ////////////////////////////////
            // ///////////////////////
            // read Data from server
            InputStream outputFromServer = socket.getInputStream();
            FileOutputStream saveData = new FileOutputStream(
                    f);
            while ((len = outputFromServer.read(buf)) != -1) {
                saveData.write(buf, 0, len);
            }
            saveData.close();

            Log.d(WiFiDirectActivity.TAG, "Client: Data written");
        } catch (IOException e) {
            Log.e("exception at client", e.getMessage());
        } finally {
            if (socket != null) {

                if (socket.isConnected()) {
                    try {
                        socket.close();
                    } catch (Exception e) {
                        // Give up
                        Log.e("exception at clientin socket close",
                                e.toString());
                    }
                }
            }
        }

Server Side

try {
            server = new ServerSocket(8988);
            Socket client = server.accept();
            final File f = new File(
                    Environment.getExternalStorageDirectory()
                            + "/wifip2pshared-"
                            + System.currentTimeMillis() + ".jpg");

            File dirs = new File(f.getParent());
            if (!dirs.exists())
                dirs.mkdirs();
            f.createNewFile();

            // receive Data From Client
            InputStream is = client.getInputStream();
            FileOutputStream fos = new FileOutputStream(f);
            String a = "acb";
            while ((len = is.read(buf)) != -1) {
                fos.write(buf, 0, len);
                Log.e("In server reviving data", a);
            }
            fos.close();
            // Send Data To Client
            OutputStream stream = client.getOutputStream();

            FileInputStream file = new FileInputStream(
                    "/sdcard/samsung/Image/001" + ".jpg");
            while ((len1 = file.read(buf1)) != -1) {
                stream.write(buf1, 0, len1);
            }
            file.close();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

client sends the file to server and server receives it and saves it. But when in the Client that Line(while ((len = outputFromServer.read(buf)) != -1)) comes the client stuckes i dont know why?

役に立ちましたか?

解決

read() on a socket stream will return -1 only if connection is closed or an error occurs. Server receives the data and saves it but never leaves the receiver loop to send data. Even if it would do the client wouldn't then leave its receiver loop either.

You must either close the connection or send file size before the actual file and receiving stops when given size was read.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top