char buffer[100];
QTcpSocket *clientConnection =tcpServer->nextPendingConnection() ;
if( clientConnection->waitForReadyRead() == true) //注意::读取要设成阻塞状态 因为客户端的也是阻塞写
{
QMessageBox::warning(NULL,"提示"," 接受信息成功 !",QMessageBox::Yes); //这句话放错位置,怎个程序都错误
clientConnection->read(buffer,100);
}
心太累了, QMessageBox::warning(NULL,"提示"," 接受信息成功 !",QMessageBox::Yes); 这句话放错位置,怎个程序都错误,就会遇到address is already in use ,listen error!
的错误,说明了写程序时,要严谨。
为什么QMessageBox::warning(NULL,"提示"," 接受信息成功 !",QMessageBox::Yes); 会造成读取信息失败呢?
我猜测是因为弹出的对话框,把缓冲区里的信息清除,原因未明白,感兴趣的小伙伴吗,可以去研究一下
除此之外,我运行程序的时候老是有
"The bound address is already in use"
listen error!
tcpServer = new QTcpServer(this);
if(!tcpServer->listen(QHostAddress::AnyIPv4,9000)) //网络端口//if(!tcpServer->listen(QHostAddress::LocalHost,9000))
{
//监听本地主机的9000端口,如果出错就输出错误信息,并关闭
qDebug() << tcpServer->errorString();
qDebug()<<" listen error!";
close();
// exit(0);
}
我经过调试之后,发现了,socket多次使用,要在if之外,加上close();
{
tcpServer = new QTcpServer(this);
if(!tcpServer->listen(QHostAddress::AnyIPv4,9000)) //网络端口//if(!tcpServer->listen(QHostAddress::LocalHost,9000))
{
//监听本地主机的9000端口,如果出错就输出错误信息,并关闭
qDebug() << tcpServer->errorString();
qDebug()<<" listen error!";
close();
// exit(0);
}
……
……
……
在最后添加: close();
}
empty