• 3906阅读
  • 2回复

[提问]请教关于TCP通讯问题 [复制链接]

上一主题 下一主题
离线hailmy
 

只看楼主 倒序阅读 楼主  发表于: 2014-04-02
PC -- 客户端
ARM -- 服务端

PC向ARM板通过TCP来发送文件,有个问题

PC端:发送部分代码 这里发送顺序是  文件名长度--文件大小--文件名
QDataStream sendOut(&outBlock, QIODevice::WriteOnly);
sendOut.setByteOrder(QDataStream::LittleEndian);

//获取文件名
QString currentFileName = fileName.right(fileName.size() - fileName.lastIndexOf('/')-1);

qDebug() << currentFileName;
qDebug() << currentFileName.length();

//依次写入总大小信息空间,文件名大小信息空间,文件名
sendOut << currentFileName.length() << totalBytes << currentFileName;

qDebug() << totalBytes;

tcpClient->write(outBlock);

//发送完头数据后剩余数据的大小
bytesToWrite = totalBytes - tcpClient->write(outBlock);

outBlock.resize(0);

-------------------------------------------------
ARM 这边接收的话,大概是这样的
while (1)
{
    printf("########################################\n");
    //文件名长度    
    if (bytes_received == 0)
    {
        cur_bytes_received = recv(*client_fd, buf, sizeof(int), 0);    //4个字节        
        if (cur_bytes_received <= 0)
        {
            send_msg(client_fd, ERROR);
            break;
        }    
        
        printf("%d\n", cur_bytes_received);
                    
        memcpy(&fileNameSize, buf, sizeof(int));
        printf("File Name Size:%d\n", fileNameSize);        
        
        bytes_received += cur_bytes_received;
        
        //break;
    }

    printf("########################################\n");
    bzero(buf, BUFFER_SIZE);
    //usleep(2000*1000);
    
    //文件大小    
    if (bytes_received == sizeof(int))
    {
        cur_bytes_received = recv(*client_fd, buf, sizeof(int), 0);
        if (cur_bytes_received <= 0)
        {
            send_msg(client_fd, ERROR);
            break;
        }    
        
        printf("%d\n", cur_bytes_received);
        memcpy(&totalBytes, buf, sizeof(int));
        printf("File size: %d\n",totalBytes);
        
        bytes_received += cur_bytes_received;            
        
        //break;
    }
    
    printf("########################################\n");
    bzero(buf, BUFFER_SIZE);
    bzero(filename, FILE_NAME_SIZE);
    
    //文件名
    if (bytes_received == sizeof(int)*2)
    {
        cur_bytes_received = recv(*client_fd, buf, fileNameSize, 0);
        if (cur_bytes_received <= 0)
        {
            send_msg(client_fd, ERROR);
            break;
        }    
        
        buf[cur_bytes_received] = '\0';
        memcpy(filename, buf, fileNameSize);
        
        printf("%d\n", cur_bytes_received);
        printf("buf:%s\n", buf);
        printf("File Name:%s\n", filename);

        bytes_received += cur_bytes_received;    
        
        break;            
    }
    
}    

文件名长度跟 文件大小,都能正常读取,但是文件名就是读取不了
输出结果是:
./server
Init Socket Success!
Waiting connect...
Waiting upgrade...
########################################
4
File Name Size:10
########################################
4
File size: 276626
########################################
10
buf:
File Name:
-------
读取返回是说读了10字节,但是里面是空的。

请教各位,感谢了~!
离线dbzhang800

只看该作者 1楼 发表于: 2014-04-02
如果你的接收端不是同版本的Qt,最好不要使用QDataStream。用的话,你必须搞清楚各个类型的序列化方式。
离线hailmy

只看该作者 2楼 发表于: 2014-04-02
嗯,我也想到了这点,PC上是QT,ARM上,是linux下的,确实不太对应,感谢,我再看看
快速回复
限100 字节
 
上一个 下一个