16,216
社区成员
发帖
与我相关
我的任务
分享
//widget.cpp服务端
#include "widget.h"
#include "ui_widget.h"
#include <QtGui>
#include <stdio.h>
#include <QSqlDatabase>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("Server");
this->server = new QTcpServer(this);
this->tcpsocket = new QTcpSocket(this);
//sql_con();//SQL databas init.SQL连接配置
port=new QString();
port->clear();
ip="172.16.113.22";//121.8.242.229-服务器IP
port->append("6666");//要监听的端口
i=1;
if(!server->listen(QHostAddress(ip),(quint16)port->toInt(0,10))){
//printf("please input your PC IP\n");//to apply the IP that you will listen's IP
//QString cip;
//scanf("&s:",&cip);
//if(!server->listen(QHostAddress(cip),(quint16)port->toInt(0,10))){
QMessageBox box;//修改为qdebug-----S
box.setText("listen error");//-----
box.exec();//修改为qdebug-----E
close();
// }
}else{
connect(this->server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
connect(this->ui->replyPushButton, SIGNAL(clicked()), this, SLOT(clickReplyButton()));
connect(this->server, SIGNAL(newConnection()), this, SLOT(autosend()));//autosend.自动发送内容
}
}
Widget::~Widget()
{
delete ui;
}
void Widget::sql_con()//SQL database init.
{
QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");
db.setUserName("root");
db.setPassword("weiqun");
db.setDatabaseName("zigbee");
}
void Widget::clickReplyButton()//The button to send message.发送消息按钮
{
//this->tcpsocket = this->server->nextPendingConnection();
connect(this->server, SIGNAL(newConnection()), this, SLOT(sendToClient()));
}
void Widget::acceptConnection()//when connect success to do.连接成功后所作操作
{
this->tcpsocket = this->server->nextPendingConnection();
connect(this->tcpsocket, SIGNAL(readyRead()), this, SLOT(readFormClient()));
}
void Widget::readFormClient()//receive message.接收消息
{
QString str;
str = this->tcpsocket->readAll();
this->ui->fromTextEdit->append(str);
}
void Widget::sendToClient()//send message.发送消息
{
QString sss="Sending message to client...";
sss.append(this->ui->toTextEdit->toPlainText());
if(sss != "")
{
string str2 = string(sss.toAscii().data());
const char *str3 = str2.c_str();
this->tcpsocket->write(str3);
}
this->ui->toTextEdit->clear();
}
void Widget::autosend(){//自动发送信息至客户端
QString si=QString::number(i);
QString sss= "Sending message to client:number.";//要发送的内容
sss.append(si);
sss.append("--:");
string str2 = string(sss.toAscii().data());
const char *str3 = str2.c_str();
this->tcpsocket->write(str3);
i++;
}
//服务端头文件widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QtNetwork>
#include <QString>
using namespace std;
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
QTcpServer *server;
QTcpSocket *tcpsocket;
QString readFormClientMessage;
quint16 blockSize;
QString ip;
QString *port;
int i;
private slots:
void clickReplyButton();
void readFormClient();
void sendToClient();
void acceptConnection();
void autosend();
void sql_con();
};
#endif // WIDGET_H
//客户端widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowTitle("Client");
this->client = new QTcpSocket(this);
connect(this->ui->requestPushButton, SIGNAL(clicked()), this, SLOT(clickRequestButton()));
connect(this->client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
connect(this->client, SIGNAL(readyRead()), this, SLOT(readFormServer()));
}
Widget::~Widget()
{
delete ui;
}
void Widget::clickRequestButton()
{
sendToServer();
if(!ui->errorLabel->text().isEmpty())
ui->errorLabel->setText("");
}
void Widget::sendToServer()
{
this->client->abort();
QString ip ="121.8.242.229";// "172.16.113.22";
int port = 6666;
this->client->connectToHost(ip, port);
QString str = this->ui->toTextEdit->toPlainText();
if(str != "")
{
string str2 = string(str.toAscii().data());
const char *str3 = str2.c_str();
this->client->write(str3);
}
}
void Widget::readFormServer()
{
//this->ui->fromTextEdit->clear();
QString str5;
str5 = this->client->readAll();
this->ui->fromTextEdit->append(str5);
}
void Widget::displayError(QAbstractSocket::SocketError)
{
this->ui->errorLabel->setText(this->client->errorString());
}
//客户端头文件widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QtNetwork>
#include <QString>
using namespace std;
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
QTcpSocket *client;
QString readFormServerMessage;
quint16 blockSize;
private slots:
void clickRequestButton();
void sendToServer();
void readFormServer();
void displayError(QAbstractSocket::SocketError);
};
#endif // WIDGET_H