Qt关于TCP通讯问题.
客户端代码:
#include <QTcpSocket>
#include <QtGui/QMainWindow>
#include "ui_network.h"
#define SPORT 5059
class network : public QMainWindow
{
Q_OBJECT
public:
network(QWidget *parent = 0, Qt::WFlags flags = 0);
~network();
void DoConNect();
private:
Ui::networkClass ui;
int m_port,m_ifcon;
QString m_ipadd;
QString m_respond;
QString m_sdcomd;
QString m_data;
QTcpSocket m_tcpSocket;
public slots:
void SendMessage();
void ConMessage();
void DisplayMessage();
void ConStatus();
void error();
};
#endif // NETWORK_H
//////////////////////////////////////////////////
#include "network.h"
#include <QMessageBox>
#include <QHostAddress>
network::network(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
/*初始化变量*/
m_ifcon = 0;
connect(ui.Bn_Connect,SIGNAL(clicked()),this,SLOT(ConMessage()));
connect(ui.Bn_Port1,SIGNAL(clicked()),this,SLOT(SendMessage()));
connect(ui.Bn_Port2,SIGNAL(clicked()),this,SLOT(SendMessage()));
connect(ui.Bn_Port3,SIGNAL(clicked()),this,SLOT(SendMessage()));
/*网络*/
connect(&m_tcpSocket,SIGNAL(connected()),this,SLOT(ConStatus()));
connect(&m_tcpSocket,SIGNAL(readyRead()),this,SLOT(DisplayMessage()));
connect(&m_tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(error()));
}
void network::ConMessage()
{
m_tcpSocket.connectToHost(QHostAddress::LocalHost,SPORT);
}
void network::ConStatus()
{
m_ifcon = 1;
QMessageBox::about(NULL,"Notice","Connect Succesfull!");
ui.Bn_Connect->setEnabled(false);
return;
}
void network::error()
{
ui.lbstatus->setText(m_tcpSocket.errorString());
}
void network::SendMessage()
{
m_data = ui.tx_SendParam->toPlainText();
m_tcpSocket.write(qPrintable(m_data));
}
void network::DisplayMessage()
{
QByteArray byte;
byte = m_tcpSocket.readAll();
QString str(byte);
ui.tx_Respond->clear();
ui.tx_Respond->setText(str);
}
network::~network()
{
}
/*服务端代码*/
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QtGui/QMainWindow>
#include "ui_tcpserver.h"
#include <QTcpServer>
#include <QList>
#include <QTcpSocket>
#define PORT 5059
#define TCPSOCKET_MAX 3
class tcpserver : public QMainWindow
{
Q_OBJECT
public:
tcpserver(QWidget *parent = 0, Qt::WFlags flags = 0);
~tcpserver();
private:
Ui::tcpserverClass ui;
private:
QTcpServer *m_tcpServer;
QList <QTcpSocket *> m_tcpSocketList;
public:
void recv_slot();
void connect_slot();
void Bn_DoCon();
};
#endif // TCPSERVER_H
///////////////////////////
#include "tcpserver.h"
#include <QMessageBox>
tcpserver::tcpserver(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
connect(ui.Bn_Con,SIGNAL(clicked()),this,SLOT(Bn_DoCon()));
}
void tcpserver::Bn_DoCon()
{
this->m_tcpServer = new QTcpServer(this);
if(m_tcpServer->listen(QHostAddress::Any,PORT))
{
ui.Bn_Con->setEnabled(false);
QMessageBox::about(NULL,"Notice","CONNECT SUCCESEFULL!");
}
connect(m_tcpServer,SIGNAL(newConnection()),this,SLOT(connect_slot()));
}
void tcpserver::connect_slot()
{
if(m_tcpSocketList.count() <= TCPSOCKET_MAX)
{
QTcpSocket *tcpSocket = m_tcpServer->nextPendingConnection();
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(recv_slot()));
m_tcpSocketList.append(tcpSocket);
}
}
void tcpserver::recv_slot()
{
QString data("OK");
for(int i=0 ; i<m_tcpSocketList.count() ; i++)
{
QByteArray byte;
QTcpSocket *tcpSocket=m_tcpSocketList.at(i);
byte = tcpSocket->readAll();
QString result(byte);
if(result.isEmpty())
return;
ui.tx_recv->setText(result);
tcpSocket->write(qPrintable(data));
}
}
tcpserver::~tcpserver()
{
}
代码如上,不知道什么原因,我的客户端总是获取到错误信号void network::error()
提示 connect refused
这是什么原因?