Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] how to disconnect a certain client user from server
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] how to disconnect a certain client user from server

Scheduled Pinned Locked Moved General and Desktop
17 Posts 5 Posters 10.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    andre
    wrote on last edited by
    #6

    You are not being "run down" because you "don't know enough", pholcer just vented his frustration on you not showing that you learned something of your last post. Learning how to program, and especially to program well, is a long an sometimes frustrating road. It does not help to learn multiple languages at the same time, though it does help to learn more than one in the long run.

    On what you need: opinions on that may differ between you and the people answering. You may be of the opinion that you just need a bite-sized bit of information, while the people who have that knowedge may think that you need to learn how to find that information yourself the next time. Otherwise, they are afraid that the next time you need another bite-sized bit of information, you will come back and ask for that one, instead of having learned how to find it yourself. It's like with the hungry: the hungry man might just want food, but it pays off to spend the time to teach him how to fish instead if he shows the aptitude to learn.

    I considder this meta-discussion now closed. Please don't continue it here. If you really feel a need, open a topic in the DevNet meta forum. I will close this topic if you, or somebody else, continues this issue here.

    Anyway, back on topic:
    In your code section above, it is very unclear what you are trying to do, as we cannot see the types of the variables you use. It looks as if you are trying to remove something from a QString though? And what is that QMapIterator doing there? What map is it related to? Note that using a QTcpSocket as a key can't work. QTcpSockets are QObjects, and these can not be copied, so they are useless as keys.

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #7

      [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.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kalster
        wrote on last edited by
        #8

        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.

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #9

          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.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kalster
            wrote on last edited by
            #10

            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.

            1 Reply Last reply
            0
            • L Offline
              L Offline
              lgeyer
              wrote on last edited by
              #11

              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.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kalster
                wrote on last edited by
                #12

                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);@

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lgeyer
                  wrote on last edited by
                  #13

                  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?

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    kalster
                    wrote on last edited by
                    #14

                    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

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kalster
                      wrote on last edited by
                      #15

                      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.

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kalster
                        wrote on last edited by
                        #16

                        ps: Andre, i fixed my few posts. they are now neater to read

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          troubalex
                          wrote on last edited by
                          #17

                          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. :)

                          THE CAKE IS A LIE
                          Web Community Manager - Qt Development Frameworks

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved