There is no need to create a socket descriptor, QTcpServer::nextPendingConnection() creates a fully constructed QTcpSocket object.
Cheers,
_
There is no need to create a socket descriptor, QTcpServer::nextPendingConnection() creates a fully constructed QTcpSocket object.
Cheers,
_
Connect the newConnection signal to a newConnection Slot and accept the connection or use your code :
Good Luck.Originally Posted by Vivek1982
Last edited by toufic.dbouk; 13th November 2013 at 14:32. Reason: Code Tag
Hi..
I'm trying built application handling multiple clients by one server Qt Application. I have tried all suggestion to change the code etc.plz have a look on my unsderstanding of this concept of Multiple client in one server. how I'm approaching/trying may be wrong plz correct me.
Actually Compiling server application and client on same PC. When Server has initiated, first Client will work fine till second client will join, after that first client data is not accepted by server/not reading the first client data.It will stay on to the second client only.
.SERVER.cpp
SERVER.HQt Code:
ui1(new Ui::SERVER) { ui1->setupUi(this); connect(team_but,SIGNAL(clicked()),this,SLOT(listen())); connect(server,SIGNAL(newConnection()),this,SLOT(on_newconn())); void Server::listen() { QHostAddress hAddr; hAddr.setAddress("192.168.64.55"); qDebug("Listening to new connection"); } void Server::on_newconn() { socket=server->nextPendingConnection(); { qDebug("new connection1 established"); } connect(socket,SIGNAL(readyRead()),this,SLOT(read_socket())); } void Server::read_socket() { { if(buffer.startsWith("1")) { qDebug()<<buf; } } else { //something to do }}To copy to clipboard, switch view to plain text mode
CLIENT.cppQt Code:
private: Ui::Server *ui1; QTcpServer *server; QTcpSocket *socket; QPushbutton *team_but private slots: void listen(); void on_newconn(); void read_socket(); }To copy to clipboard, switch view to plain text mode
Qt Code:
ui(new Ui::Client) { ui->setupUi(this); connect(pb_connect,SIGNAL(clicked()),this,SLOT(connecttoserver())); connect(socket,SIGNAL(connected()),this,SLOT(on_connected())); } void Client::connecttoserver() { QHostAddress hAddr; hAddr.setAddress("192.168.64.55"); socket->connectToHost("192.168.64.55",1234); // socket->connectToHost("192.168.64.52",1234); } void Client::on_connected() { qDebug("Connected"); } void Client::write() { //Data is lineedit text socket->write(Data.toUtf8().constData()); }To copy to clipboard, switch view to plain text mode
Client.H
Qt Code:
private: Ui::TCPClient *ui; QTcpSocket *socket; private slots: void connecttoserver(); void write(); void on_connected();To copy to clipboard, switch view to plain text mode
i hope Compiling second client on same system(PC) is not issue here.
Last edited by Vivek1982; 14th November 2013 at 08:10. Reason: missing [code] tags
First, get rid of
in Server:Qt Code:
To copy to clipboard, switch view to plain text moden_newconn(). It needlessly creates a QTcpSocket and then loses the pointer to it -> leak
Qt Code:
To copy to clipboard, switch view to plain text mode
in Server::read_socket() you first have to get the socket that emitted the signal, you currently assume that only the last connected client will send data
Qt Code:
To copy to clipboard, switch view to plain text mode
Alternatively create a "client handler" object for each client's socket, i.e. a class that handles one client connection.
Cheers,
_
Vivek1982 (15th November 2013)
I think one of your issues is that you expect that if you write something on the server, it should be received by all clients, is that correct? For that you need to iterate over all client sockets and write the same data to all client sockets (once you sort out your problems with establishing proper connections).
Vivek1982 (15th November 2013)
Ya.. exactly.. my requirement is many clients will connect to server. Send data/Character as data on socket->write. After server receives a query from client, In general It will the word to all clients.Let me try now, How others can be addresses first, later I will plan for server writing to client, Now I need rectify, server accepting many client issue.
this has to be done at server alternatively rightAlternatively create a "client handler" object for each client's socket, i.e. a class that handles one client connection.
Cheers,
Yes it should be done on the server side of course. If you recall to my first post i mentioned that you should make a class user that handles the client connection including streams and sockets.
Good Luck.
Vivek1982 (15th November 2013)
Dear All,
I thank you all and Qt Forum.Finally I got the output, Server is able to listen for many clients, based on the data sent. Server is executing small functions, based on the data from client. As earlier discussed. Now I can connect two or more clients. Actually, My complete Application is , get the data from all connected clients. Based on that data sent from each client. Server will send a data/msg to all connected clients.
Here,Server code is having one socket. while it has to send data for all clients. if I call directly Write(), by QPushbutton activity Is that okay. Or in the write function I need to set some parameter to address all Or in my client application i need to change?
Client.cppQt Code:
connect(pb_clicked,SIGNAL(clicked()),this,SLOT(write()); void Server::write() { qDebug()<<data; socket->write(data.toUtf8().constData()); qDebug("server is sending msgs to client"); }To copy to clipboard, switch view to plain text mode
Qt Code:
connect(pb_clicked,SIGNAL(clicked()),this,SLOT(connectoserver()) connect(socket,SIGNAL(connected()),this,SLOT(on_connected()) connect(socket,SIGNAL(readyRead()),this,SLOT(read())); void TcpClient::connectoserver() { //connect to server } void TcpClient::on_connected() { qDebug("Connected to server"); } void TcpClient::readsocket() { } void TcpClient::read() { qDebug("start reading the socket"); qDebug("reading the socket "); te->setText(buffer); }To copy to clipboard, switch view to plain text mode
Do you mean that you have one socket sending data to several clients from the server side? And you want to call write() to send data for all clients by THAT SAME socket ?Originally Posted by Vivek1982
Not exactly at the same time, two or more clients will send data to server. Server will analyse and process the data send by all the clients. Later Server will send data to all connected clients. After receiving the data from server, All the clients update the info/data sent by the server in all Client Application.
After searching in this Qt Forum, I made attempt to write data on server by using below mentioned code, but it was not working.
Qt Code:
void Server::writetoallclients() { foreach(QtcpSocket *socket,socket) { socket->write(Data.toUtf8().constData()); qDebug("data sent to all Clients"); } }To copy to clipboard, switch view to plain text modeQt Code:
void Client::read() { qDebug("start reading the socket"); qDebug("data rxcd from server ");To copy to clipboard, switch view to plain text mode
Dear all.. Waiting for reply...application is at final stage..I can compete soon
If you want to send to all clients, you need to keep all incoming sockets, e.g. in a list, and when sending you iterate over that container and call write on all sockets.
Cheers,
_
Thanks for the reply.... I will give my attempt. . Any alternate idea to get this done by seeing my earlier posts is most welcome to all
I dont think there is any other idea to do that because to send to one client you have to send the data from server through the connected socket. Hence to send data to all clients you have to send the data through all connected sockets. Iterating over the sockets is a good idea as posted above by anda_skoa.
You can give it an attempt save all sockets in a container of some type and add sockets to it through nextPendingConnection since it will return the connected socket and then make a method for example sendToAll() where you iterate over the items and send the message one by one.
Good Luck.
Vivek1982 (19th November 2013)
Ok... I hope this may work by QList<>.
In my new connection slot I need to try like
QList<QTcpsocket*,sockets>=new QTcpsocket.
But after declaring as aQList.we have to set socket descriptor or not required? Then we can try to sendtoall().
Okay now this may work for data to all the clients.... As mentioned above post if I need to send data for specific client.not suddenly at the same time to respective client who has sent data to server...later also...how this process will happen?
As suggested I have used QList<> in server application to address all or send data to all clients. Now, all clients are able to send/read data.but, all clients all getting data twice.Any problem in my write function of Server
Qt Code:
void Server::on_newconn() { sockets.append(socket); connect(socket,SIGNAL(readyRead()),this,SLOT(read_socket())); } void Server::write() { { socket->write(CHAT.toUtf8().constData()); } qDebug("server is sending msgs to all clients"); }To copy to clipboard, switch view to plain text mode
Qt Code:
void TCPClient::read1() { te->setText(buffer); }To copy to clipboard, switch view to plain text mode
Well, alternatively to iterating over a container one could make a client handler object for each socket that has a slot for sending and have the chat text source emit a matching signal when text should be sent.
Basically this is the same as iterating, just done internally by the signal/slot system.
Cheers,
_
I have got output where many clients can be connected to one server, two way data can be sent means from server its multi cast to all connected clients...but in client now data is coming twice in line edits.how it can be solved
In post 29...in write() its data not CHAT
Last edited by Vivek1982; 16th November 2013 at 10:15. Reason: updated contents
How many connected clients do you have?
Did you check how many times is the for loop iterating?
Bookmarks