##################### client.h ##########################
view plaincopy to clipboardprint?
01.#ifndef CLIENT_H
02.#define CLIENT_H
03.#include <QDialog>
04.#include <QtGui>
05.#include <QtNetwork>
06.namespace Ui {
07. class Client;
08.}
09.class Client : public QDialog
10.{
11. Q_OBJECT
12.public:
13. explicit Client(QWidget *parent = 0);
14. ~Client();
15.private:
16. QLabel *label;
17. QPushButton *sendBtn;
18. QTcpSocket *tcpSocket;
19. quint16 port;
20. QHostAddress *serverIP;
21.private:
22. Ui::Client *ui;
23.private slots:
24. void picSend();
25.};
26.#endif // CLIENT_H
##################### client.cpp ##########################
view plaincopy to clipboardprint?
01.#include "client.h"
02.#include "ui_client.h"
03.Client::Client(QWidget *parent) :
04. QDialog(parent),
05. ui(new Ui::Client)
06.{
07. ui->setupUi(this);
08. tcpSocket = new QTcpSocket(this);
09. port = 8010;
10. serverIP = new QHostAddress;
11. QString ip = "127.0.0.1";
12. serverIP->setAddress(ip);
13. tcpSocket->connectToHost(*serverIP,port);
14. label = new QLabel(this);
15. label->setFixedSize(800,600);
16. label->move(0,0);
17. label->show();
18. sendBtn = new QPushButton("send",this);
19. sendBtn->setFixedSize(browse->sizeHint());
20.
21. sendBtn->show();
22. connect(sendBtn,SIGNAL(clicked()),this,SLOT(picSend()));
23.}
24.Client::~Client()
25.{
26. delete ui;
27.}
28.void Client::picSend()
29.{
30. QPixmap pic(":/images/1.png");
31. QBuffer buffer;
32. buffer.open(QIODevice::ReadWrite);
33. pic.save(&buffer,"PNG");
34. label->setPixmap(pic);
35.
36. QByteArray dataStr;
37. QDataStream out(&dataStr,QIODevice::WriteOnly);
38. out.setVersion(QDataStream::Qt_4_3);
39. out << (quint32) buffer.data().size();
40. dataStr.append(buffer.data());
41. tcpSocket->write(dataStr);
42. dataStr.resize(0);
43.}
服务器端代码:
##################### server.h ########################
view plaincopy to clipboardprint?
01.#ifndef SERVER_H
02.#define SERVER_H
03.#include <QDialog>
04.#include <QtGui>
05.#include <QtNetwork>
06.#define PORT 8010
07.namespace Ui {
08. class Server;
09.}
10.class MyServer : public QTcpServer
11.{
12. Q_OBJECT
13.public:
14. MyServer(QObject* parent =0 );
15. ~MyServer();
16.protected:
17. void incomingConnection(int);
18.signals:
19. void newConnected(QTcpSocket *);
20.};
21.class Server : public QDialog
22.{
23. Q_OBJECT
24.public:
25. explicit Server(QWidget *parent = 0);
26. ~Server();
27.private:
28. Ui::Server *ui;
29.private:
30. QLabel *label;
31. MyServer *server;
32. QTcpSocket *tcpSocket;
33.protected slots:
34. void newConnectionArrived(QTcpSocket *);
35. void socketConnectionClosed();
36. void socketReadyRead();
37.};
38.#endif // SERVER_H
##################### server.cpp #######################
view plaincopy to clipboardprint?
01.#include "server.h"
02.#include "ui_server.h"
03.int dataSize=0;
04.MyServer::MyServer(QObject *parent):QTcpServer(parent)
05.{
06.}
07.MyServer::~MyServer()
08.{
09.}
10.void MyServer::incomingConnection(int socketDescriptor)
11.{
12. QTcpSocket *t = new QTcpSocket(this);
13. t->setSocketDescriptor(socketDescriptor);
14. emit newConnected(t);
15.}
16.Server::Server(QWidget* parent):
17. QDialog(parent),
18. ui(new Ui::Server)
19.{
20. ui->setupUi(this);
21. label = new QLabel(this);
22. label->setFixedSize(1000,800);
23. label->setText("wait...");
24.
25. tcpSocket = NULL;
26. server = new MyServer(this);
27. server->listen(QHostAddress::Any,PORT);
28. connect(server,SIGNAL(newConnected(QTcpSocket*)),this,SLOT(newConnectionArrived(QTcpSocket*)));
29.}
30.Server::~Server()
31.{
32. delete ui;
33.}
34.void Server::newConnectionArrived(QTcpSocket *s)
35.{
36. tcpSocket = s;
37. connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(socketConnectionClosed()));
38. connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(socketReadyRead()));
39.}
40.void Server::socketReadyRead()
41.{
42. if(dataSize ==0)
43. {
44. QDataStream in(tcpSocket);
45. in.setVersion(QDataStream::Qt_4_3);
46. if(tcpSocket->bytesAvailable() < sizeof(quint32))
47. return;
48. in>>dataSize;
49. qDebug()<<dataSize;
50. }
51. if(dataSize > tcpSocket->bytesAvailable())
52. return;
53. QByteArray array = tcpSocket->read(dataSize);
54. QBuffer buffer(&array);
55. buffer.open(QIODevice::ReadOnly);
56. QImageReader reader(&buffer,"PNG");
57. QImage image = reader.read();
58. if(!image.isNull())
59. {
60. qDebug()<<"hello";
61. label->setPixmap(QPixmap::fromImage(image));
62. dataSize = 0;
63. }
64. else
65. label->setText("Empty");
66.}
67.void Server::socketConnectionClosed()
68.{
69. tcpSocket = NULL;
70.}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/XiaoMT_Rush/archive/2011/05/21/6435955.aspx