QT下控制檯TCP通訊例程

jpsyq發表於2012-01-13
 QT下的TCP通訊例程大多是基於介面的,好不容易找到一個基於控制檯的TCP通訊基本例程,轉載過來並略作修改,與大家分享,供QT下socket程式設計入門學習之用。
原文地址http://hacktao.com/2010/03/11/50

服務端server:

// server's main.cpp

#include <QtCore/QCoreApplication>
#include <iostream>
#include "server.h"

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    //從class的類建構函式呼叫套接字
    Server server;
    //顯示狀態
    server.begin();
    return a.exec();
}


// server.cpp
#include "server.h"
#include <QHostAddress>
#include <iostream>

using namespace std;

Server::Server(QObject* parent): QObject(parent)
{
    //將newConnection()與acceptConnection()連結
    connect(&server, SIGNAL(newConnection()),this,SLOT(acceptConnection()));
    cout<<"listening..."<<endl;
    //bool QTcpServer::listen ( const QHostAddress & address = QHostAddress::Any, quint16 port = 0 )
    //Tells the server to listen for incoming connections on address address and port port.
    //If port is 0, a port is chosen automatically.
    //If address is QHostAddress::Any, the server will listen on all network interfaces.
    //在任意網路介面監聽
    //server.listen(QHostAddress::Any,8888);
    //在服務端IP地址172.18.218.2的埠8888監聽
    QHostAddress addr("172.18.218.2");
    server.listen(addr, 8888);
    cout<<"listen..."<<endl;
}

Server::~Server()
{
    cout<<"closing..."<<endl;
    //關閉伺服器
    server.close();
}

void Server::acceptConnection()
{
    //返回等待的連線
    cout<<"connecting..."<<endl;
    client = server.nextPendingConnection();
    //將readyRead()與startRead()連結
    connect(client, SIGNAL(readyRead()),this, SLOT(startRead()));
}

void Server::startRead()
{
    char buffer[1024] = {0};
    //讀入資料
    cout<<"reading..."<<endl;
    client->read(buffer, client->bytesAvailable());
    cout<<"outputing..."<<endl;
    //顯示資料
    cout << buffer << endl;
    //關閉客戶端
    client->close();
}

void Server::begin()
{
    //顯示狀態
     cout<<"try to connect..."<<endl;
}


//server.h
#ifndef SERVER_H
#define SERVER_H

#include <QtNetwork>
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>

class Server: public QObject
{
Q_OBJECT
public:
    Server(QObject * parent = 0);
    ~Server();
    void begin();

public slots:
    void acceptConnection();
    void startRead();

private:
    QTcpServer server;
    QTcpSocket* client;
};

#endif // SERVER_H


 

客戶端client:

// client's main.cpp
#include <QtCore/QCoreApplication>
#include <iostream>
#include "client.h"

using namespace std;

int main(int argc, char* argv[])
{
    QCoreApplication a(argc, argv);
    Client client;
    //開始建立TCP連線,傳送資料
    //本機環回通訊
    //client.start("127.0.0.1", 8888);
    //服務端/客戶端通訊,填入服務端IP地址與埠號
    client.start("172.18.216.230", 8888);
    return a.exec();
}


//client.cpp
#include <QHostAddress>
#include <iostream>
#include "client.h"

using namespace std;

Client::Client(QObject* parent): QObject(parent)
{
    //建立connected()函式與startTransfer()函式的連結
    connect(&client, SIGNAL(connected()),this, SLOT(startTransfer()));
}

Client::~Client()
{
    //關閉客戶端
    client.close();
}

void Client::start(QString address, quint16 port)
{
    //顯示狀態
    cout<<"client begins to connect..."<<endl;
    //The QHostAddress class provides an IP address.
    //This class holds an IPv4 or IPv6 address in a platform- and protocol-independent manner.
    //建立QHostAddress類的物件
    QHostAddress addr(address);
    //跟服務端連線
    //Attempts to make a connection to hostName on the given port.
    client.connectToHost(addr, port);
}

void Client::startTransfer()
{
    //qint64 QIODevice::write ( const char * data, qint64 maxSize )
    //Writes at most maxSize bytes of data from data to the device.
    //Returns the number of bytes that were actually written, or -1 if an error occurred.
    //寫入資料到裝置
    client.write("hello qt!", 9);
}

//client.h
#ifndef CLIENT_H
#define CLIENT_H

#include <QtNetwork>
#include <QObject>
#include <QString>
#include <QTcpSocket>

class Client: public QObject
{
    Q_OBJECT
public:
    Client(QObject* parent = 0);
    ~Client();
    void start(QString address, quint16 port);

public slots:
    void startTransfer();

private:
    QTcpSocket client;

};
#endif // CLIENT_H

相關文章