之前一个QT socket程序在升级系统后,突然没办法正常工作了。用socket client测试程序,能够连接上tcp server。但是QTcpServer中的incomingConnection函数没有被触发执行。同样的代码在debian7上和windows上都可以执行,但是在debain8就不能正常工作。后来发现,虚函数的incomingConnection的函数参数改变了,有void TripServer::incomingConnection(int socketId) 变成了void TripServer::incomingConnection(qintptr socketId)。参数类型不同,所以函数定义也就不同了。将参数类型有int改成qaintptr就好了。好贱的问题呀。附上server代码
#include "chat_server.h"
#include "my_socket.h"
#include <QHostAddress>
ChatServer::ChatServer( QObject *parent /* = NULL */ )
: QTcpServer( parent )
{
_mysockets.empty();
}
ChatServer::~ChatServer()
{
qDeleteAll( _mysockets );
_mysockets.clear();
}
void ChatServer::Run( quint16 port )
{
if( !this->listen(QHostAddress::Any, port) )
printf( "ChatServer listen failed !" );
}
void ChatServer::sendData(QString string){
printf( "client data to all client!\n" );
QList<MySocket*>::iterator iter;
for( iter = _mysockets.begin(); iter != _mysockets.end(); iter++ ) {
if((*iter)->isOpen())
{
(*iter)->write(qPrintable(string),string.length());
}
}
}
void ChatServer::sendData(QByteArray string){
printf( "client data to all client!\n" );
QList<MySocket*>::iterator iter;
for( iter = _mysockets.begin(); iter != _mysockets.end(); iter++ ) {
if((*iter)->isOpen())
{
(*iter)->write(string,string.length());
}
}
}
/*void ChatServer::incomingConnection( int handle )*///修改之前
void ChatServer::incomingConnection( qintptr handle )//修改之后
{
printf( "incomingConnection(): %d !\n", handle );
MySocket *mysocket = new MySocket( this );
mysocket->setServer(this);
mysocket->setSocketDescriptor( handle );
connect( mysocket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()) );
_mysockets.append( mysocket );
}
void ChatServer::clientDisconnected()
{
printf( "client disconnected !\n" );
QList<MySocket*>::iterator iter;
for( iter = _mysockets.begin(); iter != _mysockets.end(); iter++ ) {
if( -1 == (*iter)->socketDescriptor() ) {
_mysockets.erase( iter );
return;
}
}
}