QT中的SOCKET的通訊
1、服務端程式
- //ui_widget.h
- #ifndef UI_WIDGET_H
- #define UI_WIDGET_H
- #include <QtCore/QVariant>
- #include <QtGui/QAction>
- #include <QtGui/QApplication>
- #include <QtGui/QButtonGroup>
- #include <QtGui/QHeaderView>
- #include <QtGui/QPushButton>
- #include <QtGui/QTextEdit>
- #include <QtGui/QWidget>
- QT_BEGIN_NAMESPACE
- class Ui_Widget
- {
- public:
- QTextEdit *textSend;
- QPushButton *send;
- QTextEdit *textReceive;
- QPushButton *receive;
- void setupUi(QWidget *Widget)
- {
- if (Widget->objectName().isEmpty())
- Widget->setObjectName(QString::fromUtf8("Widget"
- Widget->resize(626, 286);
- textSend = new QTextEdit(Widget);
- textSend->setObjectName(QString::fromUtf8("textSend"));
- textSend->setGeometry(QRect(10, 10, 281, 191));
- send = new QPushButton(Widget);
- send->setObjectName(QString::fromUtf8("send"
- send->setGeometry(QRect(10, 220, 281, 41));
- textReceive = new QTextEdit(Widget);
- textReceive->setObjectName(QString::fromUtf8("textReceive"));
- textReceive->setGeometry(QRect(320, 10, 281, 191));
- receive = new QPushButton(Widget);
- receive->setObjectName(QString::fromUtf8("receive"));
- receive->setGeometry(QRect(320, 220, 281, 41));
- retranslateUi(Widget);
- QMetaObject::connectSlotsByName(Widget);
- } // setupUi
- void retranslateUi(QWidget *Widget)
- {
- Widget->setWindowTitle(QApplication::translate("Widget", "Widget", 0, QApplication::UnicodeUTF8));
- send->setText(QApplication::translate("Widget", "/345/217/221/351/200/201", 0, QApplication::UnicodeUTF8));
- receive->setText(QApplication::translate("Widget", "/346/216/245/346/224/266", 0, QApplication::UnicodeUTF8));
- Q_UNUSED(Widget);
- } // retranslateUi
- };
- namespace Ui {
- class Widget: public Ui_Widget {};
- } // namespace Ui
- QT_END_NAMESPACE
- #endif // UI_WIDGET_H
- //widget.h
- #ifndef WIDGET_H
- #define WIDGET_H
- #include <QtGui/QWidget>
- #include <QUdpSocket>
- namespace Ui
- {
- class Widget;
- }
- class Widget : public QWidget
- {
- Q_OBJECT
- public:
- Widget(QWidget *parent = 0);
- ~Widget();
- int port1;
- int port2;
- QHostAddress *hostaddr1;
- QHostAddress *hostaddr2;
- private:
- Ui::Widget *ui;
- QUdpSocket *udpSocket;
- private slots:
- void send();
- void receive();
- };
- #endif // WIDGET_H
- //widget.cpp
- #include "widget.h"
- #include "ui_widget.h"
- #include <QMessageBox>
- Widget::Widget(QWidget *parent)
- : QWidget(parent), ui(new Ui::Widget)
- {
- ui->setupUi(this);
- port1=4444;
- port2=4445;
- hostaddr1 = new QHostAddress("10.10.19.162");
- hostaddr2 = new QHostAddress("10.10.19.161");
- //建立 QUdpSocket 物件
- udpSocket=new QUdpSocket(this);
- bool conn=udpSocket->bind(*hostaddr2,port2); //伺服器端IP為10.10.19.161,埠為4445
- //連結失敗
- if(!conn){
- QMessageBox box;
- box.setText(tr("連結錯誤"));
- box.exec();
- }else{
- //把udpSocket的訊號關聯到槽
- connect(udpSocket,SIGNAL(readyRead()),this,SLOT(receive()));
- connect(ui->send,SIGNAL(clicked()),this,SLOT(send()));
- }
- setWindowTitle(tr("伺服器端"));
- }
- Widget::~Widget()
- {
- delete ui;
- }
- void Widget::send()
- {
- QMessageBox box;
- QString text=ui->textSend->toPlainText();
- if(text.length()==0){
- box.setText(tr("請輸入傳送內容"));
- }
- /*開始傳送資料*/
- //初始化長度
- int len=0;
- //udpSocket->writeDatagram(傳送的資料,傳送資料的長度,IP,埠); 返回一個長度.
- len=udpSocket->writeDatagram(text.toLatin1(),text.length(),*hostaddr1,port1);
- if(len){
- box.setText(tr("傳送成功"));
- }
- box.exec();
- }
- void Widget::receive()
- {
- while(udpSocket->hasPendingDatagrams()){ //是否讀到資料
- QByteArray data;
- //udpSocket->pendingDatagramSize 獲取報文長度
- //data.resize 給 data 陣列設定長度
- data.resize(udpSocket->pendingDatagramSize());
- //讀入資料
- udpSocket->readDatagram(data.data(),data.size());
- //顯示資料內容
- QString str=data.data();
- ui->textReceive->insertPlainText(str+"/r/n");
- }
- }
- //main.cpp
- #include <QtGui/QApplication>
- #include <QTextCodec>
- #include "widget.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- QTextCodec::setCodecForTr(QTextCodec::codecForName("gb2312"));
- Widget w;
- w.show();
- return a.exec();
- }
2、客戶端程式
- //ui_widget.h
- #ifndef UI_WIDGET_H
- #define UI_WIDGET_H
- #include <QtCore/QVariant>
- #include <QtGui/QAction>
- #include <QtGui/QApplication>
- #include <QtGui/QButtonGroup>
- #include <QtGui/QHeaderView>
- #include <QtGui/QPushButton>
- #include <QtGui/QTextEdit>
- #include <QtGui/QWidget>
- QT_BEGIN_NAMESPACE
- class Ui_Widget
- {
- public:
- QPushButton *close;
- QTextEdit *textReceive;
- QPushButton *send;
- QTextEdit *textSend;
- void setupUi(QWidget *Widget)
- {
- if (Widget->objectName().isEmpty())
- Widget->setObjectName(QString::fromUtf8("Widget"));
- Widget->resize(604, 275);
- close = new QPushButton(Widget);
- close->setObjectName(QString::fromUtf8("close"));
- close->setGeometry(QRect(20, 20, 251, 41));
- textReceive = new QTextEdit(Widget);
- textReceive->setObjectName(QString::fromUtf8("textReceive"));
- textReceive->setGeometry(QRect(20, 80, 251, 171));
- send = new QPushButton(Widget);
- send->setObjectName(QString::fromUtf8("send"));
- send->setGeometry(QRect(320, 20, 251, 41));
- textSend = new QTextEdit(Widget);
- textSend->setObjectName(QString::fromUtf8("textSend"));
- textSend->setGeometry(QRect(320, 80, 251, 171));
- retranslateUi(Widget);
- QMetaObject::connectSlotsByName(Widget);
- } // setupUi
- void retranslateUi(QWidget *Widget)
- {
- Widget->setWindowTitle(QApplication::translate("Widget", "Widget", 0, QApplication::UnicodeUTF8));
- close->setText(QApplication::translate("Widget", "/345/205/263/351/227/255", 0, QApplication::UnicodeUTF8));
- send->setText(QApplication::translate("Widget", "/345/217/221/351/200/201", 0, QApplication::UnicodeUTF8));
- Q_UNUSED(Widget);
- } // retranslateUi
- };
- namespace Ui {
- class Widget: public Ui_Widget {};
- } // namespace Ui
- QT_END_NAMESPACE
- #endif // UI_WIDGET_H
- //widget.h
- #ifndef WIDGET_H
- #define WIDGET_H
- #include <QtGui/QWidget>
- #include <QUdpSocket>
- namespace Ui
- {
- class Widget;
- }
- class Widget : public QWidget
- {
- Q_OBJECT
- public:
- Widget(QWidget *parent = 0);
- ~Widget();
- int port1;
- int port2;
- QHostAddress *hostaddr1;
- QHostAddress *hostaddr2;
- private:
- Ui::Widget *ui;
- QUdpSocket *udpSocket;
- private slots:
- void send();
- void receive();
- };
- #endif // WIDGET_H
- //widget.cpp
- #include "widget.h"
- #include "ui_widget.h"
- #include <QTextCodec>
- #include <QMessageBox>
- Widget::Widget(QWidget *parent)
- : QWidget(parent), ui(new Ui::Widget)
- {
- ui->setupUi(this);
- port1=4444;
- port2=4445;
- hostaddr1 = new QHostAddress("10.10.19.162");
- hostaddr2 = new QHostAddress("10.10.19.161");
- setWindowTitle(tr("接收端"));
- //例項化QUdpSocket 物件...
- udpSocket=new QUdpSocket(this);
- //監聽埠
- bool conn=udpSocket->bind(*hostaddr1,port1); //客戶端的IP為10.10.19.162,埠號為4444。
- //連結失敗
- if(!conn){
- QMessageBox box;
- box.setText(tr("連結錯誤"));
- box.exec();
- }else{
- //把udpSocket的訊號關聯到槽
- connect(udpSocket,SIGNAL(readyRead()),this,SLOT(receive()));
- connect(ui->send,SIGNAL(clicked()),this,SLOT(send()));
- }
- connect(ui->close,SIGNAL(clicked()),this,SLOT(close()));
- }
- Widget::~Widget()
- {
- delete ui;
- }
- void Widget::send()
- {
- QMessageBox box;
- QString text=ui->textSend->toPlainText();
- if(text.length()==0){
- box.setText(tr("請輸入傳送內容"));
- }
- /*開始傳送資料*/
- //初始化長度
- int len=0;
- //udpSocket->writeDatagram(傳送的資料,傳送資料的長度,IP,埠); 返回一個長度.
- len=udpSocket->writeDatagram(text.toLatin1(),text.length(),*hostaddr2,port2); //傳送的字元傳送到IP為10.10.19.161和埠為4445的服務端
- if(len){
- box.setText(tr("傳送成功"));
- }
- box.exec();
- }
- void Widget::receive()
- {
- while(udpSocket->hasPendingDatagrams()){ //是否讀到資料
- QByteArray data;
- //udpSocket->pendingDatagramSize 獲取報文長度
- //data.resize 給 data 陣列設定長度
- data.resize(udpSocket->pendingDatagramSize());
- //讀入資料
- udpSocket->readDatagram(data.data(),data.size());
- //顯示資料內容
- QString str=data.data();
- ui->textReceive->insertPlainText(str+"/r/n");
- }
- }
- //main.cpp
- #include <QtGui/QApplication>
- #include <QTextCodec>
- #include "widget.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
- Widget w;
- w.show();
- return a.exec();
- }
3、執行程式如下。
客戶端傳送receive字元時伺服器接收框中顯示receive。服務端傳送service時,客戶端接收框中顯示service
4、說明
本程式實際實現的是TCP協議,因為雙方都綁定了自己的IP和埠,只有同時識別IP和埠號才能正確的傳送和接收資料。
不過,也可以更改為UDP協議或直接用廣播的形式。
這個程式只是個簡單的例項,但可以通過這個小的例項,將其嵌入到大型的程式裡面,以實現複雜的SOCKET通訊。
相關推薦
QT 中Socket客戶端與伺服器異常斷開後重連
在現在的專案開發中,經常要使用TCP/IP協議來進行通訊,但有時候與伺服器端的連結由於網路問題導致連線異常或斷開,這就需要我們的軟體能自動重連,在Linux中,我們的思維一般是通過心跳包來監控連線是否斷開,有時候還單獨開一個執行緒,但是在QT中,就變得簡單多了,當連線異常
關於Java中Socket通訊時使用ObjectInputStream與ObjectOutputStream的順序問題
在Java中使用Socket與ServerSocket建立客戶機和伺服器時,若採用ObjectInputStream與ObjectOutputStream建立通訊,則需要注意兩個流的順序。否則會發生兩方互相等待導致死鎖。 下面通過一個例子來證明: 伺服器: package com.gar
Android中Socket通訊的簡單實現
前言Android Framework 層程式碼中大量使用了 Binder IPC 通訊方式,除此之外,Socket 也是一種重要的 IPC 通訊方式,比如StorageManagerService(8.0 之前叫 MountService)與 Vold 之前的通訊,Syst
C#中Socket通訊程式設計的非同步實現
本文將在C#中Socket同步通訊的基礎上,分析和研究Socket非同步程式設計的實現方法,目的是深入瞭解Socket程式設計的基本原理,增強對網路遊戲開發相關內容的認識。 什麼是Socket程式設計的非同步是實現 所謂Socket程式設計的非同步實現是指按
QT TCP socket通訊(一)
TCP即Transmission Control Protocol,傳輸控制協議。與UDP不同,它是面向連線和資料流的可靠傳輸協議。也就是說,它能使一臺計算機上的資料無差錯的發往網路上的其他計算機,所以當要傳輸大量資料時,我們選用TCP協議。 TCP協議的程式使用的是客
詳解C#中Socket通訊(四):使用雜湊表管理客戶端
public void StartListening() { //獲取本機IP地址 IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName()); HostIP = ipHo
QT中的SOCKET的通訊
1、服務端程式//ui_widget.h#ifndef UI_WIDGET_H#define UI_WIDGET_H#include <QtCore/QVariant>#include <QtGui/QAction>#include &l
Qt中採用多執行緒實現Socket程式設計
本文介紹的是Qt中採用多執行緒Socket程式設計,由於工作專案的需要,使用Qt的socket程式設計。Qt裡的example是個不錯的教程,但是當我把程式碼移植
Qt中Qchart外掛實現PMW3901迷你光流模組上位機(串列埠通訊)
文章目錄 Qt中Qchart外掛實現PMW3901迷你光流模組上位機 光流計介紹 上位機 完整工程[猛戳這兒](https://github.com/LiangtaoZhong/PMW3901-monitor)。
JAVA中Socket服務端和客戶端網路通訊簡單案例
一. 簡單介紹下Socket的用途: 1.Java最初是作為網路程式語言出現的,其對網路提供了高度的支援,使得客戶端和伺服器的溝通變成了現實,而在網路程式設計中,使用最多的就是Socket。像大家熟悉的QQ、MSN都使用了Socket相關的技術 2.IP地址+埠號組成了所
Android 中的Socket通訊
公司新專案中涉及到Socket通訊有關的東西,雖然之前接觸到一點,不過好長時間不用基本上忘個七七八八了,網上查了查資料,根據專案中的需求自己做了個小Demo,歡迎大家指正. 1.需求: 1.1:客戶端測量完畢後,將測量資料以Socket的方式上傳
Socket通訊中粘包分包問題的介紹和解決(C#)
最近在做Unity區域網時,用到了Socket通訊基於TCP協議,然後使用非同步方式,主要用到了BeginAccept和BeginReceive方法 然而就可以實現非同步通訊,然而還是要解決粘包和分包問題 這裡我先說明一下什麼是分包和粘包,TCP提供面向連線的、可靠的資料流傳輸,所以當我們傳
(websocket)協議中Ping Pong,Socket通訊ping pong(長連線)
- websocket協議,長連線;Http短連線 WebSocket如何建立連線、交換資料的細節,以及資料幀的格式。 WebSocket複用了HTTP的握手通道。具體指的是,客戶端通過HTTP請求與WebSocket服務端協商升級協議。協議升級完成後,後續的資料交換則遵照WebSock
Socket 通訊中由 read 返回值造成的的死鎖問題(socket 阻塞)
詳細見原文。 示例 在第一章中,作者給出了一個 TCP Socket 通訊的例子——反饋伺服器,即伺服器端直接把從客戶端接收到的資料原原本本地反饋回去。 問題的引出 明確問題 客戶端與伺服器端在接收和傳送資料時,read()和write()方法不一
Linux系統網路程式設計中TCP通訊socket--send導致程序被關閉
send(sockfd, buff, sizeof(buff), 0); 在linuxTCP通訊中上述send形式是常用的設定方式,但是如果在TCP連結斷開後呼叫該send函式,多次即會導致程序結束。 解決方法 最簡單的方式就是 send(sockfd, buff, sizeo
SOCKET通訊中TCP、UDP資料包大小的確定
TCP、UDP資料包大小的確定 UDP和TCP協議利用埠號實現多項應用同時傳送和接收資料。資料通過源埠傳送出去,通過目標埠接收。有的網路應用只能使用預留或註冊的靜態埠;而另外一些網路應用則可以使用未被註冊的動態埠。因為UDP和TCP報頭使用兩個位元組存放埠號,所以埠號的有效範圍是從0到6
Socket通訊中AF_INET 和 AF_UNIX域的區別
轉載:http://blog.csdn.net/sandware/article/details/40923491 1. AF_INET域socket通訊過程 典型的TCP/IP四層模型的通訊過程。 傳送方、接收方依賴IP:Port來標識,即將本地的socket繫結到對應的IP埠上,傳送資料時
Unity中的Socket通訊(多個客戶端的非同步通訊)
using UnityEngine; using System; using System.Net; using System.Net.Sockets; using System.Collections; using System.Text; using System.Collections.Generic;
【轉】Unity3d中的Tcp socket通訊(開源)
轉自:https://blog.csdn.net/hiramtan/article/details/72621787HiSocket_unity如何使用可以從此連結下載最新的unity package: 功能Tcp socketUdp socket可伸縮位元組表高效能位元組塊
java中各種通訊的例項(socket、http等)
socket通訊的例項: 服務端: /** * <b>專案名:</b>複習總結<br/> * <b>包 名:</b>socket<br/> * <b>檔名:</b>Se