求助,qtcpsocket 接收数据,不完整

oShaMoZhiYing12345 2016-07-16 09:45:48
接收端
bool b_res=this->waitForNewConnection(-1);
if(b_res==false)
continue;

//received new connection
QTcpSocket *p_tcpsocket=this->nextPendingConnection();
将p_tcpsocket赋给HandleConnection的mp_connection变量。
int HandleConnection::handle_connection()
{
int n_res=0;
int n_lenrec=0;
while(true)
{
int n_state=mp_connection->state();

qDebug("the state of connection is %d\n",n_state);
if(n_state!=QAbstractSocket::ConnectedState)
break;

bool b_res=mp_connection->waitForReadyRead();
//time out,return
if(!b_res)
{
QString qstr_err=mp_connection->errorString();
QByteArray ba = qstr_err.toLatin1();

char *pc_err=ba.data();
qDebug("the error of waitforreadyread is %s\n",pc_err);
n_res=ERR_TIME_OUT;
break;
}

qint32 n_port=mp_connection->peerPort();
QByteArray bary_port=QByteArray((char*)&n_port,sizeof(qint32));

QHostAddress addr_src=mp_connection->peerAddress();
qint32 n_addr=addr_src.toIPv4Address();
QByteArray bary_addr=QByteArray((char*)&n_addr,sizeof(qint32));

int n_lenavailable=mp_connection->bytesAvailable();
//store data received in bary_datagram
QByteArray bary_datagram=mp_connection->readAll();

if(n_lenavailable!=bary_datagram.size())
qDebug("the length availalbe is not equal to the data received!\n");

n_lenrec+=bary_datagram.size();
//insert source ip and port in the head of bary_datagram;
bary_datagram.insert(0,bary_port);
bary_datagram.insert(0,bary_addr);
HandleMsg *p_handlemsg=new HandleMsg(bary_datagram,mp_dealmsg);
n_res=p_handlemsg->handle_msg();

}

qDebug("the length of data received in all is %d\n",n_lenrec);
if(mp_workthread)
mp_workthread->quit();

return n_res;
}


发送端:
int CmSender::send_data_tcp()
{
QTcpSocket *p_tcpsocket=new QTcpSocket();
p_tcpsocket->abort();

quint16 n_porttmp=mn_port;
p_tcpsocket->connectToHost(maddr_dst,n_porttmp);

bool b_res=p_tcpsocket->waitForConnected();
if(b_res==false)
return ERR_TCP_CONNECT;

//put the whole length of datagram in the fist 8 bytes.
//the whole length include the 8 bytes taken by the length.
quint64 nlen_data=mbary_datagram.size();
quint64 nlen_all=nlen_data+sizeof(quint64);

mbary_datagram.insert(0,(char*)&nlen_all,sizeof(quint64));
quint64 nlen_written=0;

int i=0;
while(nlen_written < nlen_all)
{
qDebug("the num in while is %d\n",++i);
QByteArray bary_send=mbary_datagram.right(nlen_all-nlen_written);
qint64 nlen_send=p_tcpsocket->write(bary_send);

qDebug("the length of data sent is %d\n",nlen_send);
p_tcpsocket->flush();


if(nlen_send==-1)
return ERR_SOCKET_WRITE;

nlen_written+=nlen_send;
}

b_res=p_tcpsocket->waitForBytesWritten();
QString str_error=p_tcpsocket->errorString();
QByteArray ba_error=str_error.toLatin1();
char *pc_err=ba_error.data();
qDebug("the error of send socket is %s\n",pc_err);

if(b_res==false)
return ERR_SOCKET_WAITWRITE;

//sleep(30);

p_tcpsocket->close();
p_tcpsocket->deleteLater();
return 0;
}


发送端发送1M的数据,接收端总共只能收到前500多K,而且最后接收端都报连接已经disconnect。求助各位大神,什么原因?折磨好久了。。。。
...全文
672 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
tyousi 2016-07-21
  • 打赏
  • 举报
回复
不用waitForReadyRead,使用信号readyRead,在槽函数中readAll

16,216

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧