[SOLVED] how to disconnect a certain client user from server
-
[quote author="kalster" date="1326183713"]all i want to do is delete a user from client. i really need a step by step example here. can someone take the time to help me. all i have is a user name and an ip address and i need to boot that person from the client. volker said that i need a qmap but that is not enough information for me to solve this.[/quote]
Honestly, there is no way one can provide you a step by step example here. There are so many questions left unanswered: what is a user, what is a client in your case, what does boot mean, how are all those things are actually tied together in your application and how do they map to application objects.
There are specific answers to specific questions and there are general answers to general questions - you cannot have both. So if you need specific assistance you will have to provide (a whole lot) more information - otherwise all you'll get is a general solution - which might map to your problem, or not - like this:
If you want to store some value accessible by a key you will have to select an associative container - which is basically QMap or QHash. As both are template classes they allow you to freely select the types for the key (Key) and the value (T), as long as they provide a default constructor, a copy constructor and an assignment operator. This means you can use QHostAddress as Key, but you will have to use QObject* or QTcpSocket* in your case instead of QObject as T (QObject and QObject* are different types).
@
QHash<QHostAddress, QTcpSocket*> clients;QTcpSocket* client = new QTcpSocket;
...// client connected, add to list
clients.insert(client->peerAddress(), client);
...// disconnect and remove client with QHostAddress address
QTcpSocket* client = clients.value(address);
client->close();
client->deleteLater();
clients.remove(address);
...
@
Brain to terminal. Not tested. Exemplary. -
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. :)