欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

QT網(wǎng)絡(luò)編程Tcp下C/S架構(gòu)如何實現(xiàn)即時通信

這篇文章主要介紹QT網(wǎng)絡(luò)編程Tcp下C/S架構(gòu)如何實現(xiàn)即時通信,文中介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們一定要看完!

目前創(chuàng)新互聯(lián)已為上1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機、網(wǎng)站運營、企業(yè)網(wǎng)站設(shè)計、禹會網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

先寫一個客戶端,實現(xiàn)簡單的,能加入聊天,以及加入服務(wù)器的界面。

#ifndef TCPCLIENT_H
#define TCPCLIENT_H
 
#include <QDialog>
#include <QListWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QGridLayout>
#include <QtNetWork/QHostAddress>
#include <QtNetWork/QTcpSocket>
 
class TcpClient : public QDialog
{
 Q_OBJECT
 
public:
 TcpClient(QWidget *parent = 0,Qt::WindowFlags f=0);
 ~TcpClient();
private:
 QListWidget *contentListWidget;
 QLineEdit *sendLineEdit;
 QPushButton *sendBtn;
 QLabel *userNameLabel;
 QLineEdit *userNameLineEdit;
 QLabel *serverIPLabel;
 QLineEdit *serverIPLineEdit;
 QLabel *portLabel;
 QLineEdit *portLineEdit;
 QPushButton *enterBtn;
 QGridLayout *mainLayout;
 bool status;
 int port;
 QHostAddress *serverIP;
 QString userName;
 QTcpSocket *tcpSocket;
public slots:
 void slotEnter();
 void slotConnected();
 void slotDisconnected();
 void dataReceived();
 void slotSend();
};
 
#endif // TCPCLIENT_H

有一個加入服務(wù)器的按鈕,還有一個發(fā)送消息的按鈕,在頭文件,先定義兩個函數(shù)。

#include "tcpclient.h"
#include <QMessageBox>
#include <QHostInfo>
 
TcpClient::TcpClient(QWidget *parent,Qt::WindowFlags f)
 : QDialog(parent,f)
{
 setWindowTitle(tr("TCP Client"));
 
 contentListWidget = new QListWidget;
 
 sendLineEdit = new QLineEdit;
 sendBtn = new QPushButton(tr("send"));
 
 userNameLabel = new QLabel(tr("name"));
 userNameLineEdit = new QLineEdit;
 
 serverIPLabel = new QLabel(tr("server"));
 serverIPLineEdit = new QLineEdit;
 
 portLabel = new QLabel(tr("port"));
 portLineEdit = new QLineEdit;
 
 enterBtn= new QPushButton(tr("join chat"));
 
 mainLayout = new QGridLayout(this);
 mainLayout->addWidget(contentListWidget,0,0,1,2);
 mainLayout->addWidget(sendLineEdit,1,0);
 mainLayout->addWidget(sendBtn,1,1);
 mainLayout->addWidget(userNameLabel,2,0);
 mainLayout->addWidget(userNameLineEdit,2,1);
 mainLayout->addWidget(serverIPLabel,3,0);
 mainLayout->addWidget(serverIPLineEdit,3,1);
 mainLayout->addWidget(portLabel,4,0);
 mainLayout->addWidget(portLineEdit,4,1);
 mainLayout->addWidget(enterBtn,5,0,1,2);
 
 status = false;
 
 port = 8010;
 portLineEdit->setText(QString::number(port));
 
 serverIP =new QHostAddress();
 
 connect(enterBtn,SIGNAL(clicked()),this,SLOT(slotEnter()));
 connect(sendBtn,SIGNAL(clicked()),this,SLOT(slotSend()));
 
 sendBtn->setEnabled(false);
}
 
TcpClient::~TcpClient()
{
 
}
 
void TcpClient::slotEnter()
{
 if(!status)
 {
  QString ip = serverIPLineEdit->text();
  if(!serverIP->setAddress(ip))
  {
   QMessageBox::information(this,tr("error"),tr("server ip address error!"));
   return;
  }
 
  if(userNameLineEdit->text()=="")
  {
   QMessageBox::information(this,tr("error"),tr("User name error!"));
   return;
  }
 
  userName=userNameLineEdit->text();
 
  tcpSocket = new QTcpSocket(this);
  connect(tcpSocket,SIGNAL(connected()),this,SLOT(slotConnected()));
  connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(slotDisconnected()));
  connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived()));
 
  tcpSocket->connectToHost(*serverIP,port);
 
  status=true;
 }
 else
 {
  int length=0;
  QString msg=userName+tr(":Leave Chat Room");
  if((length=tcpSocket->write(msg.toLatin1(),msg.length()))!=msg. length())
  {
   return;
  }
 
  tcpSocket->disconnectFromHost();
 
  status=false;
 }
}
 
void TcpClient::slotConnected()
{
 sendBtn->setEnabled(true);
 enterBtn->setText(tr("離開"));
 
 int length=0;
 QString msg=userName+tr(":Enter Chat Room");
 if((length=tcpSocket->write(msg.toLatin1(),msg.length()))!=msg.length())
 {
  return;
 }
}
 
void TcpClient::slotSend()
{
 if(sendLineEdit->text()=="")
 {
  return ;
 }
 
 QString msg=userName+":"+sendLineEdit->text();
 
 tcpSocket->write(msg.toLatin1(),msg.length());
 sendLineEdit->clear();
}
 
void TcpClient::slotDisconnected()
{
 sendBtn->setEnabled(false);
 enterBtn->setText(tr("join chat"));
}
 
void TcpClient::dataReceived()
{
 while(tcpSocket->bytesAvailable()>0)
 {
  QByteArray datagram;
  datagram.resize(tcpSocket->bytesAvailable());
 
  tcpSocket->read(datagram.data(),datagram.size());
 
  QString msg=datagram.data();
  contentListWidget->addItem(msg.left(datagram.size()));
 }
}

實現(xiàn)界面布局。在Enter槽函數(shù)中,確定加入還是離開的服務(wù)器的功能。如果加入了,就將消息,寫到tcpsocket中,構(gòu)造消。

服務(wù)端的頭文件:

#ifndef TCPSERVER_H
#define TCPSERVER_H
 
#include <QDialog>
#include <QListWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
#include "server.h"
 
class TcpServer : public QDialog
{
 Q_OBJECT
 
public:
 TcpServer(QWidget *parent = 0,Qt::WindowFlags f=0);
 ~TcpServer();
private:
 QListWidget *ContentListWidget;
 QLabel *PortLabel;
 QLineEdit *PortLineEdit;
 QPushButton *CreateBtn;
 QGridLayout *mainLayout;
 int port;
 Server *server;
public slots:
 void slotCreateServer();
 void updateServer(QString,int);
};
 
#endif // TCPSERVER_H

這是服務(wù)端的界面的,把消息顯示而已。實現(xiàn)這個布局。

#include "tcpserver.h"
 
TcpServer::TcpServer(QWidget *parent,Qt::WindowFlags f)
 : QDialog(parent,f)
{
 setWindowTitle(tr("TCP Server"));
 
 ContentListWidget = new QListWidget;
 
 PortLabel = new QLabel(tr(" port"));
 PortLineEdit = new QLineEdit;
 
 CreateBtn = new QPushButton(tr("create chat"));
 mainLayout = new QGridLayout(this);
 mainLayout->addWidget(ContentListWidget,0,0,1,2);
 mainLayout->addWidget(PortLabel,1,0);
 mainLayout->addWidget(PortLineEdit,1,1);
 mainLayout->addWidget(CreateBtn,2,0,1,2);
 
 port=8010;
 PortLineEdit->setText(QString::number(port));
 
 connect(CreateBtn,SIGNAL(clicked()),this,SLOT(slotCreateServer()));
}
 
TcpServer::~TcpServer()
{
 
}
 
void TcpServer::slotCreateServer()
{
 server = new Server(this,port);
 connect(server,SIGNAL(updateServer(QString,int)),this,SLOT(updateServer(QString,int)));
 
 CreateBtn->setEnabled(false);
}
 
void TcpServer::updateServer(QString msg,int length)
{
 ContentListWidget->addItem(msg.left(length));
}

創(chuàng)建TCP的套接字,以便實現(xiàn)服務(wù)端和客戶端的通信。

#ifndef TCPCLIENTSOCKET_H
#define TCPCLIENTSOCKET_H
 
#include <QtNetWork/QTcpSocket>
#include <QObject>
 
class TcpClientSocket : public QTcpSocket
{
 Q_OBJECT
public:
 TcpClientSocket();
signals:
 void updateClients(QString,int);
 void disconnected(int);
protected slots:
 void dataReceived();
 void slotDisconnected();
};
 
#endif // TCPCLIENTSOCKET_H

以上是頭文件,具體的是:

#include "tcpclientsocket.h"
 
TcpClientSocket::TcpClientSocket()
{
 connect(this,SIGNAL(readyRead()),this,SLOT(dataReceived()));
 connect(this,SIGNAL(disconnected()),this,SLOT(slotDisconnected()));
}
 
void TcpClientSocket::dataReceived()
{
 while(bytesAvailable()>0)
 {
  int length = bytesAvailable();
  char buf[1024];
  read(buf,length);
 
  QString msg=buf;
  emit updateClients(msg,length);
 }
}
 
void TcpClientSocket::slotDisconnected()
{
 emit disconnected(this->socketDescriptor());
}

實現(xiàn)服務(wù)器,頭文件:

#ifndef SERVER_H
#define SERVER_H
 
#include <QtNetWork/QTcpServer>
#include <QObject>
#include "tcpclientsocket.h"
 
class Server : public QTcpServer
{
 Q_OBJECT
public:
 Server(QObject *parent=0,int port=0);
 QList<TcpClientSocket*> tcpClientSocketList;
signals:
 void updateServer(QString,int);
public slots:
 void updateClients(QString,int);
 void slotDisconnected(int);
protected:
 void incomingConnection(int socketDescriptor);
};
 
#endif // SERVER_H
#include "server.h"
 
Server::Server(QObject *parent,int port)
 :QTcpServer(parent)
{
 listen(QHostAddress::Any,port);
}
 
void Server::incomingConnection(int socketDescriptor)
{
 TcpClientSocket *tcpClientSocket = new TcpClientSocket;
 connect(tcpClientSocket,SIGNAL(updateClients(QString,int)),this,SLOT(updateClients(QString,int)));
 connect(tcpClientSocket,SIGNAL(disconnected(int)),this,SLOT(slotDisconnected(int)));
 
 tcpClientSocket->setSocketDescriptor(socketDescriptor);
 
 tcpClientSocketList.append(tcpClientSocket);
}
 
void Server::updateClients(QString msg,int length)
{
 emit updateServer(msg,length);
 for(int i=0;i<tcpClientSocketList.count();i++)
 {
  QTcpSocket *item = tcpClientSocketList.at(i);
  if(item->write(msg.toLatin1(),length)!=length)
  {
   continue;
  }
 }
}
 
void Server::slotDisconnected(int descriptor)
{
 for(int i=0;i<tcpClientSocketList.count();i++)
 {
  QTcpSocket *item = tcpClientSocketList.at(i);
  if(item->socketDescriptor()==descriptor)
  {
   tcpClientSocketList.removeAt(i);
   return;
  }
 }
 return;
}

實現(xiàn)后的界面:

QT網(wǎng)絡(luò)編程Tcp下C/S架構(gòu)如何實現(xiàn)即時通信

以上是“QT網(wǎng)絡(luò)編程Tcp下C/S架構(gòu)如何實現(xiàn)即時通信”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文題目:QT網(wǎng)絡(luò)編程Tcp下C/S架構(gòu)如何實現(xiàn)即時通信
文章地址:http://aaarwkj.com/article32/igehpc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、移動網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、用戶體驗、靜態(tài)網(wǎng)站、云服務(wù)器

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司
日韩最新视频一区二区三| 日韩毛片免费看美日韩毛片| 国产麻豆剧传媒国产av| 亚洲各类熟女们中文字幕| 亚洲天堂av现在观看| 神马影院在线观看午夜| 最新人妻少妇精品中文字幕视频| 日本99精品视频10| 欧美综合亚洲韩精品区| 蜜桃视频国产在线观看| 黄色av网站在线免费| 国内一级黄色片免费观看| 欧美成人午夜福利在线视频| 欧美日韩在线一区二区精品| 未满十八禁止免费在线观看| 88国产精品久久久久久| 蜜臀av首页在线观看| 真做的欧美三级在线观看| 国产精品日韩伦理一区二区| 少妇二区三区精品视频| 成人午夜福利视频免费观看| 成人激情视频在线观看| 午夜精品国产日韩欧美在线 | 亚洲欧美成人高清在线观看| 午夜香蕉av一区二区三区| 欧美熟妇精品一区二区蜜桃| 日本一区两区三区不卡视频| 亚洲中文字幕伦理在线| 成年人免费在线观看国产| 国产自拍最新在线视频| 国产美女被狂操到高潮| 国产偷国产偷亚洲综合av| 国产成人亚洲精品午夜国产馆| 天天爽天天看天天射天天操| 少妇高潮叫床免费网站在线观看| 亚洲视频一直看一直爽| 亚洲成人高清在线播放| 熟妇一区二区三区av| 中文字幕日本专区人妻| 日本美女阴部毛茸茸视频| 青青草原一区二区三区|