server端只接收数据,client端发送数据
server端代码:
server::server(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::server)
{
ui->setupUi(this);
QMainWindow(parent),
ui(new Ui::server)
{
ui->setupUi(this);
this->setWindowTitle("QQ2010");
//绑定套接字
tcpServer = new QTcpServer;
tcpServer->listen(QHostAddress("219.229.140.203"), 8888);
QObject::connect(tcpServer, SIGNAL(newConnection()), this,
SLOT(processConnection()));
QObject::connect(ui->sendaction, SIGNAL(triggered()), this, SLOT(sendFile()));
QObject::connect(ui->recvaction, SIGNAL(triggered()), this, SLOT(recvFile()));
}
tcpServer->listen(QHostAddress("219.229.140.203"), 8888);
QObject::connect(tcpServer, SIGNAL(newConnection()), this,
SLOT(processConnection()));
QObject::connect(ui->sendaction, SIGNAL(triggered()), this, SLOT(sendFile()));
QObject::connect(ui->recvaction, SIGNAL(triggered()), this, SLOT(recvFile()));
}
server::~server()
{
delete ui;
}
{
delete ui;
}
void server::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
//处理连接信号
void server::processConnection()
{
tcpSocket = new QTcpSocket;
tcpSocket = tcpServer->nextPendingConnection();
QObject::connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(processReadData()));
}
{
tcpSocket = new QTcpSocket;
tcpSocket = tcpServer->nextPendingConnection();
QObject::connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(processReadData()));
}
//处理socket数据可读信号
void server::processReadData()
{
time = QDateTime(QDateTime::currentDateTime());
QString timeString = time.toString("hh:mm:ss");
ui->messagetextEdit->append(timeString);
ui->messagetextEdit->append(QString(tcpSocket->readAll()));
}
{
time = QDateTime(QDateTime::currentDateTime());
QString timeString = time.toString("hh:mm:ss");
ui->messagetextEdit->append(timeString);
ui->messagetextEdit->append(QString(tcpSocket->readAll()));
}