//服务器实例
class LittleServer :
public QMainWindow
{
Q_OBJECT
public:
LittleServer(void);
virtual ~LittleServer(void);
public:
QUdpSocket *UdpSender;//udp套接字器
QTcpSocket *TcpReciever;//tcp套接字器
QTcpServer* m_TcpServer;//tcp服务器
QWidget* m_CentralWidget;//中心窗体
QByteArray datagram;//数据
QLabel* m_Label;
QLineEdit* m_LineEdit;
QPushButton* m_SendButton;
public slots:
void slot_Send();
void acceptConnection();
public:
void readClient();
};
//cpp实现
LittleServer::LittleServer(void)
{
m_TcpServer=new QTcpServer(this);//创建服务器实例
TcpReciever=nullptr;//赞无接收套接字
m_TcpServer->listen(QHostAddress::Any, 8888);//监听端口号为8888,这里自行设计
connect(m_TcpServer, SIGNAL(newConnection()), this, SLOT(acceptConnection()));//当服务器发现有新的连接时,出发接受新连接函数
m_Label=new QLabel("",this);
m_CentralWidget=new QWidget(this);
QHBoxLayout* mainLayout=new QHBoxLayout(this);
m_SendButton=new QPushButton("发送",this);
m_LineEdit=new QLineEdit(this);
mainLayout->addStretch();
mainLayout->addWidget(m_SendButton);
mainLayout->addWidget(m_LineEdit);
mainLayout->addStretch();
mainLayout->addWidget(m_Label);
this->setCentralWidget(m_CentralWidget);
m_CentralWidget->setLayout(mainLayout);
UdpSender = new QUdpSocket(this);//暂时没使用
connect(m_SendButton,SIGNAL(clicked()),this,SLOT(slot_Send()));
}
LittleServer::~LittleServer(void)
{
}
//当点击的时候发送
void LittleServer::slot_Send()
{
datagram=m_LineEdit->text().toAscii();//得到显示的数据
//UDP广播
UdpSender->writeDatagram(datagram.data(),datagram.size(),QHostAddress::Broadcast,6665);
//向特定IP发送
QHostAddress serverAddress = QHostAddress("192.168.20.156");
int Ret=UdpSender->writeDatagram(datagram.data(), datagram.size(),QHostAddress::LocalHost, 6665);
/* writeDatagram函数原型,发送成功返回字节数,否则-1
qint64 writeDatagram(const char *data,qint64 size,const QHostAddress &address,quint16 port)
qint64 writeDatagram(const QByteArray &datagram,const QHostAddress &host,quint16 port)
*/
}
void LittleServer::acceptConnection()
{
//当有客户端连接到服务器的时候:就得到当前的socket,然后有数据就读取
//qDebug(State:%d\n”,mSocket->state()); // State: 2; 2代表ConnectingState,正确的应该为3(ConnectedState)
TcpReciever =m_TcpServer->nextPendingConnection();//套接字器指向该链接
connect(TcpReciever, SIGNAL(readyRead()), this, SLOT(readClient()));//当有数据来时,触发读取客户端的信息
QString ipCli =TcpReciever->peerAddress().toString();//链接地址
qint16 portCli = TcpReciever->peerPort();//链接方端口
QString temp = QString("客户端:[%1:%2]:连接成功").arg(ipCli).arg(portCli);
ipCli=TcpReciever->localAddress().toString();
portCli=TcpReciever->localPort();
temp += QString("服务器:[%1:%2]").arg(ipCli).arg(portCli);
m_Label->setText(temp);//显示服务器客户端分别地址和ip
}
void LittleServer::readClient()
{
//将读取的客户端信息显出来
TcpReciever->waitForReadyRead();
QString str = TcpReciever->readAll();
m_Label->setText(str);
//或者
//char buf[1024];
//m_TcpServer->read(buf,1024);
}
class LittleClient :
public QMainWindow
{
Q_OBJECT
public:
LittleClient(void);
virtual ~LittleClient(void);
QTcpSocket* m_TcpSocket;
QUdpSocket *UdpReceiver;//udp
QWidget* m_CentralWidget;//中心窗体
QByteArray datagram;//接受到的数据
QLabel* m_Label;
QPushButton* m_ShowButton;
//信号槽
private slots:
void readPendingDatagrams();
void sendTcp();
void ConnectToServerSuccessed();
};
LittleClient::LittleClient(void)
:UdpReceiver(new QUdpSocket(this))
{
UdpReceiver->bind(QHostAddress::LocalHost, 6665);//绑定端口
connect(UdpReceiver, SIGNAL(readyRead()),this, SLOT(readPendingDatagrams()));
m_CentralWidget=new QWidget(this);
QHBoxLayout* mainLayout=new QHBoxLayout(this);
m_ShowButton=new QPushButton("显示",this);
m_Label=new QLabel("开始",this);
mainLayout->addStretch();
mainLayout->addWidget(m_ShowButton);
mainLayout->addWidget(m_Label);
mainLayout->addStretch();
this->setCentralWidget(m_CentralWidget);
m_CentralWidget->setLayout(mainLayout);
m_TcpSocket=new QTcpSocket(this);
//m_TcpSocket->abort();
connect(m_TcpSocket,SIGNAL(connected()),this,SLOT(ConnectToServerSuccessed()));
m_TcpSocket->connectToHost(QHostAddress::LocalHost,8888);//链接服务器
bool isConnected=m_TcpSocket->waitForConnected();//只有使用waitForConnected()后,QTcpSocket才真正尝试连接服务器,并返回是否连接的结果。
//m_TcpSocket->connectToHost("192.168.20.156", 9999);//先建立链接
connect(m_ShowButton,SIGNAL(clicked()),this,SLOT(sendTcp()));
}
LittleClient::~LittleClient(void)
{
}
void LittleClient::readPendingDatagrams()
{
while (UdpReceiver->hasPendingDatagrams()) {
datagram.resize(UdpReceiver->pendingDatagramSize());
UdpReceiver->readDatagram(datagram.data(), datagram.size());
m_Label->setText(QString(datagram));
//数据接收在datagram里
/* readDatagram 函数原型
qint64 readDatagram(char *data,qint64 maxSize,QHostAddress *address=0,quint16 *port=0)
*/
}
}
void LittleClient::sendTcp()
{
QString str=m_Label->text();
//m_TcpSocket->write(str.toAscii());
m_TcpSocket->write("12345",5);
//m_TcpSocket->write(str.toStdString().c_str(), strlen(str.toStdString().c_str()));
bool isSend=m_TcpSocket->waitForBytesWritten(); //当使用waitForBytesWritten()后,QTcpSocket才真正发送数据。
}
void LittleClient::ConnectToServerSuccessed()
{
m_Label->setText(QString("服务器:[%1:%2 客户端:%3:%4]").arg(m_TcpSocket->peerAddress().toString()).arg(m_TcpSocket->peerPort()).arg(m_TcpSocket->localAddress().toString()).arg(m_TcpSocket->localPort()));
}
然后分别创建该littleclient实例和 littleserver实例,即可进行通讯(端口和Ip依据实际情况填写),其中tcp收发信息需要客户端和服务端先建立链接,才行,而udp发送消息则不需要理会,只管发,不管对方是否接受或者处理。