Bonjour, me heurte a un problème depuis plusieurs jour ou j'ai essayer de le ressoudre par mes propre moyen mais sans résultat.

J'utilise un QTcpSocket dans une app avec GUI, le tout fonctionne correctement
mais quand je change de fenêtre le socket mais plusieurs minute a récupérer des packet de quelque octet (j'ai vérifier depuis mon serveur ils sont envoyer correctement et dans les temps)

voila mon code 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
#pragma once
 
#include <QtGui>
#include <QtNetwork>
#include "../Opcode.hpp"
#include "OpcodeHandlers.hpp"
 
/// Tcp client interface
class ClientSocket : QObject
{
	Q_OBJECT
 
	public:
		/// Constructor
		ClientSocket(QObject * p_Parent);
		/// Destructor
		~ClientSocket();
 
		/// Async connect to host
		void AsyncConnect(QString p_Host, quint16 p_Port);
 
		/// Send 
		void Send(quint16 p_Opcode, Network::Packet & p_Buffer);
 
		/// Close
		void Close();
 
		/// run
		void run();
 
	signals:
		/// On connected
		void OnConnected();
		/// On disconnected
		void OnDisconnected();
		/// On packet ready
		void OnPacketReady(quint16 p_Opcode, Network::Packet * p_Packet);
 
	private slots:
		/// Connected
		void _Connected();
		/// Disconnected
		void _Disconnected();
		/// Data availble
		void _DataAvailable();
		/// Socket error
		void _SocketError(QAbstractSocket::SocketError p_Error);
 
	private:
		QTcpSocket *	m_Socket;				/// Qt client socket
		quint16			m_PendingPacketSize;	/// Pending packet size
		void * m_PacketDispatcher;
};
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
121
122
#include "stdafx.h"
#include "ClientSocket.hpp"
#include "OpcodeHandlers.hpp"
 
/// Constructor
ClientSocket::ClientSocket(QObject * p_Parent)
	:  QObject(p_Parent)
{
	m_Socket = new QTcpSocket(this);
 
	connect(m_Socket, SIGNAL(connected()), this, SLOT(_Connected()));
	connect(m_Socket, SIGNAL(disconnected()), this, SLOT(_Disconnected()));
	connect(m_Socket, SIGNAL(readyRead()), this, SLOT(_DataAvailable()));
	connect(m_Socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(_SocketError(QAbstractSocket::SocketError)));
	//connect(this, SIGNAL(OnPacketReady(quint16, Network::Packet*)), this, SLOT(_PacketReady(quint16, Network::Packet*)));
 
	m_PendingPacketSize = 0;
}
/// Destructor
ClientSocket::~ClientSocket()
{
	m_Socket->abort();
	m_Socket->close();
}
 
//////////////////////////////////////////////////////////////////////////
 
/// Async connect to host
void ClientSocket::AsyncConnect(QString p_Host, quint16 p_Port)
{
	m_Socket->abort();
	m_Socket->connectToHost(p_Host, p_Port);
}
 
//////////////////////////////////////////////////////////////////////////
 
/// Send 
void ClientSocket::Send(quint16 p_Opcode, Network::Packet & p_Buffer)
{
	QByteArray l_Paquet(2 * sizeof(quint16), 0);
	QDataStream l_Stream(&l_Paquet, QIODevice::WriteOnly);
 
	l_Stream << (quint16)(p_Buffer.GetDataSize() + sizeof(quint16));
	l_Stream << p_Opcode;
 
	m_Socket->write(l_Paquet);
 
	if (p_Buffer.GetDataSize() != 0)
		m_Socket->write(p_Buffer.GetData(), p_Buffer.GetDataSize());
}
 
//////////////////////////////////////////////////////////////////////////
 
/// Close
void ClientSocket::Close()
{
	m_Socket->close();
	m_Socket->abort();
}
 
 
//////////////////////////////////////////////////////////////////////////
 
/// Connected
void ClientSocket::_Connected()
{
	emit OnConnected();
}
/// Disconnected
void ClientSocket::_Disconnected()
{
	emit OnDisconnected();
}
/// Data availble
void ClientSocket::_DataAvailable()
{
	QDataStream l_Stream(m_Socket);
 
	if (m_PendingPacketSize == 0)
	{
		if (m_Socket->bytesAvailable() < (int)sizeof(quint16))
			return;
 
		l_Stream >> m_PendingPacketSize;
	}
 
	if (m_Socket->bytesAvailable() < m_PendingPacketSize)
		return;
 
	quint16 l_Opcode = 0xFFFF;
 
	l_Stream >> l_Opcode;
 
	Network::Packet * l_Packet = new Network::Packet(l_Opcode, m_PendingPacketSize - sizeof(quint16));
 
	if ((m_PendingPacketSize - sizeof(quint16)) != 0)
		l_Stream.readRawData(l_Packet->GetData(), m_PendingPacketSize - sizeof(quint16));
 
	m_PendingPacketSize = 0;
 
	Network::CallOpcodesHandlers(l_Opcode, l_Packet);
	//emit OnPacketReady(l_Opcode, l_Packet);
}
/// Socket error
void ClientSocket::_SocketError(QAbstractSocket::SocketError p_Error)
{
	switch(p_Error)
	{
	case QAbstractSocket::HostNotFoundError:
		emit OnDisconnected();
		break;
	case QAbstractSocket::ConnectionRefusedError:
		emit OnDisconnected();
		break;
	case QAbstractSocket::RemoteHostClosedError:
		emit OnDisconnected();
		break;
 
	default:
		emit OnDisconnected();
	}
}