文件名称:
Qt编程范例之中高级篇3_网络编程部分
开发工具:
文件大小: 7mb
下载次数: 0
上传时间: 2010-12-10
详细说明: /**************************************************************************** ** ** Copyright (C) 1992-2008 Trolltech ASA. All rights reserved. ** ** This file is part of the example classes of the Qt Toolkit. ** ** This file may be used under the terms of the GNU General Public ** License versions 2.0 or 3.0 as published by the Free Software ** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3 ** included in the packaging of this file. Alternatively you may (at ** your option) use any later version of the GNU General Public ** License if such license has been publicly approved by Trolltech ASA ** (or its successors, if any) and the KDE Free Qt Foundation. In ** addition, as a special exception, Trolltech gives you certain ** additional rights. These rights are described in the Trolltech GPL ** Exception version 1.1, which can be found at ** http://www.trolltech.com/products/qt/gplexception/ and in the file ** GPL_EXCEPTION.txt in this package. ** ** Please review the following information to ensure GNU General ** Public Licensing requirements will be met: ** http://trolltech.com/products/qt/licenses/licensing/opensource/. If ** you are unsure which license is appropriate for your use, please ** review the following information: ** http://trolltech.com/products/qt/licenses/licensing/licensingoverview ** or contact the sales department at sales@trolltech.com. ** ** In addition, as a special exception, Trolltech, as the sole ** copyright holder for Qt Designer, grants users of the Qt/Eclipse ** Integration plug-in the right for the Qt/Eclipse Integration to ** link to functionality provided by Qt Designer and its related ** libraries. ** ** This file is provided "AS IS" with NO WARRANTY OF ANY KIND, ** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR ** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly ** granted herein. ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ****************************************************************************/ #include #include "client.h" #include "connection.h" #include "peermanager.h" Client::Client() { peerManager = new PeerManager(this); peerManager->setServerPort(server.serverPort()); peerManager->startBroadcasting(); QObject::connect(peerManager, SIGNAL(newConnection(Connection *)), this, SLOT(newConnection(Connection *))); QObject::connect(&server, SIGNAL(newConnection(Connection *)), this, SLOT(newConnection(Connection *))); } void Client::sendMessage(const QString &message) { if (message.isEmpty()) return; QList connections = peers.values(); foreach (Connection *connection, connections) connection->sendMessage(message); } QString Client::nickName() const { return QString(peerManager->userName()) + "@" + QHostInfo::localHostName() + ":" + QString::number(server.serverPort()); } bool Client::hasConnection(const QHostAddress &senderIp, int senderPort) const { if (senderPort == -1) return peers.contains(senderIp); if (!peers.contains(senderIp)) return false; QList connections = peers.values(senderIp); foreach (Connection *connection, connections) { if (connection->peerPort() == senderPort) return true; } return false; } void Client::newConnection(Connection *connection) { connection->setGreetingMessage(peerManager->userName()); connect(connection, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectionError(QAbstractSocket::SocketError))); connect(connection, SIGNAL(disconnected()), this, SLOT(disconnected())); connect(connection, SIGNAL(readyForUse()), this, SLOT(readyForUse())); } void Client::readyForUse() { Connection *connection = qobject_cast(sender()); if (!connection || hasConnection(connection->peerAddress(), connection->peerPort())) return; connect(connection, SIGNAL(newMessage(const QString &, const QString &)), this, SIGNAL(newMessage(const QString &, const QString &))); peers.insert(connection->peerAddress(), connection); QString nick = connection->name(); if (!nick.isEmpty()) emit newParticipant(nick); } void Client::disconnected() { if (Connection *connection = qobject_cast(sender())) removeConnection(connection); } void Client::connectionError(QAbstractSocket::SocketError /* socketError */) { if (Connection *connection = qobject_cast(sender())) removeConnection(connection); } void Client::removeConnection(Connection *connection) { if (peers.contains(connection->peerAddress())) { peers.remove(connection->peerAddress()); QString nick = connection->name(); if (!nick.isEmpty()) emit participantLeft(nick); } connection->deleteLater(); } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.