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

首先给大家分享一个巨牛巨牛的人工智能教程,是我无意中发现的。教程不仅零基础,通俗易懂,而且非常风趣幽默,还时不时有内涵段子,像看小说一样,哈哈~我正在学习中,觉得太牛了,所以分享给大家!点这里可以跳转到教程

               


1新建一个项目:TCPServer.pro

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

SOURCES+=\

   TcpServer.cpp\

   TcpClient.cpp

 

HEADERS+=\

   TcpServer.h\

   TcpClient.h

 

QT+=gui widgets network

 

CONFIG+=C++11

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

5 关于QT中的网络编程,QTcpSocket,QUdpSocket_第1张图片

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

#ifndefCHOOSEINTERFACE_H

#defineCHOOSEINTERFACE_H

 

#include

#include

 

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

#include

 

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;

}

上面的运行结果是:

5 关于QT中的网络编程,QTcpSocket,QUdpSocket_第2张图片

编写TcpServer.h

#ifndefTCPSERVER_H

#defineTCPSERVER_H

 

#include

#include

#include

#include

 

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

#include

#include

#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

#include

#include

 

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

#include

 

TcpClient::TcpClient(QWidget*parent):

   QWidget(parent)

{

   _socket=newQTcpSocket(this);

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

 

   _lineEdit=newQLineEdit;

   QHBoxLayout*lay=newQHBoxLayout(this

你可能感兴趣的:(5 关于QT中的网络编程,QTcpSocket,QUdpSocket)