Chinaunix首页 | 论坛 | 博客
  • 博客访问: 618554
  • 博文数量: 178
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 2162
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-12 20:06
文章分类

全部博文(178)

文章存档

2011年(1)

2010年(94)

2009年(86)

我的朋友

分类:

2009-11-17 22:55:36

1.QTcpSocket:client

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
#include
#include
#include

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

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

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

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

return app.exec();
}

2.QTcpServer
: server

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
#include
#include
#include

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
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

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

3.QUdpSocket:


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.

From:  :zxz1984(浪迹天涯)
阅读(547) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:删除QListWidget的item

给主人留下些什么吧!~~