Bonjour,

Je suis en train de développer un petit programme basé sur QTcpSocket et QTcpServer, le but étant d'avoir une communication bidirectionnelle entre un serveur et un client.
Malheureusement, si je reçois les données correctement au niveau du client la communication client => serveur ne fonctionne pas.
N'étant pas du tout familier avec les sockets, Qt ou autre, j'en profite pour poster l'ensemble de mon code pour que vous puissiez faire des éventuelles critiques.

Voici le code coté serveur :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "CTcpServer.h"
 
#include <QFile>
#include <QCoreApplication>
 
//-------------------------------------------------------------------------------------------------
CTcpServer::CTcpServer(int iListenPort, QObject *parent /*= 0*/)
: QTcpServer(parent)
, m_iListenPort(iListenPort)
, m_bIsRunning(false)
, m_iBlockSize(0)
{
	connect(this, SIGNAL(newConnection()), this, SLOT(onServerConnection()));
}
 
//-------------------------------------------------------------------------------------------------
void CTcpServer::stop()
{
	m_bIsRunning = false;
 
	close();
 
	foreach (QTcpSocket* tcpSocket, m_TcpSocketList)
	{
		tcpSocket->close();
	}
 
	m_TcpSocketList.clear();
}
 
//-------------------------------------------------------------------------------------------------
void CTcpServer::addSendMessage(const QString& strMessage)
{
	m_strSendMessage = strMessage;
	sendMessage();
}
 
//-------------------------------------------------------------------------------------------------
void CTcpServer::startListening()
{
	if (!listen(QHostAddress::Any, m_iListenPort))
	{
		qDebug() << "Unable to start the server";
		m_bIsRunning = false;
	}
	else
	{
		m_bIsRunning = true;
	}
}
 
//-------------------------------------------------------------------------------------------------
void CTcpServer::sendMessage()
{
	if (m_bIsRunning && !m_strSendMessage.isEmpty())
	{
		foreach (QTcpSocket* tcpSocket, m_TcpSocketList)
		{
			QByteArray block;
			QDataStream out(&block, QIODevice::WriteOnly);
			out.setVersion(QDataStream::Qt_4_7);
			out << (quint16)0;
			out << m_strSendMessage;
			out.device()->seek(0);
			out << (quint16)(block.size() - sizeof(quint16));
 
			tcpSocket->write(block);
			tcpSocket->waitForBytesWritten();
		}
 
		m_strSendMessage.clear();
	}
}
 
//-------------------------------------------------------------------------------------------------
void CTcpServer::onServerConnection()
{
	QTcpSocket* pSocket = nextPendingConnection();
	//pSocket->setReadBufferSize(SOCKET_READ_BUFFER);
	m_TcpSocketList.append(pSocket);
	connect(pSocket, SIGNAL(readyRead()), this, SLOT(onSocketReadyRead()));
	connect(pSocket, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
}
 
//-------------------------------------------------------------------------------------------------
void CTcpServer::onSocketReadyRead()
{
	if (QTcpSocket* pSocket = dynamic_cast<QTcpSocket*>(QObject::sender()))
	{
		QDataStream in(pSocket);
		in.setVersion(QDataStream::Qt_4_7);
 
		if (m_iBlockSize == 0)
		{
			if (pSocket->bytesAvailable() < (int)sizeof(quint16))
				return;
 
			in >> m_iBlockSize;
		}
 
		if (pSocket->bytesAvailable() < m_iBlockSize)
			return;
 
		m_iBlockSize = 0;
 
		// On arrive jamais ici.. :(
		QString lastMsg;
		in >> lastMsg;
		emit newMessage(pSocket->peerAddress().toString(), lastMsg);
	}
}
 
//-------------------------------------------------------------------------------------------------
void CTcpServer::onDisconnected()
{
	if (QTcpSocket* pSocket = dynamic_cast<QTcpSocket*>(QObject::sender()))
	{
		m_TcpSocketList.removeAll(pSocket);
	}
}
Et le côté client :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
 
#include <QtGui>
#include <QtNetwork>
 
#include "client.h"
 
//-------------------------------------------------------------------------------------------------
Client::Client(const QString& strHostIPAdress, int iPort, QObject *parent /*= 0*/)
: QObject(parent)
, m_strHostIPAdress(strHostIPAdress)
, m_iPort(iPort)
, m_bIsConnected(false)
, m_iBlockSize(0)
{
	m_TcpSocket = new QTcpSocket(this);
 
	connect(m_TcpSocket, SIGNAL(readyRead()), this, SLOT(onReadMessage()));
	connect(m_TcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onSocketError(QAbstractSocket::SocketError)));
	connect(m_TcpSocket, SIGNAL(disconnected()), this, SLOT(disconnectFromHost()));
 
	connect(&m_RetryConnectionTimer, SIGNAL(timeout()), this, SLOT(connectToHost()));
	connectToHost();
 
	m_RetryConnectionTimer.start(iDTReconnection);
}
 
//-------------------------------------------------------------------------------------------------
Client::~Client()
{
	delete m_TcpSocket;
}
 
//-------------------------------------------------------------------------------------------------
void Client::connectToHost()
{
	if (!m_bIsConnected)
	{
		m_bIsConnected = true;
		m_iBlockSize = 0;
		m_TcpSocket->abort();
		m_TcpSocket->connectToHost(m_strHostIPAdress, m_iPort, QIODevice::ReadWrite);
		m_TcpSocket->waitForConnected();
 
		qDebug() << "Client::connectToHost() : Connected.";
	}
}
 
//-------------------------------------------------------------------------------------------------
void Client::disconnectFromHost()
{
	m_bIsConnected = false;
	m_RetryConnectionTimer.stop();
	m_TcpSocket->abort();
}
 
//-------------------------------------------------------------------------------------------------
void Client::onReadMessage()
{
	QDataStream in(m_TcpSocket);
	in.setVersion(QDataStream::Qt_4_7);
 
	if (m_iBlockSize == 0)
	{
		if (m_TcpSocket->bytesAvailable() < (int)sizeof(quint16))
			return;
 
		in >> m_iBlockSize;
	}
 
	if (m_TcpSocket->bytesAvailable() < m_iBlockSize)
		return;
 
	m_iBlockSize = 0;
 
	QString lastMsg;
	in >> lastMsg;
	emit newMessage(lastMsg);
}
 
//-------------------------------------------------------------------------------------------------
void Client::onSocketError(QAbstractSocket::SocketError socketError)
{
	m_bIsConnected = false;
}
 
//-------------------------------------------------------------------------------------------------
void Client::addSendMessage(const QString& sendMessage)
{
	if (m_bIsConnected && !sendMessage.isEmpty())
	{
		QByteArray packet;  
		QDataStream out(&packet, QIODevice::WriteOnly);
		out.setVersion(QDataStream::Qt_4_7);
		out << (quint16)0;
		out << sendMessage;
		out.device()->seek(0);  
		out << (quint16)(packet.size() - sizeof(quint16));  
		m_TcpSocket->write(packet);
		m_TcpSocket->flush();
		m_TcpSocket->waitForBytesWritten();		
	}
}