qt socket 网络编程,不知道哪出了问题
客户端QStringstring="<table><id>1212</id><name>name</name></table>";QHostAddressaddr("12...
客户端QString string="<table><id>1212</id><name>name</name></table>";
QHostAddress addr("127.0.0.1");
socket.connectToHost(addr,7777);
forever
{
if(socket.waitForConnected(50))
break;
}
QByteArray array;
QDataStream stream(&array,QIODevice::WriteOnly);
stream.setVersion(QDataStream::Qt_4_6);
stream<<string;
socket.write(array);
forever
{
if(socket.waitForDisconnected(50))
break;
}
服务器端:
QTcpServer server;
QString string;
server.listen(QHostAddress::Any,7777);
forever
{
if(server.waitForNewConnection())
break;
}
std::cout<<"new connect"<<std::endl;
QTcpSocket *client=server.nextPendingConnection();
QDataStream datastream(client);
forever
{
std::cout<<"ddd"<<std::endl;
if(client->waitForReadyRead(50))
break;
}
datastream>>string;
std::cout<<string.toStdString().length();
client->disconnectFromHost(); 展开
QHostAddress addr("127.0.0.1");
socket.connectToHost(addr,7777);
forever
{
if(socket.waitForConnected(50))
break;
}
QByteArray array;
QDataStream stream(&array,QIODevice::WriteOnly);
stream.setVersion(QDataStream::Qt_4_6);
stream<<string;
socket.write(array);
forever
{
if(socket.waitForDisconnected(50))
break;
}
服务器端:
QTcpServer server;
QString string;
server.listen(QHostAddress::Any,7777);
forever
{
if(server.waitForNewConnection())
break;
}
std::cout<<"new connect"<<std::endl;
QTcpSocket *client=server.nextPendingConnection();
QDataStream datastream(client);
forever
{
std::cout<<"ddd"<<std::endl;
if(client->waitForReadyRead(50))
break;
}
datastream>>string;
std::cout<<string.toStdString().length();
client->disconnectFromHost(); 展开
2个回答
展开全部
倒数第二句应该是std::cout<<string.toStd(String().length());吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询