用Qt和Linux下服务器TCP通信。
附上发送函数
void MySocket::subpackage()//类似分包
{
int sendNUmber = m_strMsgType.len;
int serial_number = sendNUmber/MAXDATASEND; // 总点集/一次发送的字节
int i = 0;
for(; i<serial_number; i++)多次发送
{
char array[1265] = {0};
m_strMsgType.len = MAXDATASEND;
memcpy(array,&m_strMsgType,HEDERLEN);//拷贝数据包头到array
memcpy(array+HEDERLEN,m_pSendDatabuf+i*MAXDATASEND,MAXDATASEND*sizeof(QPoint));//拷贝数据到array
m_PcliSocket->write(array,HEDERLEN+MAXDATASEND*sizeof(QPoint)+1);
bool abcd = m_PcliSocket->waitForBytesWritten(30000);//等待发送缓冲区为空
qDebug() << "return is " << abcd << endl;
//RecvThread::SLEEP(200);
int cao = m_PcliSocket->bytesToWrite();
qDebug() << "cao is " << cao << endl;
}
unsigned int last = 0;
if(i>0)
last = sendNUmber%MAXDATASEND;
else
last = sendNUmber - (i)*MAXDATASEND;
if(last > 0 || i==0)//最后剩下不足1200字节的最后发送一次
{
char array[1265] = {0};
m_strMsgType.len = last;
memcpy(array,&m_strMsgType,HEDERLEN);
memcpy(array+HEDERLEN,&m_pSendDatabuf[sendNUmber-last],last*sizeof(QPoint));//每次数据1265个字节,其中1200个是数据
m_PcliSocket->write(array,HEDERLEN+last*sizeof(QPoint)+1);
}
qDebug() << "shiji fasong cishu is " << count << endl;
m_strMsgType.len = 0;
}
以下是接收函数
void MySocket::RecvData(msgType& pRecvdata)
{
memset(&pRecvdata,'0',sizeof(msgType));
m_PcliSocket->waitForReadyRead(30000);
QByteArray recvdata=m_PcliSocket->read(1265);//每次只读1265个字节
{
char *ch;
ch = recvdata.data();
memcpy(&pRecvdata,ch,sizeof(msgType));//收到数据,存入pRecvdat
if(pRecvdata.type[0] == 'T' && (pRecvdata.type[1] == None) )//即为None
{
qDebug() << "kaishi recv len" << pRecvdata.len << endl;
if(m_pRecvDatabuf == NULL)
m_pRecvDatabuf = new QPoint[MAXSENDNUMBER+1];//