Hello, I am developing a client-server app with QTcpSocket and QTcpServer (right now I have to make a simple chat).
I am testing my app in local network, where there is one server and 2 clients, one client connects to localhost (as it runs on the same machine with the server) and another to 192.168.0.1, server listens at port 25600
Clients connect fine, server stores their QTcpSockets in QList and QMap and when info comes from one client, the server sends it to other clients. If changes happen on server, it sends info to all clients.
Data transfer works fine, but when any client leaves the server, the following code is executed:
void Server::Disconnected() // slot, connected to sockets
{
RemoveClient(qobject_cast<QTcpSocket*>(sender()));
}
{
QString nick
= clients
[client
];
//clients is a QMap<QTcpSocket*, QString> //at this line client is removed from map and list, but I don't remember the exact code, I'm writing this post from other machine
foreach
(QTcpSocket* s, clientsRaw
) //clientsRaw is a QList<QTcpSocket*> {
//here I want to inform all other clients that participant called 'nick' left
WriteMessage(DT_PLAYERLEFT, nick, s); // this method writes number of bytes to be written, data type and the string to the QTcpSocket
//DT_PLAYERLEFT is const quint16 with value of 5
}
}
void Server::Disconnected() // slot, connected to sockets
{
RemoveClient(qobject_cast<QTcpSocket*>(sender()));
}
void Server::RemoveClient(QTcpSocket* client)
{
QString nick = clients[client]; //clients is a QMap<QTcpSocket*, QString>
//at this line client is removed from map and list, but I don't remember the exact code, I'm writing this post from other machine
foreach(QTcpSocket* s, clientsRaw) //clientsRaw is a QList<QTcpSocket*>
{
//here I want to inform all other clients that participant called 'nick' left
WriteMessage(DT_PLAYERLEFT, nick, s); // this method writes number of bytes to be written, data type and the string to the QTcpSocket
//DT_PLAYERLEFT is const quint16 with value of 5
}
}
To copy to clipboard, switch view to plain text mode
The nickname is valid, WriteMessage method works fine as it was tested when I transfered data with all clients online.
But after this code, when I have only 1 client connected to server, the data coming TO client is some junk, while data coming FROM client is read fine. The client code which handles incoming data is valid, but the incoming data after another client's leave does'nt even have meaningful data type. What did I do wrong?
Bookmarks