QTcpServer類 用來偵聽端口 ,獲取QTcpSocket.ios
QTcpSocket有 connected的信號(已經鏈接),還有readyread()信號,表示已經有數據發過來了,準備讀取。服務器
若要關閉 當前鏈接只須要 調用 qtcpsocket::close();就關閉了當前鏈接socket
下面有兩個例子tcp
服務器端ui
用的是控制檯程序(QT) 當用戶 發送數據過來 就cout顯示,而後就write一個 I Love You的字符串 返回到客戶端。而後close斷開鏈接this
客戶端spa
用的書圖形界面,一個輸入框 輸入數據 而後發送,最後 QMessagebox顯示服務器返回消息code
=======================server
服務器端(三個文件)ci
#ifndef MYSERVER_H #define MYSERVER_H #include<QTcpSocket> #include<iostream> #include <QObject> #include<QTcpServer> class myserver : public QTcpServer { Q_OBJECT public: QTcpSocket * socket; QTcpServer *server; myserver(); private slots: void getData(); void newconnectslot(); }; #endif // MYSERVER_H #include "myserver.h" #include<QByteArray> #include<QString> #include<QDataStream> myserver::myserver() { this->socket=new QTcpSocket; this->server=new QTcpServer; if(this->server->listen(QHostAddress::Any,520)) { std::cout<<"bind port 520 successful."<<std::endl; }else { std::cout<<"bind port 520 failed."<<std::endl; } QObject::connect(this->server,SIGNAL(newConnection()),this,SLOT(newconnectslot())); } void myserver::newconnectslot() { this->socket=this->server->nextPendingConnection(); connect(this->socket,SIGNAL(readyRead()),this,SLOT(getData())); } void myserver::getData() { QByteArray by=this->socket->readAll(); QDataStream ds(by); QString x; ds>>x; QByteArray ba = x.toLatin1(); char * p=ba.data(); std::cout<<p<<std::endl; socket->write("I love you");//返回給客戶端 this->socket->close();//斷開鏈接 } #include <QCoreApplication> #include<iostream> #include"myserver.h" #include<QHostAddress> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); std::cout<<"--Server initialized By HanHan--"<<std::endl; myserver *server=new myserver; return a.exec(); }
客戶端(三個文件)
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include<QString> #include<QByteArray> #include<QDataStream> #include<QTcpSocket> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: QTcpSocket * socket; explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void connnectslot(); void on_btn_send_clicked(); void readyslot(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
#include "mainwindow.h" #include "ui_mainwindow.h" #include<QHostAddress> #include<QMessageBox> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); this->socket=new QTcpSocket; } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_btn_send_clicked() { QHostAddress address("127.0.0.1"); this->socket->connectToHost(address,520); connect(this->socket,SIGNAL(connected()),this,SLOT(connnectslot())); connect(this->socket,SIGNAL(readyRead()),this,SLOT(readyslot()));//接收發來的數據 } void MainWindow::connnectslot() { QString data=this->ui->data_edit->toPlainText(); QByteArray array; QDataStream ds(&array,QIODevice::WriteOnly); ds<<data; this->socket->write(array); }
void MainWindow::readyslot() { QString x=this->socket->readAll(); QMessageBox::about(this,"x",x); } #include "mainwindow.h" #include <QApplicatio>int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
運行截圖:
n