(1)篇里面代码没完全贴出,有些同学说没看明白。今天又闲的蛋疼,无聊弄个服务端跟客户端整在一个程序上的例子。
代码跟一篇有点相同,但是又有点不同。先贴出效果图。。。
为了效果明显,我一个在linux 下,一个在xp下运行。
代码如下:
头文件
#ifndef NETWORKTCP_H #define NETWORKTCP_H #include <QMainWindow> #include <QtNetwork/QTcpServer> #include <QtNetwork/QTcpSocket> #include <map> #include <iostream> #include <QMessageBox> using namespace std; namespace Ui { class NetworkTcp; } class NetworkTcp : public QMainWindow { Q_OBJECT public: explicit NetworkTcp(QWidget *parent = 0); ~NetworkTcp(); QTcpServer *m_pTcpServer; QTcpSocket *m_pTcpSocket; map<int,QTcpSocket*> map_TcpSocket; QTcpSocket *pTcpSocketServer; QTcpSocket *pTcpSocketClient; bool isServer; private: Ui::NetworkTcp *ui; private slots: void slotStartServer(); void slotConnectServer(); void slotSendMesg(); void slotNewConnect(); void slotReadMesg(); }; #endif // NETWORKTCP_H
源文件
#include "NetworkTcp.h" #include "ui_networktcp.h" NetworkTcp::NetworkTcp(QWidget *parent) : QMainWindow(parent), ui(new Ui::NetworkTcp) { ui->setupUi(this); isServer= true; //默认服务端 this->connect(ui->pushButton_StartServer,SIGNAL(clicked()),this,SLOT(slotStartServer())); this->connect(ui->pushButton_ConnectServer,SIGNAL(clicked()),this,SLOT(slotConnectServer())); this->connect(ui->pushButton_Send,SIGNAL(clicked()),this,SLOT(slotSendMesg())); } NetworkTcp::~NetworkTcp() { delete ui; } void NetworkTcp::slotStartServer() //启动服务器 { isServer = true; ui->pushButton_ConnectServer->setEnabled(false); ui->lineEdit_IP->setEnabled(false); ui->lineEdit_NetPort->setEnabled(false); m_pTcpServer = new QTcpServer(); m_pTcpServer->listen(QHostAddress::Any,19999); this->connect(m_pTcpServer,SIGNAL(newConnection()),this,SLOT(slotNewConnect())); } void NetworkTcp::slotNewConnect() { // QTcpSocket *pTcpSocket; pTcpSocketServer = m_pTcpServer->nextPendingConnection(); // pTcpSocketTemp = pTcpSocket; // int sockfd = pTcpSocketTemp->socketDescriptor(); // map_TcpSocket.insert(make_pair(sockfd,pTcpSocket)); this->connect(pTcpSocketServer,SIGNAL(readyRead()),this,SLOT(slotReadMesg())); } void NetworkTcp::slotReadMesg() //读取信息 { // int sockfd = pTcpSocketTemp->socketDescriptor(); if(isServer == true) { QByteArray qba= pTcpSocketServer->readAll(); //读取 QString ss=QVariant(qba).toString(); QHostAddress ip = pTcpSocketServer->peerAddress(); QString qStrSendInfo = ip.toString()+" : "+ss; ui->textEdit_Info->append(qStrSendInfo); } else if(isServer == false) { QByteArray qba= pTcpSocketClient->readAll(); //读取 QString ss=QVariant(qba).toString(); QHostAddress ip = pTcpSocketClient->peerAddress(); QString qStrSendInfo = ip.toString()+" : "+ss; ui->textEdit_Info->append(qStrSendInfo); } } void NetworkTcp::slotConnectServer() //连接服务器 { if(ui->lineEdit_IP->text().isEmpty() ||ui->lineEdit_NetPort->text().isEmpty()) { QMessageBox::warning(this,tr("Warning"),tr("Please input IP OR NetPort!")); return; } isServer = false; ui->pushButton_StartServer->setEnabled(false); pTcpSocketClient = new QTcpSocket(this); pTcpSocketClient->abort(); QString qStrIP = ui->lineEdit_IP->text(); quint16 netport = ui->lineEdit_NetPort->text().toInt(); pTcpSocketClient->connectToHost(qStrIP,netport); connect(pTcpSocketClient,SIGNAL(readyRead()),this,SLOT(slotReadMesg())); } void NetworkTcp::slotSendMesg() //发送消息 { QString strMesg= ui->lineEdit_sendmsg->text(); ui->lineEdit_sendmsg->clear(); if(isServer == true) { pTcpSocketServer->write(strMesg.toStdString().c_str(),strlen(strMesg.toStdString().c_str())); } else if(isServer == false) { pTcpSocketClient->write(strMesg.toStdString().c_str(),strlen(strMesg.toStdString().c_str())); } }
我一直想知道QQ发信息是怎么弄的,到现在还是没想明白里面的流程。。。唉。。。
希望懂的人一起交流交流。。。