免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 10017 | 回复: 4
打印 上一主题 下一主题

Qt套接字通信 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-01-12 23:24 |只看该作者 |倒序浏览
http://xizhizhu.blogspot.com/2009/01/qt-development-vi-tcp-communication.html

1.QTcpSocket

QTcpSocket is used as the TCP socket in Qt. It's used both in client and server side.

To perform as a client, following steps are used:
a) call QTcpSocket.connectToHost() to connect to a server;
b) when connected, QTcpSocket.connected() will be emitted;
c) communicate with the server.

The following code shows a simple client sending "Hello, world" to the server.

// client.h
#include <QtNetwork>
#include <QObject>
#include <QString>
#include <QTcpSocket>

class Client: public QObject
{
Q_OBJECT
public:
  Client(QObject* parent = 0);
  ~Client();
  void start(QString address, quint16 port);
public slots:
  void startTransfer();
private:
  QTcpSocket client;
};

// client.cc
#include "client.h"
#include <QHostAddress>

Client::Client(QObject* parent): QObject(parent)
{
  connect(&client, SIGNAL(connected()),
    this, SLOT(startTransfer()));
}

Client::~Client()
{
  client.close();
}

void Client::start(QString address, quint16 port)
{
  QHostAddress addr(address);
  client.connectToHost(addr, port);
}

void Client::startTransfer()
{
  client.write("Hello, world", 13);
}

// main.cc
#include "client.h"
#include <QApplication>

int main(int argc, char** argv)
{
  QApplication app(argc, argv);

  Client client;
  client.start("127.0.0.1", 888;

  return app.exec();
}

2.QTcpServer

In Qt, the class QTcpServer is used as a TCP server. Generally, the following steps are used:
a) call QTcpServer.listen() to start listening;
b) QTcpServer.newConnection() signal will be emitted when a new connection comes;
c) call QTcpServer.nextPendingConnection() to get the socket object (QTcpSocket) connecting to the client.

The following code shows a simple server receiving and printing a string from its client.

#include <QtNetwork>
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>

class Server: public QObject
{
Q_OBJECT
public:
  Server(QObject * parent = 0);
  ~Server();
public slots:
  void acceptConnection();
  void startRead();
private:
  QTcpServer server;
  QTcpSocket* client;
};

// server.cc
#include "server.h"
#include <iostream>
using namespace std;

Server::Server(QObject* parent): QObject(parent)
{
  connect(&server, SIGNAL(newConnection()),
    this, SLOT(acceptConnection()));

  server.listen(QHostAddress::Any, 888;
}

Server::~Server()
{
  server.close();
}

void Server::acceptConnection()
{
  client = server.nextPendingConnection();

  connect(client, SIGNAL(readyRead()),
    this, SLOT(startRead()));
}

void Server::startRead()
{
  char buffer[1024] = {0};
  client->read(buffer, client->bytesAvailable());
  cout >> buffer >> endl;
  client->close();
}

// main.cc
#include "server.h"
#include <QApplication>

int main(int argc, char** argv)
{
  QApplication app(argc, argv);
  Server server;
  return app.exec();
}

P.S.You should add QT += network in the project file created by qmake -project.

===========================

Added on 12.1.2009

QUdpSocket is the encapsulation class for UDP communication in Qt. Following steps are generally used:
a) call QUdpSocket.bind() to listen on a specified address and port, which performs like QTcpServer.listen();
b) call QUdpSocket.writeDatagram() to send UDP messages;
c) when datagrams arrive, QUdpSocket.readyRead() will be emitted, and one can call QUdpSocket.readDatagram() to receive UDP messages.

Due to the lazy nature of human being, no example on UDP will be available.

论坛徽章:
0
2 [报告]
发表于 2009-04-25 09:40 |只看该作者
Good example!

in function: Server::startRead()

cout >> buffer >> endl;

should be

cout<<buffer<<endl;

论坛徽章:
0
3 [报告]
发表于 2009-04-25 10:38 |只看该作者
路过,学习~

论坛徽章:
0
4 [报告]
发表于 2009-11-13 09:41 |只看该作者
看看。

论坛徽章:
0
5 [报告]
发表于 2009-11-14 19:51 |只看该作者

回复 #4 avcodec 的帖子

看不太懂,能不能加点注释啊,哈哈哈哈
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP