Qt客户程序
Qt client programme
我在Qt上为客户端编写了一个程序,用于从服务器接收数据,但它没有接收数据,并将接收的字节显示为零,以下是我的程序:
//client.h
#ifndef CLIENT_H
#define CLIENT_H
#include <QObject>
#include <QString>
#include <QtNetwork/QTcpSocket>
class Client: public QObject
{
Q_OBJECT
public:
Client(QObject* parent = 0);
~Client();
void start(QString address, quint16 port);
void send(const char*);
void receive();
public slots:
void startTransfer();
private:
QTcpSocket client;
};
#endif // CLIENT_H
//客户端.cpp
#include "client.h"
#include <QtNetwork/QHostAddress>
#include<QIODevice>
Client::Client(QObject* parent): QObject(parent)
{
connect(&client, SIGNAL(connected()),
this, SLOT(startTransfer()));
//connect(&client, SIGNAL(waitForBytesWritten()),
// this, SLOT(receive()));
}
Client::~Client()
{
client.close();
}
void Client::start(QString address, quint16 port)
{
QHostAddress addr(address);
client.connectToHost(addr, port);
}
void Client::startTransfer()
{
client.write("Connection Established", 22);
}
void Client::send(const char *buffer)
{
client.write(buffer,sizeof(buffer));
}
void Client::receive()
{
char temp[1024] = {0};
int len = client.read(temp,client.bytesAvailable());
printf("tData recieved from server :: %sn",temp);
printf("tSize of data received is :: %dn",client.bytesAvailable());
printf("tBytes read is :: %dn",len);
}
//main.cpp
#include <QCoreApplication>
#include "client.h"
//#include <QApplication>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Client client;
client.start("192.168.1.2", 9602);
char buff[] = "Send operation performed from main";
client.send(buff);
// while(1)
client.receive();
return a.exec();
}
在这里,我的程序函数执行,然后停止接收(可能是),当我从服务器发送任何东西时,它不需要任何东西。有什么建议吗?如果我犯了任何愚蠢的编程错误,请不要无礼,因为我是新手。
您没有得到@Merlin069的答案。。。。你应该使用readyRead在信号的位置和你的receiveve函数作为插槽。。。它会起作用的。我希望你能理解这种简单的语言。
您可以尝试使用while(1),并在其中编写接收函数
相关文章:
- 使用调试/崩溃报告将应用程序部署到客户端
- 在 1 个服务器 n 客户端套接字 C++ MFC 应用程序中更新数据的客户端
- C++库和自注册类:客户端应用程序中的工厂映射为空
- OpenGL GLFW 应用程序 - 客户端内存限制
- CMake导出的库-在客户端应用程序中找不到include/Lib路径
- 我的web应用程序的用户需要在浏览器上手动启用本机客户端吗
- 客户端发送消息,但服务器在客户端结束程序之前不接收消息
- C++启动另一个程序(客户端-服务器)的程序
- 我正在编写一个简单的客户端套接字应用程序,但在连接后服务器收到一个空缓冲区
- C++客户端/服务器聊天应用程序 - 从 Ubuntu 命令窗口扫描输入
- C++服务器/客户端聊天程序
- UDP客户端/服务器程序在不同的IP和端口上通信超过2个插座
- DLL – 在 DLL 初始化时填充的静态向量,向客户端程序返回零大小
- 防止客户端套接字程序崩溃CPP Google Protobuf
- TCP 客户端服务器程序
- 如何避免在C 11服务器程序中为多个客户端使用多个线程
- 必须在客户端计算机上安装的内容才能运行QT快速应用程序
- 客户端服务器回声程序进入UDP的僵局
- GDB执行时,Apache IGNITE C 客户端应用程序具有许多细分故障
- C++ - 程序在尝试在 TCP 客户端上连接到不存在的地址时表现出奇怪的行为