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
- QT之不同主機之間TCP通訊QTTCP
- TCP通訊TCP
- JAVA控制檯下:控制檯商城購物系統Java
- Qt usb通訊QT
- Qt ModbusTCP通訊QTTCP
- Qt TCP通訊客戶端斷開連線有哪些方法QTTCP客戶端
- modbus tcp通訊TCP
- 百度識別例程-QT介面-識別列表-串列埠通訊-文字影像QT串列埠
- 【iCore3 雙核心板】例程四:USART通訊實驗——通過命令控制LED
- 網路通訊2:TCP通訊實現TCP
- 網路通訊3:TCP互動通訊TCP
- 網路通訊2:TCP簡單通訊TCP
- Linux TCP通訊示例LinuxTCP
- Android TCP socket通訊AndroidTCP
- NModbus4 TCP通訊TCP
- 【iCore1S 雙核心板_ARM】例程四:USART通訊實驗——通過命令控制LED
- C++學習筆記(七) - Qt 在控制檯輸出C++筆記QT
- mac下使用mysql控制檯命令列MacMySql命令列
- QT從入門到入土(九)——TCP/IP網路通訊(以及檔案傳輸)QTTCP
- QT硬體通訊基礎QT
- 上位機面試必備——TCP通訊靈魂二十問【下】面試TCP
- TCP/IP 通訊傳輸流TCP
- Java實現TCP通訊程式JavaTCP
- 通訊協議:HTTP、TCP、UDP協議HTTPTCPUDP
- Linux中的TCP通訊LinuxTCP
- 用VB5 Winsock控制元件建立TCP/IP通訊程式控制元件TCP
- Qt 程序間通訊,Qt子程序嵌入主程序QT
- 【iCore2雙核心板視訊教程二】iM_LAN 100M 乙太網模組TCP通訊例程TCP
- zookeeper控制檯
- C語言實現TCP通訊C語言TCP
- 網路通訊4:TCP廣播TCP
- TCP/IP的通訊過程-VeCloudTCPCloud
- Go語言實現TCP通訊GoTCP
- Vert.x TCP 通訊實踐TCP
- 西門子PLC Modus TCP通訊TCP
- Linux學習/TCP Socket通訊LinuxTCP
- 在Linux控制檯下顯示JPEG影像Linux