您的位置:首页 > 理论基础 > 计算机网络

QT下控制台TCP通信例程

2012-01-13 00:08 453 查看
 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  qt tcp server signal buffer class