Results 1 to 4 of 4

Thread: Communication between Client Qt4 and Server in C

  1. #1
    Join Date
    Mar 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Communication between Client Qt4 and Server in C

    I have my client with Qt4 C++ and my server is in C. I am trying o send a text from server to client and client should print it out, but It does not work. Is there any way to make that happened?

    Client:
    Qt Code:
    1. void Client::readFortune()
    2. {
    3. QDataStream in(tcpSocket);
    4. //in.setVersion(QDataStream::Qt_4_0);
    5. QTextStream out ( stdout) ;
    6. if (blockSize == 0) {
    7. if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
    8. return;
    9.  
    10. in >> blockSize;
    11.  
    12. }
    13.  
    14. if (tcpSocket->bytesAvailable() < blockSize)
    15. return;
    16.  
    17. QString nextFortune;
    18. in >> nextFortune;
    19.  
    20. if (nextFortune == currentFortune) {
    21. QTimer::singleShot(0, this, SLOT(requestNewFortune()));
    22. return;
    23. }
    24.  
    25. currentFortune = nextFortune;
    26. // It should set it here
    27. statusLabel->setText( currentFortune );
    28. getFortuneButton->setEnabled(true);
    29. }
    To copy to clipboard, switch view to plain text mode 

    Server:


    Qt Code:
    1. while(1) { // main accept() loop
    2. sin_size = sizeof their_addr;
    3. new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
    4. if (new_fd == -1) {
    5. perror("accept");
    6. continue;
    7. }
    8.  
    9. inet_ntop(their_addr.ss_family,
    10. get_in_addr((struct sockaddr *)&their_addr),
    11. s, sizeof s);
    12. printf("server: got connection from %s\n", s);
    13.  
    14. if (!fork()) { // this is the child process
    15. close(sockfd); // child doesn't need the listener
    16. if (send(new_fd, "Hello, world!", 13, 0) == -1)
    17. perror("send");
    18. close(new_fd);
    19. exit(0);
    20. }
    21. close(new_fd); // parent doesn't need this
    22. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 25th March 2011 at 19:10. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Communication between Client Qt4 and Server in C

    You are sending a plain string but trying to receive it through QDataStream, it has no chance of working. Instead of blindly copying bad examples try to think how your communication should look like.
    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.


  3. #3
    Join Date
    Mar 2011
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Communication between Client Qt4 and Server in C

    Thank you for reply. I understand that. What function I need to use in order to work. Any Help please. Thank you

  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: Communication between Client Qt4 and Server in C

    Your code doesn't even make sense. It is like you are randomly copy and pasting code and hoping it'll match your requirement. For example, you are sending the standard string "Hello, World!", but then you are retrieving an integer if blockSize == 0, and you are also using incompatible methods.

    Stop trying to hack up code, read the docs and start from scratch. Learn how every functions works before trying to use it.

Similar Threads

  1. Client-Client communication
    By zgulser in forum Newbie
    Replies: 0
    Last Post: 2nd December 2010, 08:07
  2. server-client communication
    By sksingh73 in forum Newbie
    Replies: 2
    Last Post: 22nd June 2010, 03:15
  3. Qt-- Client Server communication problem
    By thisismyuname in forum Qt Programming
    Replies: 2
    Last Post: 8th January 2010, 01:04
  4. Replies: 1
    Last Post: 27th March 2009, 13:20

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.