Wriding Data to QTcpclients from QTcpserver using Socket Descriptors?????
-
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 -
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. -
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. -
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 -
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.
-
Hi,
In this case, do we need multiple sockets to handle data from multiple clients?? -
[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.
-
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. -
Take a look to "Asynchronous QTcpServer":http://voidrealms.com/index.php?r=tutorial/view&id=256
there you can find simple and good example.