Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3434047
  • 博文数量: 864
  • 博客积分: 14125
  • 博客等级: 上将
  • 技术积分: 10634
  • 用 户 组: 普通用户
  • 注册时间: 2007-07-27 16:53
个人简介

https://github.com/zytc2009/BigTeam_learning

文章分类

全部博文(864)

文章存档

2023年(1)

2021年(1)

2019年(3)

2018年(1)

2017年(10)

2015年(3)

2014年(8)

2013年(3)

2012年(69)

2011年(103)

2010年(357)

2009年(283)

2008年(22)

分类: C/C++

2008-06-23 13:26:08

QT中的SOCKET编程

操作系统: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!\n");

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

                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会保持这个连接,直到通信的结束。

阅读(1665) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~