Page 1 of 2 12 LastLast
Results 1 to 20 of 58

Thread: Socket Prog

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Socket Prog

    There is no need to create a socket descriptor, QTcpServer::nextPendingConnection() creates a fully constructed QTcpSocket object.

    Cheers,
    _

  2. #2
    Join Date
    Dec 2012
    Posts
    197
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    25
    Thanked 41 Times in 33 Posts

    Default Re: Socket Prog

    Connect the newConnection signal to a newConnection Slot and accept the connection or use your code :
    Quote Originally Posted by Vivek1982
    Qt Code:
    1. QTcpSocket *socket = new QTcpSocket;
    2. socket=server->nextPendingConnection();
    3. if((socket->state()==QTcpSocket::ConnectedState))
    To copy to clipboard, switch view to plain text mode 
    Good Luck.
    Last edited by toufic.dbouk; 13th November 2013 at 14:32. Reason: Code Tag

  3. #3
    Join Date
    Sep 2013
    Posts
    107
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    16
    Thanked 2 Times in 1 Post

    Default Re: Socket Prog

    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
    Qt Code:
    1. SERVER::SEVER(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui1(new Ui::SERVER)
    4. {
    5. ui1->setupUi(this);
    6. server=new QTcpServer(this);
    7. connect(team_but,SIGNAL(clicked()),this,SLOT(listen()));
    8. connect(server,SIGNAL(newConnection()),this,SLOT(on_newconn()));
    9.  
    10. void Server::listen()
    11.  
    12.  
    13. {
    14. QHostAddress hAddr;
    15. hAddr.setAddress("192.168.64.55");
    16. server->listen(QHostAddress::Any,1234);
    17. qDebug("Listening to new connection");
    18. }
    19.  
    20. void Server::on_newconn()
    21. {
    22. QTcpSocket *socket= new QTcpSocket;
    23.  
    24. socket=server->nextPendingConnection();
    25. if((socket->state()==QTcpSocket::ConnectedState))
    26. {
    27. qDebug("new connection1 established");
    28. }
    29. connect(socket,SIGNAL(readyRead()),this,SLOT(read_socket()));
    30. }
    31.  
    32. void Server::read_socket()
    33. {
    34. {
    35. QByteArray buffer= socket->readLine();
    36.  
    37. if(buffer.startsWith("1"))
    38. {
    39. QByteArray buf=buffer.mid(1);
    40. qDebug()<<buf;
    41. }
    42. }
    43. else
    44. {
    45. //something to do
    46. }}
    To copy to clipboard, switch view to plain text mode 
    SERVER.H

    Qt Code:
    1. private:
    2. Ui::Server *ui1;
    3. QTcpServer *server;
    4. QTcpSocket *socket;
    5. QPushbutton *team_but
    6.  
    7. private slots:
    8. void listen();
    9. void on_newconn();
    10. void read_socket();
    11. }
    To copy to clipboard, switch view to plain text mode 
    CLIENT.cpp
    Qt Code:
    1. Client::Client(QWidget *parent) :
    2. QWidget(parent),
    3. ui(new Ui::Client)
    4. {
    5. ui->setupUi(this);
    6.  
    7. socket=new QTcpSocket(this);
    8. connect(pb_connect,SIGNAL(clicked()),this,SLOT(connecttoserver()));
    9. connect(socket,SIGNAL(connected()),this,SLOT(on_connected()));
    10. }
    11. void Client::connecttoserver()
    12. {
    13. QHostAddress hAddr;
    14. hAddr.setAddress("192.168.64.55");
    15. socket->connectToHost("192.168.64.55",1234);
    16. // socket->connectToHost("192.168.64.52",1234);
    17. }
    18. void Client::on_connected()
    19. {
    20. qDebug("Connected");
    21. }
    22.  
    23. void Client::write()
    24. {
    25. //Data is lineedit text
    26. socket->write(Data.toUtf8().constData());
    27. }
    To copy to clipboard, switch view to plain text mode 

    Client.H
    Qt Code:
    1. private:
    2. Ui::TCPClient *ui;
    3. QTcpSocket *socket;
    4.  
    5. private slots:
    6. void connecttoserver();
    7. void write();
    8. 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

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Socket Prog

    First, get rid of
    Qt Code:
    1. QTcpSocket *socket= new QTcpSocket;
    To copy to clipboard, switch view to plain text mode 
    in Server:n_newconn(). It needlessly creates a QTcpSocket and then loses the pointer to it -> leak

    Qt Code:
    1. QTcpSocket *socket = server->nextPendingConnection();
    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:
    1. QTcpSocket *socket = qobject_cast<QTcpSocket*>( sender() );
    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,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    Vivek1982 (15th November 2013)

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Socket Prog

    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).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    Vivek1982 (15th November 2013)

  8. #6
    Join Date
    Sep 2013
    Posts
    107
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    16
    Thanked 2 Times in 1 Post

    Default Re: Socket Prog

    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.

    Alternatively create a "client handler" object for each client's socket, i.e. a class that handles one client connection.
    Cheers,
    this has to be done at server alternatively right

  9. #7
    Join Date
    Dec 2012
    Posts
    197
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    25
    Thanked 41 Times in 33 Posts

    Default Re: Socket Prog

    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.

  10. The following user says thank you to toufic.dbouk for this useful post:

    Vivek1982 (15th November 2013)

  11. #8
    Join Date
    Sep 2013
    Posts
    107
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    16
    Thanked 2 Times in 1 Post

    Default Re: Socket Prog

    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?

    Qt Code:
    1. connect(pb_clicked,SIGNAL(clicked()),this,SLOT(write());
    2. void Server::write()
    3. {
    4. QString data=this->le->text();
    5. qDebug()<<data;
    6. socket->write(data.toUtf8().constData());
    7. qDebug("server is sending msgs to client");
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 
    Client.cpp
    Qt Code:
    1. connect(pb_clicked,SIGNAL(clicked()),this,SLOT(connectoserver())
    2. connect(socket,SIGNAL(connected()),this,SLOT(on_connected())
    3. connect(socket,SIGNAL(readyRead()),this,SLOT(read()));
    4. void TcpClient::connectoserver()
    5. {
    6. //connect to server
    7. }
    8. void TcpClient::on_connected()
    9. { qDebug("Connected to server");
    10. }
    11. void TcpClient::readsocket()
    12. {
    13. }
    14. void TcpClient::read()
    15. { qDebug("start reading the socket");
    16. QByteArray buffer= socket->readLine();
    17. qDebug("reading the socket ");
    18. te->setText(buffer);
    19. }
    To copy to clipboard, switch view to plain text mode 

  12. #9
    Join Date
    Dec 2012
    Posts
    197
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    25
    Thanked 41 Times in 33 Posts

    Default Re: Socket Prog

    Quote Originally Posted by Vivek1982
    Here,Server code is having one socket. while it has to send data for all clients.
    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 ?

  13. #10
    Join Date
    Sep 2013
    Posts
    107
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    16
    Thanked 2 Times in 1 Post

    Default Re: Socket Prog

    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.

  14. #11
    Join Date
    Sep 2013
    Posts
    107
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    16
    Thanked 2 Times in 1 Post

    Default Re: Socket Prog

    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:
    1. void Server::writetoallclients()
    2. {
    3. QString Data="2allclients";
    4. foreach(QtcpSocket *socket,socket)
    5. {
    6. socket->write(Data.toUtf8().constData());
    7. qDebug("data sent to all Clients");
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void Client::read()
    2. {
    3. qDebug("start reading the socket");
    4. QByteArray buffer= socket->readLine();
    5. qDebug("data rxcd from server ");
    To copy to clipboard, switch view to plain text mode 

  15. #12
    Join Date
    Sep 2013
    Posts
    107
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    16
    Thanked 2 Times in 1 Post

    Default Re: Socket Prog

    Dear all.. Waiting for reply...application is at final stage..I can compete soon

  16. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Socket Prog

    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,
    _

  17. #14
    Join Date
    Sep 2013
    Posts
    107
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    16
    Thanked 2 Times in 1 Post

    Default Re: Socket Prog

    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

  18. #15
    Join Date
    Dec 2012
    Posts
    197
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    25
    Thanked 41 Times in 33 Posts

    Default Re: Socket Prog

    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.

  19. The following user says thank you to toufic.dbouk for this useful post:

    Vivek1982 (19th November 2013)

  20. #16
    Join Date
    Sep 2013
    Posts
    107
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    16
    Thanked 2 Times in 1 Post

    Default Re: Socket Prog

    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?

  21. #17
    Join Date
    Sep 2013
    Posts
    107
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    16
    Thanked 2 Times in 1 Post

    Default Re: Socket Prog

    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:
    1. void Server::on_newconn()
    2. {
    3. QTcpSocket *socket = server->nextPendingConnection();
    4. if((socket->state()==QTcpSocket::ConnectedState))
    5. sockets.append(socket);
    6. connect(socket,SIGNAL(readyRead()),this,SLOT(read_socket()));
    7. }
    8. void Server::write()
    9. {
    10. QString data=this->le->text();
    11. foreach(QTcpSocket* socket,sockets)
    12. {
    13. socket->write(CHAT.toUtf8().constData());
    14. }
    15. qDebug("server is sending msgs to all clients");
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void TCPClient::read1()
    2. {
    3.  
    4. QByteArray buffer= socket->readLine();
    5. te->setText(buffer);
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 

  22. #18
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Socket Prog

    Quote Originally Posted by Vivek1982 View Post
    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
    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,
    _

  23. #19
    Join Date
    Sep 2013
    Posts
    107
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    16
    Thanked 2 Times in 1 Post

    Default Re: Socket Prog

    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

  24. #20
    Join Date
    Dec 2012
    Posts
    197
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    25
    Thanked 41 Times in 33 Posts

    Default Re: Socket Prog

    How many connected clients do you have?
    Did you check how many times is the for loop iterating?

Similar Threads

  1. client/server prog using UDP
    By rajeshakula in forum Newbie
    Replies: 1
    Last Post: 12th July 2011, 11:43
  2. Build issues when qextsrialport.h is included in Linux prog
    By pupqt in forum Installation and Deployment
    Replies: 1
    Last Post: 7th April 2011, 15:48
  3. Replies: 2
    Last Post: 11th March 2010, 09:46
  4. can the image be displayed as it is in chips prog..
    By sh123 in forum Qt Programming
    Replies: 11
    Last Post: 7th February 2009, 08:15
  5. Qt Prog skip the file reading block
    By dgg32 in forum Qt Programming
    Replies: 10
    Last Post: 10th December 2008, 18:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.