[SOLVED] how to disconnect a certain client user from server
-
i guess i never made my point clear. lets say the user is joe. i want to boot (ban) only joe from the client. everyone else in the client is still able to post a message.
at the server side. i know that joe is online. i need to boot him from the server.
i cant use clients.remove(client) because client could be anyone. clients.remove("joe") give a tcpserver error.
the server is online and the only needed information is what i gave in my above post.
@ QMapIterator <QTcpSocket, int> test;
QString nick = str;
test.insert(nick, 1);
str.remove(nick.toLower());
@in the last line of code, i am trying to remove joe from the client from server.. but it is not working. i get an error that a string cannot be used, which is why i used the maplterator in the first place.
from the server i need to only close the client joe. everyone else is still able to post. i would like to create a banning option for the server.
-
Please re-read Lukas' message. You are not giving enough information. I already pointed out the many issues and missing context of the small snippet you are posting, but yet you simply re-post it. You are telling us nothing about how users map to sockets, or where you keep that information your application. Yet you expect us to somehow know all that.
-
header
private:
@QSet<QTcpSocket*> clients;
QMap<QTcpSocket*,QString> users;@main.cpp
my users are connected by...
QTcpSocket client = (QTcpSocket)sender();
clients.insert(client);
clients is the current client connected.i remove the current user by...
clients.remove(client);but i am not able to remove a username from the client.
hope this helps.
-
As I'm still not quite sure about your problem I just assume that you want to get a specific QTcpSocket* stored in <code>users</code>, identified by the QString it associates with - which means you want to access a specific value, not key.
This can be done by manually iterating over the values using QMapIterator, but to me the most painless way in your case will be using "qFind":http://www.youtube.com/watch?v=-xGGPfdQ-n4.
@
QMap<QTcpSocket*, QString>::iterator user = qFind(users.begin(), users.end(), "joe");
if(user != users.end())
{
QTcpSocket* socket = user.key();
QString name = user.value();
}
@
Brain to terminal. Not tested. Exemplary.This thread is a prime example of the endless importance to obey the "smart questioning rules":http://www.catb.org/~esr/faqs/smart-questions.html. If the question would have been "How to find a value stored in a container" it has been answered in the first reply.
-
Lukas Geyer with your code, i tried to remove your code with my code at the end of it. the user does not get disconnected.
what more information do you need so that i can get this issue closed?
@clients.remove(socket);
users.remove(socket);@ -
Of course <code>users.remove(socket)</code> doesn't close the connection to the client, it just removes the value <code>socket</code> from the collection <code>users</code>.
If you take a look at the documentation of "QTcpSocket":http://developer.qt.nokia.com/doc/qt-4.8/qabstractsocket.html#close, which method might be suitable to close the connection?
-
ok. i will but i don't have time right now to try to close the socket or even look thoroughly into your code which looks great and should be what i am looking for. i will post my results later in the day.
thank you for you patience
-
I thought i did not have time but i did.
the code works great. I can now disconnect a selected user. thank you Lukas Geyer. you saved the day :)
edit: i forgot about closing the socket.
-
ps: Andre, i fixed my few posts. they are now neater to read
-
Thanks, folks. Let's try not to yell at each other and be constructive, both on the asking and the replying end.
All good, carry on. :)