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
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(); //去掉0的ip 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 |