Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 58

Thread: Socket Prog

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

    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 ?

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

    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.

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

    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 

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

    Default Re: Socket Prog

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

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

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

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

    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

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

    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.

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

    Vivek1982 (19th November 2013)

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

    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?

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

    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 

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

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

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

    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 11:15. Reason: updated contents

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

    Default Re: Socket Prog

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

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

    Default Re: Socket Prog

    No...wat ever is the clients no, but out put is coming twice in lineedit s...I checked based on no of client connected.if no of clients is one or two or four...still text wat I send in server line edit...its appearing twice in Clint application

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

    Default Re: Socket Prog

    Check what's in the QByteArray buffer at the client side, is the message also saved twice there ?

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

    Default Re: Socket Prog

    Ya.. I checked fully,,, in my server side data of linedit is sent only once across all the clients, but where as in the client side QByteArray buffer, data is coming twice.
    E.G: Server: lineedit=HI
    Client:textedit=HIHI
    Qt Code:
    1. void TCPClient::read1()
    2. {
    3.  
    4. QByteArray buffer= socket->readLine();
    5. qDebug()<<buffer;
    6. te->setText(buffer);
    7. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void Sever::on_pushButton_clicked()
    2. {
    3. QString data=this->le->text();
    4. qDebug()<<data;
    5. foreach(QTcpSocket* socket,sockets)
    6. {
    7. socket->write(data.toUtf8().constData());
    8. qDebug()<<data;
    9. }
    10. qDebug()<<data;
    11. qDebug("server is sending msgs to clients");
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Socket Prog

    Any problem in my coding.....I'm checking for solutions all the time but no reply...why my client application receiving twice

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

    Default Re: Socket Prog

    Attach the whole project as a zip in order to investigate the problem.

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

    Default Re: Socket Prog

    Hi.. I have attached the complete project, how to address?.. data saving twice problem is resolved some how. plz go through the attachment. Plz correct me if i have gone wrong me in coding. Initially I request to read Application Text file once, before compiling code to understand the Project.
    Attached Files Attached Files

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

    Default Re: Socket Prog

    Well, there are a couple of bugs but the network stuff seems to work for me.
    Any string sent by the server is only received once by each client.

    Couple of bugs:
    * SLOT((on_conn())) has one pair of () to much, should be SLOT(on_conn())
    * you are creating invisible widgets and then throw away their pointers. waste of resources
    * send buttons in client main window are connected cross-wise, i.e. the left button sends the right line edits contents

    Cheers,
    _

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

    Default Re: Socket Prog

    Plz can I get solution fixed for this... Or atleast line level solution where I need to work to get solution....I'm getting more confused with couple of bugs...please

Similar Threads

  1. client/server prog using UDP
    By rajeshakula in forum Newbie
    Replies: 1
    Last Post: 12th July 2011, 12: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, 16:48
  3. Replies: 2
    Last Post: 11th March 2010, 10: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, 09:15
  5. Qt Prog skip the file reading block
    By dgg32 in forum Qt Programming
    Replies: 10
    Last Post: 10th December 2008, 19: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.