Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. India
  4. Wriding Data to QTcpclients from QTcpserver using Socket Descriptors?????

Wriding Data to QTcpclients from QTcpserver using Socket Descriptors?????

Scheduled Pinned Locked Moved India
9 Posts 5 Posters 6.9k 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.
  • B Offline
    B Offline
    beemaneni
    wrote on last edited by
    #1

    Hi guys,

    I have developed an application where multiple clients can be connected to an server .I have stored client connections in an list.But i can send data to only an latest connection from my server where as all the other clients are connected but not able to transmit data.
    Here i got to know that, using Socket descriptors data can be sent to client .
    How can i achieve it?
    My requirement is at any point of time i should be able to send data to particular client that is saved in my list of sockets until they are connected.

    Thanks in Advance for help guys...
    Expecting solution as soon as possible as i am struck here for long

    Bala B
    Infinite Computer systems
    Chennai

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      I think it could be beneficial to check out the fortune client and server example in the "Network example section":http://qt-project.org/doc/qt-5/examples-network.html
      You can compile and run both on the same machine and it helps to understand the mechanics behind the use of QTcpServer and QTcpSocket.

      If I understand you correctly, you create a server application holding a couple of non-connected Tcp clients.

      A Tcp client is nothing more, or at least it can be reduced to, a QTcpSocket. Creating and holding a number of unconnected QTcpSockets in an application makes no sense for a server application.
      When a new connection is established to your QTcpServer instance you can get a pointer to the proper QTcpSocket for communication through "nextPendingConnection":http://qt-project.org/doc/qt-5.0/qtnetwork/qtcpserver.html#nextPendingConnection This socket respectively the pointer you may store in a list and use for your communication. Once in a while you should check the list of socket pointers for unconnected sockets and clean-up.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kpks
        wrote on last edited by
        #3

        If you go down the client list and get the specific port and ipaddress (if connecting from multiple addresses) , you can target each client specifically, connect over the tcpSocket for sending message.

        I used "fortune server example":http://qt-project.org/doc/qt-5.0/qtnetwork/threadedfortuneserver.html for 2 clients, both of which have a always on connection over localhost and different IP addresses.
        If all the clients are to get the same message a "QUdpSocket":http://qt-project.org/doc/qt-4.8/network-broadcastsender.html could be used for broadcast messages.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          beemaneni
          wrote on last edited by
          #4

          Hi koahnig,kpks
          I think i have not explained my requirement properly.Let me Explain it once again ,
          This is like a gtalk app nly.Consider you have pinged your friend for chatting and at the same time you got a ping from your other friends for chatting.So finally you have two connections established with Tcp now.So when i respond to them ,i have to find a list of connected sockets and respond to them back by selecting from list of tcp connections.I dont want to do with UdpSocket.Here i have successfully saved the connections.Consider there are n number of connections and i saved them in a list.And now i have to send data to 3rd user..what shud i do to find it?
          I am very much confused.Please help me

          Bala B
          Infinite Computer systems
          Chennai

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            just storing the pointer to the sockets are certainly not enough to support what you are trying achieve.

            You have to keep additional information about each socket. Just as brain dump, you probably need to store in the server the new connection socket's pointer together with this additional information in another object. Since QTcpSocket will inform you with a signal (readyRead) when new information is available, you need to know to which object the socket respectively its pointer belongs. This can be easily done by having a slot function receiving the initial notice of incoming information. The slot function sends another signal (e.g. sigReadyRead)now including identification information of the object and therefore also of the socket. This can be done easily by sending in addition the whole object's address (using this pointer). Alternatively you can use a string for description or whatever you like.

            Where ever you are receiving the new sigReadyRead you have to do as before, but you know also where the information came from already. No search in a list required.

            Hope that helps.

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • B Offline
              B Offline
              beemaneni
              wrote on last edited by
              #6

              Hi,
              In this case, do we need multiple sockets to handle data from multiple clients??

              Bala B
              Infinite Computer systems
              Chennai

              1 Reply Last reply
              0
              • K Offline
                K Offline
                koahnig
                wrote on last edited by
                #7

                [quote author="beemaneni" date="1390186002"]Hi,
                In this case, do we need multiple sockets to handle data from multiple clients?? [/quote]

                Yes, you do need multiple sockets.

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0
                • IamSumitI Offline
                  IamSumitI Offline
                  IamSumit
                  wrote on last edited by
                  #8

                  Hello beemaneni,

                  When you make list of connection use QMap<QString,int> map;
                  Where QString as key for value int type .
                  Store two information of the client in map i.e. name(key) and its corresponding socket descriptor/id. When a client would chat ;you need to get the name of that client in order to find its corresponding socket descriptor/id using map, like
                  Jhon wants to chat you and its socket descriptor/id is 990 then to getting its socket id use int id=map["jhon"];
                  and after getting the corresponding id ,set this is to normal socket and write it to Jhon.

                  Be Cute

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    qxoz
                    wrote on last edited by
                    #9

                    Take a look to "Asynchronous QTcpServer":http://voidrealms.com/index.php?r=tutorial/view&id=256
                    there you can find simple and good example.

                    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