本人
新手,在查找了n多
资料后修改了qt自带的例程,写了一个
qtcpsocket 多线程的历程。
一是与和我一样的菜鸟共享,二是请高手指点一下,是否存在
问题。
main.cpp
#include<QApplication>[pre]
#include<QtCore>[/pre][pre]
#include"fortuneserver.h"[/pre][pre][/pre][pre]
#include<stdlib.h>[/pre][pre]
#include<QDebug>[/pre][pre]
#include<QtDebug>[/pre][pre][/pre][pre]
intmain
(intargc,char*argv[])[/pre][pre]
{[/pre][pre]
QApplicationapp(argc,argv);[/pre][pre][/pre][pre]
FortuneServerserver;[/pre][pre][/pre][pre]
if(!server.listen
(QHostAddress::Any,888))//开始监听端口[/pre][pre]
{[/pre][pre]
//如果无法打开监听地址,报错[/pre][pre]
qDebug
()<<"can'topensocket";[/pre][pre]
returnFALSE
;[/pre][pre]
}[/pre][pre][/pre][pre]
qDebug
()<<"starttolistensocketinport888";[/pre][pre][/pre][pre]
//qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));[/pre][pre]
returnapp.exec();[/pre][pre]
}[/pre][pre]
[/pre][pre]fortuneserver.h[/pre][pre]
#ifndefFORTUNESERVER_H[pre]
#defineFORTUNESERVER_H[/pre][pre][/pre][pre]
#include<QStringList>[/pre][pre]
#include<QTcpServer>[/pre][pre][/pre][pre]
#include<QDebug>[/pre][pre]
#include<QtDebug>[/pre][pre][/pre][pre]
//![0][/pre][pre]
classFortuneServer:publicQTcpServer[/pre][pre]
{[/pre][pre]
Q_OBJECT[/pre][pre][/pre][pre]
public:[/pre][pre]
FortuneServer();[/pre][pre][/pre][pre]
protected:[/pre][pre]
voidincomingConnection(intsocketDescriptor
);[/pre][pre][/pre][pre]
private:[/pre][pre]
QStringListfortunes;[/pre][pre][/pre][pre]
};[/pre][pre]fortuneserver.cpp[/pre][pre]
#include"fortuneserver.h"[pre]
#include"fortunethread.h"[/pre][pre][/pre][pre]
#include<stdlib.h>[/pre][pre][/pre][pre]
//![0][/pre][pre]
FortuneServer::FortuneServer()[/pre][pre]
{[/pre][pre][/pre][pre]
}[/pre][pre]
//![0][/pre][pre][/pre][pre]
//![1][/pre][pre]
voidFortuneServer::incomingConnection(intsocketDescriptor)[/pre][pre]
{[/pre][pre]
FortuneThread*thread=newFortuneThread(socketDescriptor,this);[/pre][pre]
connect
(thread,SIGNAL(finished
()),thread,SLOT(deleteLater
()));[/pre][pre]
qDebug
()<<"createanewthreadforanewsocket";[/pre][pre]
thread->start
();[/pre][pre]
}[/pre][pre]fortunethread.h[/pre][pre]
[/pre][pre]
#ifndefFORTUNETHREAD_H[pre]
#defineFORTUNETHREAD_H[/pre][pre][/pre][pre]
#include<QThread>[/pre][pre]
#include<QTcpSocket>[/pre][pre][/pre][pre]
#include<QDebug>[/pre][pre]
#include<QtDebug>[/pre][pre][/pre][pre]
//![0][/pre][pre]
classFortuneThread:publicQThread[/pre][pre]
{[/pre][pre]
Q_OBJECT[/pre][pre][/pre][pre]
public:[/pre][pre]
FortuneThread(intsocketDescriptor,QObject*parent
);[/pre][pre]
//~FortuneThread();[/pre][pre][/pre][pre]
voidrun();[/pre][pre][/pre][pre]
signals:[/pre][pre]
voiderror
(QTcpSocket::SocketErrorsocketError
);[/pre][pre]
publicslots:[/pre][pre]
voidreaddata
();[/pre][pre]
voidsocket_disconnect
();[/pre][pre][/pre][pre]
private:[/pre][pre]
intsocketDescriptor;[/pre][pre]
QStringtext;[/pre][pre]
QTcpSocket*tcpSocket;[/pre][pre]
};[/pre][pre]
[/pre][pre][pre]fortunethread.cpp
threadedfortuneserver.rar (756 K) 下载次数:87 [/pre][/pre][pre]
[/pre][pre]
#include"fortunethread.h"[pre][/pre][pre]#include<QtNetwork>[/pre][pre][/pre][pre]//![0][/pre][pre]FortuneThread::FortuneThread(intsocketDescriptor,QObject*parent)[/pre][pre]:QThread(parent),socketDescriptor(socketDescriptor)[/pre][pre]{[/pre][pre]//this->socketDescriptor=socketDescriptor;[/pre][pre]}[/pre][pre]//![0][/pre][pre][/pre][pre]//![1][/pre][pre]voidFortuneThread::run()[/pre][pre]{[/pre][pre]tcpSocket=newQTcpSocket();[/pre][pre][/pre][pre]connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(readdata()),Qt::DirectConnection);[/pre][pre]connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(socket_disconnect()));[/pre][pre][/pre][pre]if(!tcpSocket->setSocketDescriptor(socketDescriptor))[/pre][pre]{[/pre][pre]emiterror(tcpSocket->error());[/pre][pre]return;[/pre][pre]}[/pre][pre][/pre][pre]qDebug()<<"waitfordata";[/pre][pre]this->exec();[/pre][pre]}[/pre][pre]//![4][/pre][pre][/pre][pre]voidFortuneThread::readdata()[/pre][pre]{[/pre][pre]qDebug()<<"youcangetmessageandsendmessagehere";[/pre][pre]}[/pre][pre][/pre][pre]voidFortuneThread::socket_disconnect()[/pre][pre]{[/pre][pre]qDebug()<<"socketdisconnectformclient";[/pre][pre]this->exit();[/pre][pre]this->quit();[/pre][pre]this->wait(100);[/pre][pre]emit(FortuneThread::finished());[/pre][pre]}[/pre][/pre][/pre][pre]
[/pre][/pre][pre]
threadedfortuneserver.rar (756 K) 下载次数:87 [/pre][pre]
[/pre][/pre][pre][/pre]
[ 此帖被ucgui在2011-05-22 23:04重新编辑 ]