zoukankan      html  css  js  c++  java
  • QT中的SOCKET编程

    转自:http://mylovejsj.blog.163.com/blog/static/38673975200892010842865/

    QT中的SOCKET编程
    2008-10-07 23:13
    http://hi.baidu.com/landuochong/blog/item/08268100cf5b6783e950cda9.html

    操作系统:ARM-Linux
    QT版本:QT-2.3.2-FOR-LINUX
    GUI:Qtopia

    在LINUX 下进行网络编程,我们可以使用LINUX提供的统一的套接字接口。但是这种方法牵涉到太多的结构体,比如IP地址,端口转换等,不熟练的人往往容易犯这样 那样的错误。QT中提供的SOCKET完全使用了类的封装机制,使用户不需要接触底层的各种结构体操作。而且它采用QT本身的signal-slot机 制,使编写的程序更容易理解。

    QT中共提供四个与套按字相关的类,分别是:

    QServerSocket:TCP-based server
    QSocket:              Buffered TCP connection
    QSocketDevice: Platform-independent low-level socket API
    QSocketNotifier: Support for socket callbacks

    下面介绍使用QT进行网络编程,我们使用一个简单的C/S模式网络程序说明如何使用QT中的套接字。同时我们用TCP和UDP两种协议实现这个程序(该程序客户端与服务端各向对方发送一个字符口串“abc”)

    1、UDP实现
          UDP是不连接协议,没有客户端与服务端的概念。
         1)建立套接字相关对象
               QSocketDevice *MUReceiveSocket;   //套接字对象
                QSocketNotifier *MSocketNotifier;     //套接字监听对象
         2)初始化套接字相关对象
               MUReceiveSocket=new QSocketDevice(QSocketDevice::Datagram);   
               //UDP初始化
               QHostAddress MyAddress;
               QString FakeAddress;

               FakeAddress = get_eth1_ip(); //取得接口IP
               MyAddress.setAddress(FakeAddress);
               MUReceiveSocket->bind(MyAddress,Port);   
              //绑定到指定网络接口地址(IP),指定逻辑端口

               MSocketNotifier = new QSocketNotifier(MUReceiveSocket->socket(),QSocketNotifier::Read,0,"MSocketNotifier");
               //监听MUReceiveSocket套接字
         3)定义用实现响应slot
               virtual void OnMReceive();
               void Client::OnMReceive()
                {
                    int ByteCount,ReadCount;
                     char *IncommingChar;

                     fprintf(stderr,"Load a piece of Message! ");

                    ByteCount=MUReceiveSocket->bytesAvailable();
                    IncommingChar=(char *)malloc(ByteCount+1);
                     ReadCount=MUReceiveSocket->readBlock(IncommingChar,ByteCount);
                    IncommingChar[ByteCount]='';

                    fprintf(stderr,“%s“,IncommingChar); //打印接收的字符串
               }
         4)关联套接字的signal和接收slot
               connect(MSocketNotifier,SIGNAL(activated(int)),this,SLOT(OnMReceive()));
               //当MSocketNotifier检测到MUReceiveSocket活跃时调用OnMReceive
         5)发送字符串
               char information[20];
                strcpy(information,“abc“);
               MUReceiveSocket->writeBlock(information,length,MyAddress,2201);
    2、TCP实现 
           TCP的实现与UDP的实现大同小异,它是面象连接的协议。这里只介绍与UDP不同的地方。
          服务端:
          1)套接字对象的定义
                比UDP多定义一个套接字,一个用来监听端口,一个用来通信。
                QSocketDevice *ServerSocket;
                QSocketDevice *ClientSocket;
                QSocketNotifier *ClientNotifier;
                QSocketNotifier *ServerNotifier;
           2)套接字的初始化

                QHostAddress MyAddress;
                QString FakeAddress;

                FakeAddress = "127.0.0.1";
                MyAddress.setAddress(FakeAddress);
                UINT Port=1234;
                ServerSocket=new QSocketDevice(QSocketDevice::Stream);
                ClientSocket=new QSocketDevice(QSocketDevice::Stream);
                ServerSocket->bind(MyAddress,Port);
                ServerSocket->listen(20); //20代表所允许的最大连接数

                ClienttNotifier = new QSocketNotifier(ClientSocket->socket(),QSocketNotifier::Read,0,"ClientSocket");
                ServerNotifier = new QSocketNotifier(ServerSocket->socket(),QSocketNotifier::Read,0,"ServerSocket");
          3)响应连接(在定义slot中响应)
               当收到客户端的连接后,响应它,并以ClientSocket接收:
                ServerSocket->SetSocket(ClientSocket->socket());
           4)接收信息slot与UDP一致,这里不在叙述。
          客户端实现:
           客户端的实现与UDP实现大同小异,不同的地方只是客户端套接字不需要bind端口,因为连接上服 务端后TCP会保持这个连接,直到通信的结束。

    http://blog.csdn.net/leo115/article/details/7363416

  • 相关阅读:
    Linux中Elasticsearch集群部署
    在.net core 项目中添加swagger支持
    ASP.NET Core 类库中取读配置文件
    Redis的服务安装步骤
    Lambda表达式多表连接的左连
    Angular7 学习资料
    Docker在eShopOnContainer项目中的使用方法
    绘图工具
    万能分页存储过程(SQL Server)
    为什么程序开发人员这么累?
  • 原文地址:https://www.cnblogs.com/findumars/p/6039968.html
Copyright © 2011-2022 走看看