Results 1 to 1 of 1

Thread: QTcpSocket Help

  1. #1
    Join Date
    Oct 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Exclamation QTcpSocket Help

    Hi,
    i am trying to build a Console Socket Program with the following code :

    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include "client.h"
    3. #include <QTcpServer>
    4. #include <QTcpSocket>
    5. #include <QDebug>
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QCoreApplication a(argc, argv);
    10. Client client;
    11. return a.exec();
    12. }
    13. Client::~Client()
    14. {
    15.  
    16. }
    17.  
    18. Client::Client()
    19.  
    20. {
    21. QTcpSocket *client = new QTcpSocket(this);
    22.  
    23. Establish(client);
    24. }
    25.  
    26. void Client::Establish(QTcpSocket *client)
    27. {
    28. quint16 port=18999;
    29. client->connectToHost(QHostAddress::LocalHost , port, QIODevice::ReadWrite);
    30.  
    31. if (!client->waitForConnected()){
    32.  
    33. for(int j=0;j<10;j++)
    34. qDebug("Error");
    35.  
    36. exit(11);
    37. }
    38. getData(client);
    39. }
    40.  
    41. void Client::getData(QTcpSocket *client)
    42. {
    43.  
    44. client->waitForReadyRead();
    45. QString line;
    46. if (client->canReadLine()) {
    47. line = client->readLine();
    48. }
    49. else
    50. qDebug("Error");
    51.  
    52. qDebug()<<line;
    53. if(!client->waitForConnected())
    54. stop(client);
    55. else
    56. Establish(client);
    57. }
    58. void Client::stop(QTcpSocket *client)
    59. {
    60. client->flush();
    61. client->waitForBytesWritten();
    62.  
    63. client->close();
    64. client->disconnectFromHost();
    65. client->waitForDisconnected();
    66.  
    67. }
    To copy to clipboard, switch view to plain text mode 

    Server:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include "server.h"
    3. #include <QTcpServer>
    4. #include <QTcpSocket>
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QCoreApplication a(argc, argv);
    9. MultiClientServer serve;
    10. return a.exec();
    11. }
    12.  
    13. MultiClientServer::MultiClientServer()
    14. {
    15. start();
    16. connect(this, SIGNAL(newConnection()), this, SLOT(handleNewConnection()));
    17.  
    18. }
    19.  
    20. void MultiClientServer::handleNewConnection()
    21. { qDebug("Somethin");
    22. client = nextPendingConnection();
    23. connect(client, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
    24.  
    25. sendHello(client);
    26. }
    27.  
    28. void MultiClientServer::clientDisconnected()
    29. {
    30. client->deleteLater();
    31. }
    32.  
    33. void MultiClientServer::sendHello(QTcpSocket *client)
    34. {
    35. if (!client)
    36. return;
    37.  
    38. client->write("1");
    39.  
    40. }
    41.  
    42. MultiClientServer::~MultiClientServer()
    43. {
    44. stop();
    45. }
    46. //The stop function closes the connections. The server will not listen anymore. Any client trying to connect will time out. Use the QTcpServer::close() function to stop listening.
    47.  
    48. void MultiClientServer::stop()
    49. {
    50. close();
    51. }
    52.  
    53. //The start function makes the server listen on a certain hostname and a certain port. In this case the hostname and port are not variable, the server will listen on localhost on the port 1234. You can of course create a settings dialog where you can change these settings.
    54.  
    55.  
    56. void MultiClientServer::start()
    57. {
    58. qDebug("Server Started");
    59. listen(QHostAddress::LocalHost, 18999);
    60. }
    To copy to clipboard, switch view to plain text mode 


    Someone Please Help me !

    • I want to transfer Integer Values but the write only allows QString/Character!!
    • Secondly I have some error cause the client only recieves data once.
    • By changing the localHost with the IP address should send the data to that IP address right?
    • Suggestions to convert this into GUI



    OKAY I FOUND OUT THAT EVERY TIME MY SERVER SENDS THE MESSAGE IT DISCONNECTS WITH THE CLIENT!
    Last edited by minotaurds; 20th October 2011 at 11:29. Reason: discovery:

Similar Threads

  1. QTcpSocket
    By ken536 in forum Qt Programming
    Replies: 5
    Last Post: 18th May 2011, 18:38
  2. QTcpSocket
    By Fallen_ in forum Qt Programming
    Replies: 5
    Last Post: 26th August 2010, 05:32
  3. QTcpSocket help
    By tsd-charlie in forum Newbie
    Replies: 1
    Last Post: 6th August 2009, 19:40
  4. QTcpSocket
    By pdoria in forum Qt Programming
    Replies: 1
    Last Post: 16th July 2009, 17:52
  5. QT4.2.2 and QTcpSocket
    By Nyphel in forum Qt Programming
    Replies: 13
    Last Post: 11th March 2007, 11:30

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.