Results 1 to 4 of 4

Thread: Communication between Client Qt4 and Server in C

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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
  •  
Qt is a trademark of The Qt Company.