TcpClient.h
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <QWidget>
#include <QTcpSocket>
#include <QLineEdit>
class TcpClient : public QWidget
{
Q_OBJECT
public:
explicit TcpClient(QWidget *parent = 0);
QTcpSocket* _socket;
QLineEdit* _lineEdit;
signals:
public slots:
void slotButtonClick();
};
#endif // TCPCLIENT_H
TcpClient.cpp
#include "TcpClient.h"
#include <QHBoxLayout>
#include <QPushButton>
TcpClient::TcpClient(QWidget *parent) :
QWidget(parent)
{
_socket = new QTcpSocket(this);
_socket->connectToHost("127.0.0.1", 9988);
_lineEdit = new QLineEdit;
QHBoxLayout* lay = new QHBoxLayout(this);
lay->addWidget(_lineEdit);
QPushButton* button = new QPushButton("Send");
lay->addWidget(button);
connect(button, SIGNAL(clicked()), this, SLOT(slotButtonClick()));
connect(_lineEdit, SIGNAL(returnPressed()), this, SLOT(slotButtonClick()));
}
void TcpClient::slotButtonClick()
{
QString strText = _lineEdit->text();
if(strText.isEmpty())
return;
//net 中不能发QString 类型
_socket->write(strText.toUtf8());
_lineEdit->clear();
}
TcpServer.h
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QTextBrowser>
class TcpServer : public QWidget
{
Q_OBJECT
public:
explicit TcpServer(QWidget *parent = 0);
QTcpServer* _server;
QTcpSocket* _socket;
QTextBrowser* _show;
signals:
public slots:
void slotNetConnection();
void slotReadyRead();
};
#endif // TCPSERVER_H
TcpServer.cpp
#include "TcpServer.h"
#include <QHBoxLayout>
#include <QNetworkInterface>
#include <QMessageBox>
#include "ChooseInterface.h"
TcpServer::TcpServer(QWidget *parent) :
QWidget(parent)
{
// 创建服务器并监听
_server = new QTcpServer;
ChooseInterface dlg;
dlg.exec();
QMessageBox::information(NULL,"you select the ip:", dlg._strSelect);
// _server->listen(QHostAddress::Any,9988); 全部地址
_server->listen(QHostAddress(dlg._strSelect), 9988);
// 当有客户端来连接时,调用slotNetConnection方法
connect(_server, SIGNAL(newConnection()),
this, SLOT(slotNetConnection()));
_show = new QTextBrowser;
QHBoxLayout* lay = new QHBoxLayout(this);
lay->addWidget(_show);
}
void TcpServer::slotNetConnection()
{
// 判断是否有未处理的连接
while(_server->hasPendingConnections())
{
// 调用nextPeddingConnection去获得连接的socket
_socket = _server->nextPendingConnection();
_show->append("New connection ....");
// 为新的socket提供槽函数,来接收数据
connect(_socket, SIGNAL(readyRead()),
this, SLOT(slotReadyRead()));
}
}
void TcpServer::slotReadyRead()
{
// 接收数据,判断是否有数据,如果有,通过readAll函数获取所有数据
while(_socket->bytesAvailable() > 0)
{
_show->append("Data arrived ..... ");
QByteArray buf = _socket->readAll();
_show->append(buf);
}
}
//选择地址类
ChooseInterface.h
#ifndef CHOOSEINTERFACE_H
#define CHOOSEINTERFACE_H
#include <QDialog>
#include <QComboBox>
class ChooseInterface : public QDialog
{
Q_OBJECT
public:
explicit ChooseInterface(QWidget *parent = 0);
QComboBox* _comboBox;
QString _strSelect;
signals:
public slots:
void slotComboBoxChange(QString);
};
#endif // CHOOSEINTERFACE_H
ChooseInterface.cpp
#include "ChooseInterface.h"
#include <QNetworkInterface>
#include <QVBoxLayout>
ChooseInterface::ChooseInterface(QWidget *parent) :
QDialog(parent)
{
/* get all interface 从 <span style="font-family: Arial, Helvetica, sans-serif;">QNetworkInterface 中</span><span style="font-family: Arial, Helvetica, sans-serif;">*/</span>
QList<QHostAddress> addrList = QNetworkInterface::allAddresses();
#if 0
QList<QNetworkInterface> infList = QNetworkInterface::allInterfaces();
QList<QNetworkAddressEntry> entryList = infList.at(0).addressEntries();
entryList.at(0).broadcast()
#endif
_comboBox = new QComboBox;
QVBoxLayout* lay = new QVBoxLayout(this);
lay->addWidget(_comboBox);
foreach(QHostAddress addr, addrList)
{
quint32 ipaddr = addr.toIPv4Address(); //如果不toIPv4Address ,toString后有 IPv4,和 IPv6
if(ipaddr == 0)
continue;
_comboBox->addItem(QHostAddress(ipaddr).toString());
}
connect(_comboBox, SIGNAL(currentIndexChanged(QString)),
this, SLOT(slotComboBoxChange(QString)));
}
void ChooseInterface::slotComboBoxChange(QString str)
{
this->_strSelect = str;
}