#include <QMessageBox>
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
clientConnection = NULL;
getLocalIP ();
connect (ui->clientSendpushButton, SIGNAL(clicked()), this, SLOT(clientSendMessage()));
connect (ui->serverSendpushButton, SIGNAL(clicked()), this, SLOT(serverSendMessage()));
ui->clientSendpushButton->setEnabled (false);
ui->serverSendpushButton->setEnabled (false);
ui->disconnectpushButton->setEnabled (false);
ui->stoppushButton->setEnabled (false);
}
void Widget::getLocalIP ()
{
IPlist = QNetworkInterface::allAddresses ();
foreach(QHostAddress IP, IPlist)
{
ui->serverIPcomboBox->addItem (IP.toString ());
}
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_listenpushButton_clicked() //开始监听
{
serverPort = ui->serverPortlineEdit->text ();
serverIP = ui->serverIPcomboBox->currentText ();
if(serverPort.isEmpty ())
{
QMessageBox::warning (this, tr("Warnning"), tr("端口号不能为空"));
return;
}
tcpServer = new QTcpServer(this);
if(!tcpServer->listen (QHostAddress::Any, serverPort.toInt ()))
{
QMessageBox::warning (this, tr("Warnning"), tcpServer->errorString ());
close ();
}
// connect (tcpServer, SIGNAL(newConnection()), this, SLOT(updateStatus()));
connect (tcpServer, SIGNAL(newConnection()), this, SLOT(createServerSocket())); //有新的连接到来,则开始创建套接字
ui->statuslabel->setText (tr("开始监听"));
ui->listenpushButton->setEnabled (false);
ui->stoppushButton->setEnabled (true);
// QTimer *time = new QTimer(this); //更新状态标签
// connect (time, SIGNAL(timeout()), this, SLOT(updateStatus()));
// time->start ();
}
void Widget::on_connectpushButton_clicked() //开始连接
{
tcpClient = new QTcpSocket(this);
connect (tcpClient, SIGNAL(readyRead()), this, SLOT(readMessage()));
connect (tcpClient, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
connect (tcpClient, SIGNAL(connected()), this, SLOT(updateClientStatusConnect())); //更新状态
connect (tcpClient, SIGNAL(disconnected()), this, SLOT(updateClientStatusDisconnect()));
blockSize = 0;
tcpClient->abort ();
serverIP = ui->clientIPlineEdit->text ();
clientPort = ui->clientPortlineEdit->text ();
if(serverIP.isEmpty () || clientPort.isEmpty ())
{
QMessageBox::warning (this, tr("Warnning"), tr("服务器IP或端口号不能为空"));
return;
}
tcpClient->connectToHost (serverIP, clientPort.toInt ()); //连接到主机
}
void Widget::on_cCleanpushButton_clicked() //清空显示区
{
ui->messagetextBrowser->setText ("");
}
void Widget::on_sCleanpushButton_clicked()
{
ui->servertextBrowser->setText ("");
}
void Widget::on_disconnectpushButton_clicked()
{
ui->clientMessagelineEdit->setText (tr("clientStop"));
clientSendMessage ();
ui->clientMessagelineEdit->setText (tr(""));
tcpClient->close ();
}
void Widget::on_stoppushButton_clicked()
{
ui->serverMessagelineEdit->setText (tr("serverStop")); //发送服务端停止监听信息
serverSendMessage ();
ui->serverMessagelineEdit->setText (tr(""));
tcpServer->close ();
ui->statuslabel->setText (tr("停止监听"));
ui->listenpushButton->setEnabled (true);
ui->stoppushButton->setEnabled (false);
ui->serverSendpushButton->setEnabled (false);
}
void Widget::createServerSocket ()
{
ui->statuslabel->setText(tr("客户端已连接"));
ui->serverSendpushButton->setEnabled (true);
clientConnection = tcpServer->nextPendingConnection ();
connect (clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));
connect (clientConnection, SIGNAL(readyRead()), this, SLOT(serverReadMessage()));
}
void Widget::readMessage ()
{
QDataStream in(tcpClient);
in.setVersion (QDataStream::Qt_4_7);
if(blockSize == 0)
{
// 判断接收的数据是否有两字节,也就是文件的大小信息
// 如果有则保存到 blockSize 变量中,没有则返回,继续接收数据
if(tcpClient->bytesAvailable () < (int)sizeof(quint16))
return ;
in >> blockSize;
}
if(tcpClient->bytesAvailable () < blockSize)// 如果没有得到全部的数据,则返回,继续接收数据
return;
in >> message;
if(message.contains ("serverStop")) //如果收到是服务器停止监听的信息
{
tcpClient->close ();
ui->cStatuslabel->setText (tr("服务器断开连接"));
blockSize = 0;
return;
}
ui->messagetextBrowser->insertPlainText (tr("reveived message: %1 \n").arg (message));
blockSize = 0;
}
void Widget::serverReadMessage ()
{
QDataStream in(clientConnection);
in.setVersion (QDataStream::Qt_4_7);
if(blockSize == 0)
{
// 判断接收的数据是否有两字节,也就是文件的大小信息
// 如果有则保存到 blockSize 变量中,没有则返回,继续接收数据
if(clientConnection->bytesAvailable () < (int)sizeof(quint16))
return ;
in >> blockSize;
}
if(clientConnection->bytesAvailable () < blockSize)// 如果没有得到全部的数据,则返回,继续接收数据
return;
in >> message;
if(message.contains ("clientStop")) //如果收到是客户端断开连接的信息
{
clientConnection->close ();
ui->serverSendpushButton->setEnabled (false);
ui->statuslabel->setText (tr("客户端断开连接"));
blockSize = 0;
return;
}
ui->servertextBrowser->insertPlainText (tr("reveived message: %1 \n").arg (message));
blockSize = 0;
}
void Widget::serverSendMessage ()
{
if(!clientConnection) //判断有没有实例化
if(!(clientConnection = tcpServer->nextPendingConnection ())) //没有客户端连接
{
return;
}
serverMessage = ui->serverMessagelineEdit->text ();
if(serverMessage.isEmpty ())
{
QMessageBox::warning (this, tr("Warnning"), tr("请输入发送数据"));
return;
}
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion (QDataStream::Qt_4_7);
out << (quint16) 0;
out << serverMessage;
out.device ()->seek (0);
out << (quint16) (block.size () - sizeof(quint16));
connect(clientConnection, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayErrorS(QAbstractSocket::SocketError)));
clientConnection->write (block);
if(serverMessage.contains ("serverStop"))
return;
ui->servertextBrowser->insertPlainText (tr("send message: %1 \n").arg (serverMessage));
}
void Widget::clientSendMessage ()
{
clientMessage = ui->clientMessagelineEdit->text ();
if(clientMessage.isEmpty ())
{
QMessageBox::warning (this, tr("Warnning"), tr("请输入发送数据"));
return;
}
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion (QDataStream::Qt_4_7);
out << (quint16) 0;
out << clientMessage;
out.device ()->seek (0);
out << (quint16)(block.size () - sizeof(quint16));
tcpClient->write (block);
if(clientMessage.contains ("clientStop"))
return;
ui->messagetextBrowser->insertPlainText (tr("send message: %1 \n").arg (clientMessage));
}
void Widget::displayError (QAbstractSocket::SocketError)
{
QMessageBox::warning (this, tr("Warnning"), tcpClient->errorString ());
tcpClient->close ();
ui->connectpushButton->setEnabled (true);
ui->disconnectpushButton->setEnabled (false);
ui->clientSendpushButton->setEnabled (false);
}
void Widget::displayErrorS (QAbstractSocket::SocketError) //服务器端错误提示
{
// QMessageBox::warning (this, tr("Warnning"), clientCon