QT下控制檯TCP通訊例程
原文地址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 控制檯訊號與槽簡例QT
- JAVA控制檯下:控制檯商城購物系統Java
- linux下使用tomcat檢視控制檯資訊LinuxTomcat
- TCP通訊TCP
- Qt TCP通訊客戶端斷開連線有哪些方法QTTCP客戶端
- 百度識別例程-QT介面-識別列表-串列埠通訊-文字影像QT串列埠
- modbus tcp通訊TCP
- Qt ModbusTCP通訊QTTCP
- Qt usb通訊QT
- Linux TCP通訊示例LinuxTCP
- 網路通訊3:TCP互動通訊TCP
- 網路通訊2:TCP簡單通訊TCP
- 網路通訊2:TCP通訊實現TCP
- NModbus4 TCP通訊TCP
- zookeeper控制檯
- QT從入門到入土(九)——TCP/IP網路通訊(以及檔案傳輸)QTTCP
- Java實現TCP通訊程式JavaTCP
- TCP/IP 通訊傳輸流TCP
- Linux學習/TCP Socket通訊LinuxTCP
- QT硬體通訊基礎QT
- 控制檯快捷鍵
- rocketmq 管理控制檯MQ
- 上位機面試必備——TCP通訊靈魂二十問【下】面試TCP
- Qt 程序間通訊,Qt子程序嵌入主程序QT
- 解決maven專案控制檯資訊亂碼Maven
- Mac控制檯的漸變色玩一下!Mac
- 網路通訊4:TCP廣播TCP
- liunx通過TCP傳送資訊TCP
- TCP/IP的通訊過程-VeCloudTCPCloud
- C語言實現TCP通訊C語言TCP
- 西門子PLC Modus TCP通訊TCP
- Go語言實現TCP通訊GoTCP
- 在 pycharm 裡通過 pytest 執行用例,控制檯沒有輸出日誌資訊PyCharm
- 物聯網通訊 – RESTDemo示例程式(C#版本)RESTC#
- Chrome控制檯技巧篇Chrome
- 控制檯編碼系列
- rocketmq控制檯安裝MQ
- VKDebugConsole App黑盒控制檯GCAPP
- Qt TCP (小型聊天視窗)QTTCP