zoukankan      html  css  js  c++  java
  • Socket之TCP-IP

    • 通常的TCP/IP流程如下:

      TCP/IP的通讯更像是打电话,连接上通了确认是自己拨叫的用户之后才能进行正常通话,更加安全合理。

     

    • Qt中的TCP/IP流程如下:

      Qt中流程和普通的思路一样,只是封装成了自己的函数,使用更加方便

     

    • 实现的效果如下

     

    • 参考代码如下:

      服务器.h文件:

     1 #ifndef WIDGET_H
     2 #define WIDGET_H
     3 
     4 #include <QWidget>
     5 #include <QTcpServer>
     6 #include <QTcpSocket>
     7 #include <QString>
     8 namespace Ui {
     9 class Widget;
    10 }
    11 
    12 class Widget : public QWidget
    13 {
    14     Q_OBJECT
    15 
    16 public:
    17     explicit Widget(QWidget *parent = 0);
    18     ~Widget();
    19 
    20 private slots:
    21     void on_send_clicked();
    22     void communication();
    23     void on_disconnect_clicked();
    24     void showtext();
    25 private:
    26     Ui::Widget *ui;
    27     QTcpServer *TcpServer;
    28     QTcpSocket *TcpSocket;
    29 };
    30 
    31 #endif // WIDGET_H
    View Code

      服务器.cpp文件:

     1 #include "widget.h"
     2 #include "ui_widget.h"
     3 
     4 Widget::Widget(QWidget *parent) :
     5     QWidget(parent),
     6     ui(new Ui::Widget)
     7 {
     8     ui->setupUi(this);
     9     TcpServer = new QTcpServer(this);
    10     TcpSocket = NULL;
    11     setWindowTitle("服务器:6666");
    12     TcpServer->listen(QHostAddress::Any,6666);
    13      connect(TcpServer,&QTcpServer::newConnection,this,&Widget::communication);
    14 
    15 
    16 }
    17 void Widget::communication()
    18 {
    19     TcpSocket=TcpServer->nextPendingConnection();
    20     QString ip = TcpSocket->peerAddress().toString();
    21     ui->textEdit->setText(ip);
    22     connect(TcpSocket,&QTcpSocket::readyRead,this,&Widget::showtext);
    23 
    24 }
    25 void Widget::showtext()
    26 {
    27     QByteArray array = TcpSocket->readAll();
    28     ui->textEdit->append(array);
    29 }
    30 Widget::~Widget()
    31 {
    32     delete ui;
    33 }
    34 
    35 void Widget::on_send_clicked()
    36 {
    37     if(TcpSocket == NULL)
    38        {
    39         return;
    40     }
    41     QString text = ui->textEdit_2->toPlainText();
    42     TcpSocket->write(text.toUtf8().data());
    43 
    44 }
    45 
    46 void Widget::on_disconnect_clicked()
    47 {
    48     if(TcpSocket == NULL)
    49        {
    50         return;
    51     }
    52     TcpSocket->disconnectFromHost();
    53     TcpSocket->close();
    54     TcpSocket = NULL;
    55 
    56 }
    View Code

      客户端.h文件:

     1 #ifndef SOCKETWIDGET_H
     2 #define SOCKETWIDGET_H
     3 
     4 #include <QWidget>
     5 #include <QTcpSocket>
     6 #include <Qstring>
     7 #include <QHostAddress>
     8 namespace Ui {
     9 class SocketWidget;
    10 }
    11 
    12 class SocketWidget : public QWidget
    13 {
    14     Q_OBJECT
    15 
    16 public:
    17     explicit SocketWidget(QWidget *parent = 0);
    18     ~SocketWidget();
    19 public slots:
    20     void dealdata();
    21     void dealread();
    22 private slots:
    23     void on_send_clicked();
    24 
    25     void on_disconnect_clicked();
    26 
    27     void on_connect_clicked();
    28 
    29 private:
    30     Ui::SocketWidget *ui;
    31     QTcpSocket *TcpSocket;
    32 };
    33 
    34 #endif // SOCKETWIDGET_H
    View Code

      客户端.cpp文件:

     1 #include "socketwidget.h"
     2 #include "ui_socketwidget.h"
     3 
     4 SocketWidget::SocketWidget(QWidget *parent) :
     5     QWidget(parent),
     6     ui(new Ui::SocketWidget)
     7 {
     8     ui->setupUi(this);
     9     TcpSocket = new QTcpSocket(this);
    10     setWindowTitle("客户端");
    11     connect(TcpSocket,&QTcpSocket::connected,this,&SocketWidget::dealdata);
    12     connect(TcpSocket,&QTcpSocket::readyRead,this,&SocketWidget::dealread);
    13 }
    14 void SocketWidget::dealdata()
    15 {
    16     QString str= "已连接服务器";
    17     ui->read->setText(str);
    18 }
    19 SocketWidget::~SocketWidget()
    20 {
    21     delete ui;
    22 }
    23 void SocketWidget::dealread()
    24 {
    25     QByteArray array = TcpSocket->readAll();
    26     ui->read->append(array);
    27 }
    28 void SocketWidget::on_send_clicked()
    29 {
    30     QString sendtext = ui->write->toPlainText();
    31     TcpSocket->write(sendtext.toUtf8().data());
    32 //发送数据
    33 }
    34 
    35 void SocketWidget::on_disconnect_clicked()
    36 {
    37     TcpSocket->disconnectFromHost();
    38     TcpSocket->close();
    39 }
    40 
    41 void SocketWidget::on_connect_clicked()
    42 {
    43     quint16  port = ui->port->toPlainText().toInt();
    44     QString ip =  ui->ip->toPlainText();
    45     TcpSocket->connectToHost(QHostAddress(ip),port);
    46 //建立主动连接
    47 }
    View Code
  • 相关阅读:
    uva 11294 Wedding
    uvalive 4452 The Ministers’ Major Mess
    uvalive 3211 Now Or Later
    uvalive 3713 Astronauts
    uvalive 4288 Cat Vs. Dog
    uvalive 3276 The Great Wall Game
    uva 1411 Ants
    uva 11383 Golden Tiger Claw
    uva 11419 SAM I AM
    uvalive 3415 Guardian Of Decency
  • 原文地址:https://www.cnblogs.com/fuzhuoxin/p/12165591.html
Copyright © 2011-2022 走看看