用QTcpSocket做客户端用来测试TCP,但不能接收数据,求指教!
#include <QMessageBox>
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setFixedSize( this->width (),this->height ()); //固定窗口大小
TcpSocket = new QTcpSocket(this);
connect(ui->ConnectBtn,SIGNAL(clicked()),this,SLOT(Connect()));
connect(ui->StopBtn,SIGNAL(clicked()),this,SLOT(Stop()));
connect(ui->ClearReceiveBtn,SIGNAL(clicked()),this,SLOT(ClearReceive()));
connect(ui->ClearSendBtn,SIGNAL(clicked()),this,SLOT(ClearSend()));
connect(ui->SendBtn,SIGNAL(clicked()),this,SLOT(Send()));
connect(TcpSocket,SIGNAL(readyRead()),this,SLOT(ReadMessage()));
connect(TcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),
this,SLOT(displayError(QAbstractSocket::SocketError)));
}
Widget::~Widget()
{
delete ui;
}
void Widget::displayError(QAbstractSocket::SocketError)
{
qDebug() << TcpSocket->errorString(); //输出错误信息
}
void Widget::ReadMessage()
{
qDebug("Read");
if(TcpSocket->waitForConnected(30000)){
QString Msg;
QByteArray qba = TcpSocket->readAll();
Msg=QVariant(qba).toString();
ui->Receive_textBrowser->append(Msg);}
}
void Widget::Connect()
{
// BlockSize = 0; //初始化其为0
TcpSocket->abort(); //取消已有的连接
TcpSocket->connectToHost(ui->Remote_IP_lineEdit->text(),
ui->Remote_Port_lineEdit->text().toInt());
ui->ConnectBtn->setEnabled(false);
ui->StopBtn->setEnabled(true);
}
void Widget::Stop()
{
TcpSocket->close();
qDebug("socket break");
ui->StopBtn->setEnabled(false);
ui->ConnectBtn->setEnabled(true);
}
void Widget::Send()
{
qDebug("send message");
int length = 0;
QString Msg= ui->SendMsg_lineEdit->text();
if((length = TcpSocket->write(Msg.toStdString().c_str(),strlen(Msg.toStdString().c_str()))) != Msg.length())
{
qDebug("send fail");
}
qDebug("%d", length);
qDebug()<< Msg;
}
void Widget::ClearSend()
{
ui->SendMsg_lineEdit->setText ("");
}
void Widget::ClearReceive()
{
ui->Receive_textBrowser->setText("");
}
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setFixedSize( this->width (),this->height ()); //固定窗口大小
TcpSocket = new QTcpSocket(this);
connect(ui->ConnectBtn,SIGNAL(clicked()),this,SLOT(Connect()));
connect(ui->StopBtn,SIGNAL(clicked()),this,SLOT(Stop()));
connect(ui->ClearReceiveBtn,SIGNAL(clicked()),this,SLOT(ClearReceive()));
connect(ui->ClearSendBtn,SIGNAL(clicked()),this,SLOT(ClearSend()));
connect(ui->SendBtn,SIGNAL(clicked()),this,SLOT(Send()));
connect(TcpSocket,SIGNAL(readyRead()),this,SLOT(ReadMessage()));
connect(TcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),
this,SLOT(displayError(QAbstractSocket::SocketError)));
}
Widget::~Widget()
{
delete ui;
}
void Widget::displayError(QAbstractSocket::SocketError)
{
qDebug() << TcpSocket->errorString(); //输出错误信息
}
void Widget::ReadMessage()
{
qDebug("Read");
if(TcpSocket->waitForConnected(30000)){
QString Msg;
QByteArray qba = TcpSocket->readAll();
Msg=QVariant(qba).toString();
ui->Receive_textBrowser->append(Msg);}
}
void Widget::Connect()
{
// BlockSize = 0; //初始化其为0
TcpSocket->abort(); //取消已有的连接
TcpSocket->connectToHost(ui->Remote_IP_lineEdit->text(),
ui->Remote_Port_lineEdit->text().toInt());
ui->ConnectBtn->setEnabled(false);
ui->StopBtn->setEnabled(true);
}
void Widget::Stop()
{
TcpSocket->close();
qDebug("socket break");
ui->StopBtn->setEnabled(false);
ui->ConnectBtn->setEnabled(true);
}
void Widget::Send()
{
qDebug("send message");
int length = 0;
QString Msg= ui->SendMsg_lineEdit->text();
if((length = TcpSocket->write(Msg.toStdString().c_str(),strlen(Msg.toStdString().c_str()))) != Msg.length())
{
qDebug("send fail");
}
qDebug("%d", length);
qDebug()<< Msg;
}
void Widget::ClearSend()
{
ui->SendMsg_lineEdit->setText ("");
}
void Widget::ClearReceive()
{
ui->Receive_textBrowser->setText("");
}
...全文
相关推荐
QT/C++ TCP多线程客户端(收发线程分离,自动粘包处理,自动数据包成型)
`SmTDemo`可能是客户端的示例应用或测试程序,展示了如何使用`SmTClientTcp`类进行实际操作。
QTcpSocket.rar
在主线程中处理网络操作可能会阻塞应用程序的用户界面,因此一般不推荐这样做。然而,对于简单应用或测试用例,这可以是一个快速的实现方式。
基于QT5编写的TCP SOCKET通信小demo程序,通讯数据用QJSON封装。
一旦客户端连接成功,双方就可以通过QTcpSocket的read和write方法来发送和接收数据。
QT测试tcp通讯SOCKET实例程序
QTcpSocket用于创建客户端,它能够建立到远程服务器的TCP连接,并发送和接收数据。而QTcpServer则用于创建服务器端,它可以监听特定端口,等待客户端的连接请求。
基于QT5实现一个简单好用的TCP调试助手
QTcpServer负责监听特定的网络端口,当接收到客户端连接请求时,会创建一个新的QTcpSocket实例来处理这个连接。QTcpSocket则用于客户端,用于发起连接请求并发送、接收数据。


