【问题标题】:send image from server to client and from client to server将图像从服务器发送到客户端以及从客户端发送到服务器
【发布时间】:2013-06-16 15:51:11
【问题描述】:

客户端将文件发送到服务器,服务器接收并保存它。但是当在客户端那条 Line(while ((len = outputFromServer.read(buf)) != -1)) 来客户端时,我不知道为什么? 试试 {

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

服务器端

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

客户端将文件发送到服务器,服务器接收并保存它。但是当在客户端那条 Line(while ((len = outputFromServer.read(buf)) != -1)) 出现时,客户端卡住了,我不知道为什么?

【问题讨论】:

    标签: android serversocket tclientsocket


    【解决方案1】:

    仅当连接关闭或发生错误时,套接字流上的read() 才会返回 -1。服务器接收数据并保存它,但永远不会离开接收器循环来发送数据。即使它会这样做,客户端也不会离开它的接收器循环。

    您必须在实际文件和接收停止之前关闭连接或发送文件大小在读取给定大小时。

    【讨论】:

    • thnx...兄弟......我已经解决了我的问题,因为你的提示......再次感谢......
    猜你喜欢
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 1970-01-01
    相关资源
    最近更新 更多