資源簡介
Qt項目實例,大量實戰(zhàn)項目,供你參考,不要錯過。
代碼片段和文件信息
/*
?*?ChessServer.cpp
?*
?*??Created?on:?2010-4-19
?*??????Author:?Administrator
?*/
#include?“ChessServer.h“
#include?“chess_zn.h“
#include?
#include?
Chess_ZN?*?chess;
ChessServer::ChessServer(QWidget?*parent):?QDialog(parent)
{
pendCount?=?iCount=?0;
id?=?1;
timer?=?new?QTimer;
peer?=?new?PeerManager(1);
peer->setServerPort(10000);???????////監(jiān)聽已開啟等待客戶端試探性連接
socudp?=?peer->returnUdp();
chess?=?qobject_cast(parent);
vblay?=?new?QVBoxLayout;
????for?(int?i?=?0;?i?3;?i++)?{
?formlay[i]?=?new?QFormLayout;
}
????line[0]?=?new?QLineEdit(“9999“);
????line[1]?=?new?QLineEdit(“32“);
????label[0]=?new?QLabel(Chess_ZN::utf(“游戲端口“));
????label[1]=?new?QLabel(Chess_ZN::utf(“連接上限“));
????button[0]=new?QPushButton(Chess_ZN::utf(“開啟“));
????button[1]=new?QPushButton(Chess_ZN::utf(“取消“));
????for(int?i?=?0;?i?2;?++i)?{
???? formlay[i]->addRow(label[i]line[i]);
????}
????formlay[2]->addRow(button[0]button[1]);
????for(int?i?=?0;?i?3;?++i)?{
???? vblay->addLayout(formlay[i]);
????}
????connect(button[0]SIGNAL(clicked())thisSLOT(createServer()));
????connect(button[1]SIGNAL(clicked())thisSLOT(close()));
????connect(socudp?SIGNAL(readyRead())?this?SLOT(readUdpData()));
????connect(timer?SIGNAL(timeout())?this?SLOT(flushINFO()));
????timer->start(2000);
??? this->setFixedSize(200140);
????this->setLayout(vblay);
????isDatabase?=?true;
connection=new?Connection;
????if(!connection->createConnection()){
??????//exit(1);
??????isDatabase?=?false;
????}
????if(isDatabase){
??????connection->setQsqlquery();//創(chuàng)表
????}
}
void?ChessServer::createServer(){
bool?okisok;
line[0]->setValidator(new?QIntValidator(065535this));
int?port?=?line[0]->text().toInt(&ok?10);
int?max?=?line[1]->text().toInt(&isok?10);
if(ok==true?&&?isok==true?&&?max?>1){
??pendCount?=?max;
??tcpServer?=?new?QTcpServer(this);
??tcpServer->setMaxPendingConnections(max);
??if?(!tcpServer->listen(QHostAddress::Anyport))?{
???????QMessageBox::critical(this?Chess_ZN::utf(“服務器信息“)Chess_ZN::utf(tr(“不能開啟服務器:?%1.“).arg(tcpServer->errorString())));
???????close();
???????return;
??}
??connect(tcpServerSIGNAL(newConnection())thisSLOT(newConnection()));
??this->setVisible(false);
??chess->action2[0]->setText(Chess_ZN::utf(“取消服務器“));
}else{
?line[0]->setText(“9999“);
?line[1]->setText(“32“);
?QMessageBox::warning(thisChess_ZN::utf(“對不起“)Chess_ZN::utf(“請您正常填寫“)QMessageBox::Ok);
?return;
}
}
void?ChessServer::newConnection(){
if(iCount? ???tcpclient?=?tcpServer->nextPendingConnection();
???????map.insert(tcpclient->peerAddress().toString()tcpclient);
???????connect(tcpclientSIGNAL(readyRead())thisSLOT(readData()));
???????connect(tcpclientSIGNAL(error(QAbstractSocket::SocketError))thisSLOT(error(QAbstractSocket::SocketError)));
????}
}
void?ChessServer::readData(){
QTcpSocket?*?client=qobject_cast
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????.......??????4165??2010-05-12?18:07??應用項目\Chess\.cproject
????.......??????2899??2010-05-12?18:07??應用項目\Chess\.project
????.......???????972??2010-05-12?18:07??應用項目\Chess\Chess.pro
????.......???????688??2010-05-12?18:07??應用項目\Chess\chess.qrc
????.......?????30051??2010-05-12?18:07??應用項目\Chess\chess_zn.cpp
????.......??????2242??2010-05-12?18:07??應用項目\Chess\chess_zn.h
????.......???????437??2010-05-12?18:07??應用項目\Chess\chess_zn.ui
????.......??????8331??2010-05-12?18:07??應用項目\Chess\ChessServer.cpp
????.......??????1491??2010-05-12?18:07??應用項目\Chess\ChessServer.h
????.......??????8554??2010-05-12?18:07??應用項目\Chess\ChessThread.cpp
????.......???????740??2010-05-12?18:07??應用項目\Chess\ChessThread.h
????.......??????4448??2010-05-12?18:07??應用項目\Chess\commands.cpp
????.......??????1474??2010-05-12?18:07??應用項目\Chess\commands.h
????.......???????922??2010-05-12?18:07??應用項目\Chess\databa
????.......???????302??2010-05-12?18:07??應用項目\Chess\databa
????.......???????425??2010-05-12?18:07??應用項目\Chess\databa
????.......???????513??2010-05-12?18:07??應用項目\Chess\databa
????.......???????790??2010-05-12?18:07??應用項目\Chess\databa
????.......???????296??2010-05-12?18:07??應用項目\Chess\databa
????.......???1636636??2010-05-12?18:07??應用項目\Chess\debug\chess_zn.o
????.......???1556757??2010-05-12?18:07??應用項目\Chess\debug\ChessServer.o
????.......???1480975??2010-05-12?18:07??應用項目\Chess\debug\ChessThread.o
????.......???1346039??2010-05-12?18:07??應用項目\Chess\debug\commands.o
????.......????934029??2010-05-12?18:07??應用項目\Chess\debug\connection.o
????.......????538262??2010-05-12?18:07??應用項目\Chess\debug\diagramitem.o
????.......???1477559??2010-05-12?18:07??應用項目\Chess\debug\diagramscene.o
????.......???1435027??2010-05-12?18:07??應用項目\Chess\debug\main.o
????.......?????15964??2010-05-12?18:07??應用項目\Chess\debug\mingwm10.dll
????.......??????3723??2010-05-12?18:07??應用項目\Chess\debug\moc_chess_zn.cpp
????.......???1448772??2010-05-12?18:07??應用項目\Chess\debug\moc_chess_zn.o
............此處省略487個文件信息
評論
共有 條評論