当前位置: 首页 > news >正文

html5企业网站案例建设通官网app下载

html5企业网站案例,建设通官网app下载,vue做的网站,网站全站开发QT示例#xff1a;基于TCP 点对多通讯#xff08;server,clients#xff09;一、服务器server二、客户端Client下载#xff1a;基于TCP 点对多Socket通讯 一、服务器server 因为对于客户端来说#xff0c;只能连接一个服务器。而对于服务器来说#xff0c;它是面向多连… QT示例基于TCP 点对多通讯server,clients一、服务器server二、客户端Client下载基于TCP 点对多Socket通讯 一、服务器server 因为对于客户端来说只能连接一个服务器。而对于服务器来说它是面向多连接的如何协调处理多客户端连接就显得尤为重要。 注意问题 每个新加入的客户端服务器给其分配一个SocketDescriptor后就会emit newConnection()信号但分配好的SocketDecriptor并没有通过newConnection()信号传递所以用户得不到这个客户端标识SocketDescriptor。同样的每当服务器收到新的消息时客户端会emit readReady()信号然而readReady()信号也没有传递SocketDescriptor 这样的话服务器端即使接收到消息也不知道这个消息是从哪个客户端发出的。解决的方法 通过重写[virtual protected] void QTcpServer::incomingConnection(qintptr socketDescriptor)获取soketDescriptor。自定义TcpClient类继承QTcpSocket并将获得的soketDescriptor作为类成员。 这个方法的优点是可以获取到soketDescriptor灵活性高。缺点是需要重写函数、自定义类。在newConnection()信号对应的槽函数中通过QTcpSocket *QTcpServer::nextPendingConnection()函数获取 新连接的客户端Returns the next pending connection as a connected QTcpSocket object. 虽然仍然得不到soketDescriptor**但可以通过QTcpSocket类的peerAddress()和peerPort()成员函数获取客户端的IP和端口号同样是唯一标识。 **优点无需重写函数和自定义类代码简洁。缺点无法获得SocketDecriptor灵活性差。 本文示例为第二种方法 1.pro 添加 QT network2主函数 main.cpp 添加 #include mytcpserver.h #include QApplicationint main(int argc, char *argv[]) {QApplication a(argc, argv);MyTcpServer w;w.show();return a.exec(); }3MyTcpServer.h 添加 #include mytcpserver.h #include ui_mytcpserver.hMyTcpServer::MyTcpServer(QWidget *parent) :QMainWindow(parent),ui(new Ui::MyTcpServer) {ui-setupUi(this);// 一 、创建QTcpSever对象;tcpServer new QTcpServer(this);ui-edtIP-setText(QNetworkInterface().allAddresses().at(1).toString()); //获取本地IPui-btnConnect-setEnabled(true);ui-btnSend-setEnabled(false);// 设置默认按钮样式ui-btnConnect-setStyleSheet();connect(tcpServer, QTcpServer::newConnection, this, MyTcpServer::NewConnectionSlot); }MyTcpServer::~MyTcpServer() {delete ui; }// 二、监听--断开 void MyTcpServer::on_btnConnect_clicked() {if(ui-btnConnect-text()监听){bool ok tcpServer-listen(QHostAddress::Any, ui-edtPort-text().toInt());if(ok){ui-btnConnect-setText(断开);ui-btnConnect-setStyleSheet(color: red;);ui-btnSend-setEnabled(true);}}else{for(int i0; itcpClient.length(); i) // 断开所有连接{tcpClient[i]-disconnectFromHost();bool ok tcpClient[i]-waitForDisconnected(1000);if(!ok){// 处理异常QMessageBox::warning(this, tr(错误),tr(断开连接失败), QMessageBox::Ok);}tcpClient.removeAt(i); // 从保存的客户端列表中取去除}tcpServer-close(); // 不再监听端口ui-btnConnect-setText(监听);ui-btnConnect-setStyleSheet();ui-btnSend-setEnabled(false);} }// 三、新连接建立的槽函数 void MyTcpServer::NewConnectionSlot() {currentClient tcpServer-nextPendingConnection();tcpClient.append(currentClient);ui-cbxConnection-addItem(tr(%1:%2).arg(currentClient-peerAddress().toString().split(::ffff:)[1]).arg(currentClient-peerPort()));connect(currentClient, QTcpSocket::readyRead, this, MyTcpServer::ReadData);connect(currentClient, QTcpSocket::disconnected, this, MyTcpServer::disconnectedSlot); }// 四、客户端数据可读信号对应的读数据槽函数 void MyTcpServer::ReadData() {// 由于readyRead信号并未提供SocketDecriptor所以需要遍历所有客户端for(int i0; itcpClient.length(); i){QByteArray buffer tcpClient[i]-readAll();if(buffer.isEmpty()) // 客户端 数据为空则跳过continue;// 客户端有数据则 获取IP 和端口static QString IP_Port, IP_Port_Pre;IP_Port tr([%1:%2]:).arg(tcpClient[i]-peerAddress().toString().split(::ffff:)[1]).arg(tcpClient[i]-peerPort());// 若此次消息的地址与上次不同则需显示此次消息的客户端地址if(IP_Port ! IP_Port_Pre)ui-edtRecv-append(IP_Port);ui-edtRecv-append(buffer);//更新ip_portIP_Port_Pre IP_Port;} }// 五、断开连接的槽函数 void MyTcpServer::disconnectedSlot() {//由于disconnected信号并未提供SocketDescriptor所以需要遍历寻找for(int i0; itcpClient.length(); i){if(tcpClient[i]-state() QAbstractSocket::UnconnectedState){// 删除存储在combox中的客户端信息ui-cbxConnection-removeItem(ui-cbxConnection-findText(tr(%1:%2).arg(tcpClient[i]-peerAddress().toString().split(::ffff:)[1]).arg(tcpClient[i]-peerPort())));// 删除存储在tcpClient列表中的客户端信息tcpClient[i]-destroyed();tcpClient.removeAt(i);}} }// 六、发送数据 void MyTcpServer::on_btnSend_clicked() {QString data ui-edtSend-toPlainText();if(data )return; // 文本输入框为空时//全部连接if(ui-cbxConnection-currentIndex() 0){for(int i0; itcpClient.length(); i)tcpClient[i]-write(data.toLatin1()); //qt5除去了.toAscii()}//指定连接else{QString clientIP ui-cbxConnection-currentText().split(:)[0]; // IP 地址int clientPort ui-cbxConnection-currentText().split(:)[1].toInt(); // port 端口号 // qDebug() clientIP; // qDebug() clientPort;for(int i0; itcpClient.length(); i){if(tcpClient[i]-peerAddress().toString().split(::ffff:)[1]clientIP tcpClient[i]-peerPort()clientPort){tcpClient[i]-write(data.toLatin1());return; //ip:port唯一无需继续检索}}} }// 清楚窗口 void MyTcpServer::on_btnClear_clicked() {ui-edtRecv-clear(); } 5界面 mytcpserver.ui 二、客户端Client 1.pro 添加 QT network2主函数 main.cpp 添加 #include mytcpclient.h #include QApplicationint main(int argc, char *argv[]) {QApplication a(argc, argv);MyTcpClient w;w.show();return a.exec(); } 3MyTcpClient.h 添加 #include QMainWindow #include QTcpSocket #include QHostAddress #include QMessageBoxnamespace Ui { class MyTcpClient; }class MyTcpClient : public QMainWindow {Q_OBJECTpublic:explicit MyTcpClient(QWidget *parent 0);~MyTcpClient();private:Ui::MyTcpClient *ui;QTcpSocket *tcpClient;private slots://客户端槽函数void ReadData();void ReadError(QAbstractSocket::SocketError);void on_btnConnect_clicked();void on_btnSend_clicked();void on_pushButton_clicked(); };4MyTcpClient.cpp 添加 #include mytcpclient.h #include ui_mytcpclient.hMyTcpClient::MyTcpClient(QWidget *parent) :QMainWindow(parent),ui(new Ui::MyTcpClient) {ui-setupUi(this);// 一、初始化TCP客户端tcpClient new QTcpSocket(this); //实例化tcpClienttcpClient-abort(); //取消原有连接ui-btnConnect-setEnabled(true);ui-btnSend-setEnabled(false);connect(tcpClient, QTcpSocket::readyRead, this, MyTcpClient::ReadData);connect(tcpClient, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(ReadError(QAbstractSocket::SocketError))); }MyTcpClient::~MyTcpClient() {delete ui; }// 二、连接 void MyTcpClient::on_btnConnect_clicked() {if(ui-btnConnect-text()连接){tcpClient-connectToHost(ui-edtIP-text(), ui-edtPort-text().toInt());if (tcpClient-waitForConnected(1000)) // 连接成功则进入if{}{ui-btnConnect-setText(断开);ui-btnSend-setEnabled(true);}}else{tcpClient-disconnectFromHost();if (tcpClient-state() QAbstractSocket::UnconnectedState || tcpClient-waitForDisconnected(1000)) //已断开连接则进入if{}{ui-btnConnect-setText(连接);ui-btnSend-setEnabled(false);}} }// 三、读取数据 void MyTcpClient::ReadData() {QByteArray buffer tcpClient-readAll();if(!buffer.isEmpty()){ui-edtRecv-append(buffer);} }// 四、发送数据 void MyTcpClient::on_btnSend_clicked() {QString data ui-edtSend-toPlainText();if(data ! ){tcpClient-write(data.toLatin1()); //qt5出去了.toAscii()} }// 连接错误信息处理 void MyTcpClient::ReadError(QAbstractSocket::SocketError) {tcpClient-disconnectFromHost();ui-btnConnect-setText(tr(连接));QMessageBox msgBox;msgBox.setText(tr(failed to connect server because %1).arg(tcpClient-errorString()));msgBox.exec(); }// 清空按钮 void MyTcpClient::on_pushButton_clicked() {ui-edtRecv-clear(); } 2mytcpclient.ui 添加 参考博客 QT 之TCP网络编程
http://wiki.neutronadmin.com/news/451722/

相关文章:

  • 丹棱县 网站建设动画制作软件有哪些
  • 做网站不打广告怎么赚钱网站建设费用要多少
  • 专业做网站的公司邢台专业做网站wordpress不能进后台
  • 访问国外网站加速服装做外贸的网站建设
  • 哈尔滨网站制作最新招聘信息织梦网站图标路径
  • 网站模板源码下载汽修网站怎么做
  • 网站开发视频百度云网站建设环境分析
  • 模板网站的好处学网站建设需要什么软件
  • 秦皇岛网站制作微商城建设谷德设计网官网
  • 免费企业网站建设要求做公司网站每年多少钱
  • 万网站wordpress 走马灯
  • 网站备案logo上海中学有哪些
  • 一级a做爰片免费网站丶给娃娃做衣服卖的网站
  • 中国建设部官方网站海外seo投放
  • seo兼职怎么收费宁波专业seo推广价格
  • 主流的网站开发语言网站开发技术文档范例
  • 空白的网站怎么建设常熟网站建设书生商友
  • 怎么申请个人网站长春网站改版
  • 沈阳网站设计定制网站建设付网站建设费
  • 学做网站论坛vip账号破解中国网站备案信息查询
  • 自贡网站设计新手怎么做淘宝店铺
  • 网站设计规划书中国城乡建设网
  • 网站制作时wordpress 图片分享主题
  • 昆山专业网站建设公司小程序开发指南
  • 帮人网站开发维护违法成都 网站制作
  • 苏州网站建设代理创建网站的注意事项
  • 网站开发一般包括有没有类似wordpress
  • 电视直播网站怎么做苏州苏网建设公司在建工程
  • 类似优酷网站建设价格河南红旗渠建设集团网站
  • wordpress手机网站模版注销主体备案与网站备案表