5 关于QT中的网络编程,QTcpSocket,QUdpSocket

               


1新建一个项目:TCPServer.pro

修改TCPServer.pro,注意:如果是想使用网络库,需要加上network

SOURCES+=\

   TcpServer.cpp\

   TcpClient.cpp

 

HEADERS+=\

   TcpServer.h\

   TcpClient.h

 

QT+=gui widgets network

 

CONFIG+=C++11

B新建如下文件,因为要用到网络库,所以加上network

C编写IP选择下拉选,头文件ChooseInterface.h

#ifndefCHOOSEINTERFACE_H

#defineCHOOSEINTERFACE_H

 

#include<QDialog>

#include<QComboBox>

 

classChooseInterface:publicQDialog

{

   Q_OBJECT

public:

   explicitChooseInterface(QWidget*parent= 0);

   QComboBox*_comboBox;

   QString_strSelect;

 

signals:

 

publicslots:

   voidslotComboBoxChange(QString);

};

 

#endif//CHOOSEINTERFACE_H

编写ChooseInterface.cpp

#include"ChooseInterface.h"

#include<QNetworkInterface>

#include<QVBoxLayout>

 

ChooseInterface::ChooseInterface(QWidget*parent):

   QDialog(parent)

{

   /*getallinterface*/

   QList<QHostAddress>addrList=QNetworkInterface::allAddresses();

#if0

   QList<QNetworkInterface>infList =QNetworkInterface::allInterfaces();

 

   QList<QNetworkAddressEntry>entryList =infList.at(0).addressEntries();

   entryList.at(0).broadcast()

#endif

 

   //编写一个下拉选

   _comboBox=newQComboBox;

   QVBoxLayout*lay=newQVBoxLayout(this);

   lay->addWidget(_comboBox);

 

   foreach(QHostAddressaddr,addrList)

   {

       //将地址都转换成为ipv4的地址

       quint32ipaddr=addr.toIPv4Address();

       //去掉0ip

       if(ipaddr==0)

           continue;

       //comboBox中添加下拉选

       _comboBox->addItem(QHostAddress(ipaddr).toString());

   }

 

   //当下拉选发生变化时的操作

   connect(_comboBox,SIGNAL(currentIndexChanged(QString)),

           this,SLOT(slotComboBoxChange(QString)));

}

 

voidChooseInterface::slotComboBoxChange(QStringstr)

{

   this->_strSelect=str;

}

上面的运行结果是:

编写TcpServer.h

#ifndefTCPSERVER_H

#defineTCPSERVER_H

 

#include<QWidget>

#include<QTcpServer>

#include<QTcpSocket>

#include<QTextBrowser>

 

classTcpServer:publicQWidget

{

   Q_OBJECT

public:

   explicitTcpServer(QWidget*parent= 0);

 

   QTcpServer*_server;

   QTcpSocket*_socket;

 

   QTextBrowser*_show;

 

signals:

 

publicslots:

   voidslotNetConnection();

   voidslotReadyRead();

};

 

#endif//TCPSERVER_H

编写TcpServer.cpp

#include"TcpServer.h"

#include<QHBoxLayout>

#include<QNetworkInterface>

#include<QMessageBox>

#include"ChooseInterface.h"

 

TcpServer::TcpServer(QWidget*parent):

   QWidget(parent)

{

   //创建服务器并监听

   _server=newQTcpServer;

 

   ChooseInterfacedlg;

   dlg.exec();

 

   //消息提示框

   QMessageBox::information(NULL,"youselecttheip:",dlg._strSelect);

 

   _server->listen(QHostAddress(dlg._strSelect),9988);

 

   //当有客户端来连接时,调用slotNetConnection方法

   connect(_server,SIGNAL(newConnection()),

           this,SLOT(slotNetConnection()));

 

   _show=newQTextBrowser;

   QHBoxLayout*lay=newQHBoxLayout(this);

   lay->addWidget(_show);

}

 

voidTcpServer::slotNetConnection()

{

   //判断是否有未处理的连接

   while(_server->hasPendingConnections())

   {

       //调用nextPeddingConnection去获得连接的socket

       _socket=_server->nextPendingConnection();

 

       _show->append("Newconnection....");

 

       //为新的socket提供槽函数,来接收数据

       connect(_socket,SIGNAL(readyRead()),

               this,SLOT(slotReadyRead()));

   }

}

 

voidTcpServer::slotReadyRead()

{

   //接收数据,判断是否有数据,如果有,通过readAll函数获取所有数据

   while(_socket->bytesAvailable()>0)

   {

       _show->append("Dataarrived.....");

       QByteArraybuf=_socket->readAll();

       _show->append(buf);

   }

}

编写TcpClient.h

#ifndefTCPCLIENT_H

#defineTCPCLIENT_H

 

#include<QWidget>

#include<QTcpSocket>

#include<QLineEdit>

 

classTcpClient:publicQWidget

{

   Q_OBJECT

public:

   explicitTcpClient(QWidget*parent= 0);

   QTcpSocket*_socket;

   QLineEdit*_lineEdit;

 

signals:

 

publicslots:

   voidslotButtonClick();

};

 

#endif//TCPCLIENT_H

编写TcpClient.cpp

#include"TcpClient.h"

#include<QHBoxLayout>

#include<QPushButton>

 

TcpClient::TcpClient(QWidget*parent):

   QWidget(parent)

{

   _socket=newQTcpSocket(this);

   _socket->connectToHost("127.0.0.1",9988);

 

   _lineEdit=newQLineEdit;

   QHBoxLayout*lay=newQHBoxLayout(this

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值