#ifndef TCPSERVER_H
#define TCPSERVER_H
//connection类
#include <QObject>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
#include <QList>
#include <QMutex>
class Connection:public QObject
{
Q_OBJECT
public:
int m_nSocketID;//-1 indicate the socket is invalid; else is the global id of the connection
int m_nMsgReceived;
int m_nMsgSent;
QTcpSocket* m_pTcpSocket;
public:
Connection()
{
m_nSocketID = -1;
}
~Connection()
{
if (m_pTcpSocket!=NULL)
{
delete m_pTcpSocket;
}
}
public:
void InitializeConnection(QTcpSocket* socket, int id)
{
m_pTcpSocket = socket;
m_pTcpSocket->setParent(this);
m_nMsgReceived = 0;
m_nMsgSent = 0;
m_nSocketID = id;
}
void DeleteConnection()
{
m_nSocketID = -1;
m_pTcpSocket->disconnect();
// delete m_pTcpSocket; 若delete会报错, ?
m_pTcpSocket = NULL;
}
};
#endif // TCPSERVER_H
#include "widget.h"
#include "ui_widget.h"
#include "tcpserver.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(showTime()));
timer->start(500);
//tcp server
m_pConnection = new Connection[TCP_MAX_CLIENTS];
m_nConnected = 0;
m_nID = 0;
if (!InitializeServers())
{
exit(-1);
}
}
Widget::~Widget()
{
delete timer;
delete m_pConnection;
delete m_pTcpServer;
delete ui;
}
void Widget::on_ledButton_clicked()
{
//ui->ledLabel->setText("wen----hello");
}
void Widget::showTime()
{
QTime time = QTime::currentTime();
QString txtTime = time.toString("hh:mm:ss");
ui->lcdNumber->display(txtTime);
}
bool Widget::InitializeServers()
{
m_pTcpServer = new QTcpServer();
if (!m_pTcpServer->listen(QHostAddress("192.168.1.226"), 60000))
{
//ShowMessageBox("server listen to 60000 error, exiting...");
return (false);
}
else
{
connect(m_pTcpServer, SIGNAL(newConnection()), this, SLOT(OnNewConnection()));
}
return true;
}
void Widget::OnNewConnection()
{
//check how many connections established
ShowStatusMessage("new connection...");
//find one connection to store the new connection
int i=0;
for (i=0; i<TCP_MAX_CLIENTS; i++)
{
if (m_pConnection[i].m_nSocketID == -1)
{
m_nID = i;
break;
}
}
char dispBuf[20];
sprintf(dispBuf,"m_nID=%d",m_nID);
ui->statusBar->setText(dispBuf);
//add new connection
m_mutexConnection.lock();
(m_pConnection[i]).InitializeConnection(m_pTcpServer->nextPendingConnection(), m_nID);
connect(m_pConnection[i].m_pTcpSocket, SIGNAL(readyRead()), this, SLOT(OnReadyRead()));
connect(m_pConnection[i].m_pTcpSocket, SIGNAL(disconnected()), this, SLOT(OnDisconnected()));
m_mutexConnection.unlock();
m_nConnected++;
//if the connection is 10, close the server
if (m_nConnected==TCP_MAX_CLIENTS)
{
m_pTcpServer->close();
ShowStatusMessage("Maximum connection (10) reached, new connection will be refused......");
}
}
void Widget::ShowStatusMessage(const QString str)
{
ui->statusBar->clear();
ui->statusBar->setText(str);
// this->ui.statusBar->clearMessage();
// this->ui.statusBar->showMessage(str);
}
void Widget::OnDisconnected()
{
//get the connection that disconnected from the server
QTcpSocket* socket = qobject_cast<QTcpSocket*>(sender());
Connection* connection = qobject_cast<Connection*>(socket->parent());
//delete the connection
m_mutexConnection.lock();
connection->DeleteConnection();
m_mutexConnection.unlock();
ShowStatusMessage("server disconnected");
m_nConnected--;
//if the connection is reduced from 10 to 9, reopen the server, listen
if (m_nConnected==TCP_MAX_CLIENTS-1)
{
// m_pTcpServer->listen(QHostAddress("192.168.1.226"), 60000);
}
}
void Widget::OnReadyRead()
{
// ShowStatusMessage("readbytes");
int i;
QString disP,str;
for(i = 0; i < TCP_MAX_CLIENTS; i++)
{
if (m_pConnection[i].m_nSocketID == -1)
continue;
else
{
str = m_pConnection[i].m_pTcpSocket->readAll();
if(str!=NULL)
{
disP = m_pConnection[i].m_pTcpSocket->peerAddress().toString();
disP += str;
//sprintf(dispBuf,"%s:%s",m_pConnection[i].m_pTcpSocket->peerAddress().toIPv4Address(),str);
ShowStatusMessage(disP);
}
}
}
}
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTimer>
#include <QTime>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
#include "tcpserver.h"
#define TCP_MAX_CLIENTS 10
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_ledButton_clicked();
void showTime();
void OnNewConnection();
void OnDisconnected();
void OnReadyRead();
private:
Ui::Widget *ui;
QTimer *timer;
QTcpServer *m_pTcpServer;
Connection *m_pConnection;
QMutex m_mutexConnection;//mutex to protect the array
int m_nConnected;
int m_nID;
public:
bool InitializeServers();
void ShowStatusMessage(const QString str);
};
#endif // WIDGET_H