qq_26470013
skyblue_2012
2019-03-24 18:20

android客户端与qt服务端通过socket通信传图片的问题

  • c++
  • android-studio
  • android

android客户端:
public void sendData(final String filepath) {
new Thread() {
@Override
public void run() {
try {
Socket socket = new Socket();
// Socket socket = new Socket("192.168.1.108", 8888);//创建Socket实例,并绑定连接远端IP地址和端口
socket.connect(new InetSocketAddress("192.168.1.108", 8888));
Log.i("线程反馈1", "创建成功!");
// DataOutputStream out = new DataOutputStream(socket.getOutputStream());
// out.writeUTF("Helloworld");
//获取输出流
OutputStream ou = socket.getOutputStream();
//读取服务器响应
BufferedReader bff = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
String line = null;
String buffer = "";
while ((line = bff.readLine()) != null) {
buffer = line + buffer;
}
//向服务器发送文件
File file = new File(filepath);
if (file.exists()) {
FileInputStream fileInput = new FileInputStream(filepath);
DataOutputStream dos = new DataOutputStream(ou);
// 文件名
dos.writeUTF(file.getName());
byte[] bytes = new byte[1024];
int length = 0;
while ((length = fileInput.read(bytes)) != -1) {
dos.write(bytes, 0, length);
}
//告诉服务端,文件已传输完毕
socket.shutdownOutput();
fileInput.close();
dos.close();
}
//关闭各种输入输出流
ou.flush();
// bff.close();
ou.close();
socket.close();
} catch (Exception e) {
Log.i("线程反馈", "线程异常!");
}
}
}.start();
}
qt服务端:
void MainWindow::readMessage()
{
QByteArray array;
quint32 data_len = 0;
quint32 len = 0xffffffff;
//struct Head head;

while(m_tcpSocket->waitForReadyRead(5000)){
    qDebug()<<"bytesAvailable"<<endl;
    if(data_len == 0){
        QDataStream in(m_tcpSocket);
        in >> len;
        qDebug("datalen:%d",len);
    }
    data_len += m_tcpSocket->bytesAvailable();
    array.append((QByteArray)m_tcpSocket->readAll());
    qDebug()<<data_len<<endl;
    if(data_len == len){
        qDebug()<<"ok,all bytes revd"<<endl;
        break;
    }
}
qDebug()<<array;
qDebug("recvd len:%d",data_len);

QPixmap picture;
picture.loadFromData(array,"jpeg");
picture.save("F:/test/test.jpg","jpg",-1);

}
是哪出了问题,传字符串还有反应,传图片一直没反应

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

0条回答

为你推荐