调用完功能函数(如connectToHost)立刻调用对应的同步辅助函数即实现同步编程
代码运行时需启动socket的服务程序模拟tcp服务端
main.cpp
#include "clientdemo.h" #include <QtGui/QApplication> #include <QTcpSocket> #include <QDebug> void SyncClientDemo() { QTcpSocket client; char buf[256] = {0}; client.connectToHost("127.0.0.1", 8080); qDebug() << "Connected:" << client.waitForConnected(); qDebug() << "Send Bytes:" << client.write("D.T.Software"); qDebug() << "Send Status:" << client.waitForBytesWritten(); qDebug() << "Data Available:" << client.waitForReadyRead(); qDebug() << "Received Bytes:" << client.read(buf, sizeof(buf)-1); qDebug() << "Received Data:" << buf; client.close(); // client.waitForDisconnected(); } int main(int argc, char *argv[]) { QApplication a(argc, argv); SyncClientDemo(); //ClientDemo w; //w.show(); return a.exec(); }ClientDemo.cpp
#include "ClientDemo.h" #include <QHostAddress> #include <QDebug> ClientDemo::ClientDemo(QObject* parent) : QObject(parent) { connect(&m_client, SIGNAL(connected()), this, SLOT(onConnected())); connect(&m_client, SIGNAL(disconnected()), this, SLOT(onDisconnected())); connect(&m_client, SIGNAL(readyRead()), this, SLOT(onDataReady())); connect(&m_client, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64))); } void ClientDemo::onConnected() { qDebug() << "onConnected"; qDebug() << "Local Address:" << m_client.localAddress(); qDebug() << "Local Port:" << m_client.localPort(); } void ClientDemo::onDisconnected() { qDebug() << "onDisconnected"; } void ClientDemo::onDataReady() { char buf[256] = {0}; qDebug() << "onDataReady:" << m_client.read(buf, sizeof(buf)-1); qDebug() << "Data:" << buf; } void ClientDemo::onBytesWritten(qint64 bytes) { qDebug() << "onBytesWritten:" << bytes; } void ClientDemo::connectTo(QString ip, int port) { m_client.connectToHost(ip, port); } qint64 ClientDemo::send(const char* data, int len) { return m_client.write(data, len); } qint64 ClientDemo::available() { return m_client.bytesAvailable(); } void ClientDemo::close() { m_client.close(); }结果
clientdemo.h
#ifndef CLIENTDEMO_H #define CLIENTDEMO_H #include <QObject> //#include "ui_clientdemo.h" #include <QTcpSocket> class ClientDemo : public QObject { Q_OBJECT QTcpSocket m_client; protected slots: void onConnected(); void onDisconnected(); void onDataReady(); void onBytesWritten(qint64 bytes); public: qint64 send(const char* data, int len); qint64 available();//得到到达本机的数据长度 ClientDemo(QObject *parent = NULL); void connectToHost(QString ip,int port); void close(); ~ClientDemo(); private: // Ui::ClientDemoClass ui; }; #endif // CLIENTDEMO_Hclientdemo.cpp
#include "clientdemo.h" #include <QDebug> ClientDemo::ClientDemo(QObject *parent): QObject(parent) { //ui.setupUi(this); connect(&m_client, SIGNAL(connected()), this, SLOT(onConnected()) ); connect(&m_client, SIGNAL(disconnected()), this, SLOT(onDisconnected())); connect(&m_client, SIGNAL(readyRead()), this, SLOT(onDataReady())); connect(&m_client, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64))); } void ClientDemo::onConnected() { qDebug() << "onConnected"; qDebug() << "Local Address:" ;//<< m_client.localAddress(); qDebug() << "Local Port:" << m_client.localPort(); } void ClientDemo::onDisconnected() { qDebug() << "onDisconnected"; } void ClientDemo::onDataReady() { char buf[256] = {0}; qDebug() << "onDataReady:" << m_client.read(buf, sizeof(buf)-1); qDebug() << "Data:" << buf; } void ClientDemo::onBytesWritten(qint64 bytes) { qDebug() << "onBytesWritten:" << bytes; } void ClientDemo::connectToHost(QString ip,int port) { m_client.connectToHost(ip, port); } qint64 ClientDemo::send(const char* data, int len) { return m_client.write(data, len); } qint64 ClientDemo::available() { return m_client.bytesAvailable(); } void ClientDemo::close() { m_client.close(); } ClientDemo::~ClientDemo() { }main.cpp
#include "clientdemo.h" #include <QtGui/QApplication> #include <QTcpSocket> #include <QDebug> int main(int argc, char *argv[]) { QApplication a(argc, argv); //SyncClientDemo(); ClientDemo w; w.connectToHost("127.0.0.1",8080); char buf[]="abc"; w.send(buf,sizeof(buf)-1); //w.show(); return a.exec(); }结果