Results 1 to 6 of 6

Thread: Client/server application

  1. #1
    Join Date
    Mar 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Client/server application

    hi everyone,

    i am writing a network application and have one problem. I have server and two clients. The first client send message to server, when this message comes to server, the server send other message to the second client but I dont know how can I do that.

    I have this function, which send data to client
    Qt Code:
    1. void MainWindow::writeData(QTcpSocket* socket, QByteArray data)
    2. {
    3. socket->write(data);
    4. socket->flush();
    5. }
    To copy to clipboard, switch view to plain text mode 

    The first client send message START, the second send "FINISH"
    When message FINISH comes, I want to send message HELLO to the client, which sended message START.

    sorry about my english

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Client/server application

    So you know how to send data.

    Do you know how to receive data? If so, what is the problem? Post your code and point to the part which isn't working.

  3. #3
    Join Date
    Mar 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Client/server application

    yes I know how send/recieve data, but I dont know how find right socket.

    server.cpp
    Qt Code:
    1. void MainWindow::startServer()
    2. {
    3. server = new QTcpServer(this);
    4. QObject::connect(server, SIGNAL(newConnection()),this, SLOT(acceptConnection()));
    5. server->listen();
    6. }
    7.  
    8. void MainWindow::acceptConnection()
    9. {
    10. QTcpSocket* socket = server->nextPendingConnection();
    11. connect(socket, SIGNAL(readyRead()), SLOT(startRead()));
    12. }
    13.  
    14. // IN THIS FUNCTION I HAVE PROBLEM!!!!
    15. void MainWindow::startRead()
    16. {
    17. QTcpSocket* socket = qobject_cast<QTcpSocket*>(this->sender());
    18. QByteArray rawdata = socket->readAll();
    19.  
    20. if(rawdata == "FINISHREADY")
    21. {
    22. // PROBLEM
    23. // the first parameter in this function must be a socket from which comes message
    24. // STARTREADY
    25. // and I dont know how find right socket
    26.  
    27. zapsat(start, "READY");
    28. }
    29. }
    30.  
    31. //write data to socket
    32. void MainWindow::writeData(QTcpSocket* socket, QByteArray data)
    33. {
    34. socket->write(data);
    35. socket->flush();
    36. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Client/server application

    So you need to store the socket pointer when you accept the connection. Where you store it is up to you.

  5. #5
    Join Date
    Mar 2011
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Client/server application

    yes this is it, but i dont khow how to do. How to store socket pointer? I didnt find any method.

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Client/server application

    Your already storing it in local scope (QTcpSocket* socket), what you need to do is store it in a more global scope, such as in your class. Just assign, eg. clientSock = socket;

Similar Threads

  1. problem with client/server application
    By marco.stanzani in forum Newbie
    Replies: 1
    Last Post: 9th April 2011, 08:36
  2. TCP server-client application with external access
    By dominate in forum Qt Programming
    Replies: 3
    Last Post: 4th February 2011, 20:18
  3. TCP Client-Server Application
    By Tavit in forum Qt Programming
    Replies: 1
    Last Post: 18th March 2008, 14:20
  4. Client-Server Application design suggestion
    By berzeck in forum Qt Programming
    Replies: 6
    Last Post: 17th December 2007, 19:13

Tags for this Thread

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.