有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从客户端(Android studio.Java)向服务器(C++)接收损坏或丢失的部分图像

我已经修改了客户端(Android studio:Java)的代码客户端

  @Override
protected Boolean doInBackground(final Bitmap... bitmap) {

    Bitmap imageBitmap = bitmap[0];
    try {
        if (m_socket != null) m_socket.close();

        m_socket = new Socket(m_Address, m_nPort);

        if (imageBitmap != null) {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            imageBitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);

            byte[] array =  bos.toByteArray();

            if(bSendData) { // send data
                OutputStream out = m_socket.getOutputStream();
                DataOutputStream dos = new DataOutputStream(out);

                dos.write(array, 0, array.length);
                dos.flush();
               dos.close();

             }else // send length of data 
            {
                m_pw = new PrintWriter(m_socket.getOutputStream());
                m_pw.write(String.valueOf(array.length));
                m_pw.flush();
                m_pw.close();
            }

        }
        m_socket.close();


        if (imageBitmap != null) {
            m_IsOPrationSuccuses = true;
            publishProgress("Sending ID-Photo to the server successfully");
            return Boolean.TRUE;
        }
    }
    catch (IOException e) {
        e.printStackTrace();
        m_IsOPrationSuccuses = false;
        publishProgress("Failed to connect to the server!"+ e.getMessage());
    }
    return Boolean.FALSE;
}

在c++中,用cSoC:

在C++接收函数上编写了<强>服务器端< /强>
        if (m_nCount == 1)
        {
            char pBuf[512];
            int iBufSize = 512;
            memset(pBuf, 0, sizeof(pBuf));

                        // get the temp of file path and create if doesnt exist
            fpwImage = _wfsopen(GetFullIDImageTempPath(), L"wb", _SH_DENYNO);

            m_nCount = 0;

            int nRead = 1, nTotalRead = 0; ;
            while (nRead > 0 )
            {
                nRead = m_sConnectSocket.Receive(pBuf, iBufSize);    
                 fwrite(pBuf, 1, nRead, fpwImage);

                nTotalRead += nRead;

                TRACE(L"8\n Read[%d] nTotalRead [%d]\n",nRead, nTotalRead);

                if (nRead == 0)
                    break;
            }

            if (nRead == 0) {
                fclose(fpwImage);
                m_nCount = 1;
            }
  }

问题是接收到的图像缺少部分。服务器或客户端是否有任何错误或丢失?我该怎么解决?我使用从客户端接收的文本文件长度,但仍然是相同的问题


共 (0) 个答案