Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with TextEdit
Forum Updated to NodeBB v4.3 + New Features

Problem with TextEdit

Scheduled Pinned Locked Moved General and Desktop
18 Posts 3 Posters 5.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    andre
    wrote on last edited by
    #6

    [quote author="kay2e4" date="1340719017"]
    @
    while (1)

    {
    // Call recvfrom() to get it then display the received data...
    ByteReceived = recvfrom(ReceivingSocket, ReceiveBuf, BufLength,
    0, (SOCKADDR *)&SenderAddr, &SenderAddrSize);

    if (ByteReceived > 0)
    m_pReceiver->PrintInfo("\n\nServer: Total Bytes received: "
    + QString(ByteReceived)
    + "\nServer: The data is: "
    + QString(ReceiveBuf));
    else if (ByteReceived <= 0)
    m_pReceiver->PrintInfo("Server: Connection closed with error code: "
    + WSAGetLastError());
    else
    m_pReceiver->PrintInfo("Server: recvfrom() failed with error code: "
    + WSAGetLastError());
    @
    [/quote]

    edit: Please use @ tags around code sections; Andre

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kay2e4
      wrote on last edited by
      #7

      done thanks for the advice, this is just a sample of the codes, so once i receive a packet from the sender it will display the raw data as string. I will try to post a picture of the software when running tomorrow in my school pc

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #8

        It is exactly what I thought: you're not letting the event loop do it's work. Don't use a
        @
        while (1) { }
        @
        construct. Network communications in Qt are supposed to asynchronous.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kay2e4
          wrote on last edited by
          #9

          i think i show u the entire set of codes just in case....

          @int ReceiverThread::StartReceiver()
          {
          WSADATA wsaData;
          SOCKET ReceivingSocket;
          SOCKADDR_IN ReceiverAddr, SenderAddr;
          int SenderAddrSize = sizeof(SenderAddr), Port = 5150, BufLength = 6000,
          ByteReceived = 5, SelectTiming, ErrorCode;
          char ReceiveBuf[6000];

          // Initialize Winsock version 2.2
          if( WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
          {
              m_pReceiver->PrintInfo("Server: WSAStartup failed with error %ld\n"
                                     + WSAGetLastError());
              return -1;
          }
          else
              m_pReceiver->PrintInfo("Server: The Winsock DLL status is "
                                     + QString(wsaData.szSystemStatus));
          
          // Create a new socket to receive datagrams on.
          ReceivingSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
          
          if (ReceivingSocket == INVALID_SOCKET)
          {
              m_pReceiver->PrintInfo("Server: Error at socket(): " + WSAGetLastError());
              WSACleanup();
              // Exit with error
              return -1;
          }
          else
              m_pReceiver->PrintInfo("Server: socket() is OK!");
          
          // Set up a SOCKADDR_IN structure that will tell bind that we want to
          // receive datagrams from all interfaces using port 5150.
          
          // The IPv4 family
          ReceiverAddr.sin_family = AF_INET;
          // Port no. 5150
          ReceiverAddr.sin_port = htons(Port);
          // From all interface (0.0.0.0)
          ReceiverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
          
          // Associate the address information with the socket using bind.  At this
          // point you can receive datagrams on your bound socket.
          if (bind(ReceivingSocket, (SOCKADDR *)&ReceiverAddr, sizeof(ReceiverAddr))
              == SOCKET_ERROR)
          {
              m_pReceiver->PrintInfo("Server: bind() failed! Error: " + WSAGetLastError());
              closesocket(ReceivingSocket);
              WSACleanup();
              // and exit with error
              return -1;
          }
          else
              m_pReceiver->PrintInfo("Server: bind() is OK!");
          
          // Some info on the receiver side...
          getsockname(ReceivingSocket, (SOCKADDR *)&ReceiverAddr, (int *)sizeof(ReceiverAddr));
          
          m_pReceiver->PrintInfo("Server: Receiving IP(s) used: "
                                 + QString(inet_ntoa(ReceiverAddr.sin_addr))
                                 + "\nServer: Receiving port used: "
                                 + QString(htons(ReceiverAddr.sin_port))
                                 + "\nServer: I\'m ready to receive a datagram...");
          
          SelectTiming = RecvFromTimeoutUDP(ReceivingSocket, RCV_TIMEOUT, 0);
          
          switch (SelectTiming)
          {
          case 0:
              // Timed out, do whatever you want to handle this situation
              m_pReceiver->PrintInfo("Server: Timeout while waiting for client!...");
              break;
          case -1:
              // Error occurred, maybe we should display an error message?  Need more
              // tweaking here and the recvfromTimeOutUDP()...
              m_pReceiver->PrintInfo("Server: Some error encountered with code number: "
                                     + WSAGetLastError());
              break;
          default:
              while (1) {
                  // Call recvfrom() to get it then display the received data...
                  ByteReceived = recvfrom(ReceivingSocket, ReceiveBuf, BufLength,
                                          0, (SOCKADDR *)&SenderAddr, &SenderAddrSize);
          

          if (ByteReceived > 0)
          m_pReceiver->PrintInfo("\n\nServer: Total Bytes received: "
          + QString(ByteReceived)
          + "\nServer: The data is: "
          + QString(ReceiveBuf));
          else if (ByteReceived <= 0)
          m_pReceiver->PrintInfo("Server: Connection closed with error code: "
          + WSAGetLastError());
          else
          m_pReceiver->PrintInfo("Server: recvfrom() failed with error code: "
          + WSAGetLastError());

                  // Some info on the sender side
                  getpeername(ReceivingSocket, (SOCKADDR *)&SenderAddr, &SenderAddrSize);
                  m_pReceiver->PrintInfo("Server: Sending IP used: "
                                         + QString(inet_ntoa(SenderAddr.sin_addr))
                                         + "Server: Sending port used: "
                                         + QString(htons(SenderAddr.sin_port)));
              }
          }
          
          // When your application is finished receiving datagrams close the socket.
          m_pReceiver->PrintInfo("Server: Finished receiving. Closing the listening socket...");
          if (closesocket(ReceivingSocket) != 0)
              m_pReceiver->PrintInfo("Server: closesocket() failed! Error code: "
                                     + WSAGetLastError());
          else
              m_pReceiver->PrintInfo("Server: closesocket() is OK...");
          
          // When your application is finished call WSACleanup.
          m_pReceiver->PrintInfo("Server: Cleaning up...");
          if(WSACleanup() != 0)
              m_pReceiver->PrintInfo("Server: WSACleanup() failed! Error code: "
                                     + WSAGetLastError());
          else
              m_pReceiver->PrintInfo("Server: WSACleanup() is OK");
          
          // system&#40;"PAUSE"&#41;;
          return 0;
          

          }@

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kay2e4
            wrote on last edited by
            #10

            The program is running on TextEdit but due to the disappearance of the text, I post this tread to ask whether is it a bug, and can it be solved, for now to solve it, I have to select empty area then the text will show... Is there a solution? The problem lies with the TextEdit

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #11

              Analysis stays exactly the same. Your while(1) loop is the main issue.

              1 Reply Last reply
              0
              • K Offline
                K Offline
                kay2e4
                wrote on last edited by
                #12

                I am a novice in programming, so what do you suggest I use?

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kay2e4
                  wrote on last edited by
                  #13

                  So I can assume once I solve the while(1) loop the TextEdit should have no problems right?

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #14

                    Moderators note: please edit previous comments instead of posting a new reply seconds after your last one.

                    On topic: yes, getting rid of the bussy waiting loop will solve the issue, but doing that will require a re-design of your application.

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kay2e4
                      wrote on last edited by
                      #15

                      so i just stay as it is?

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #16

                        No. You do the needed re-design. I was just trying to warn you that it might be trickyer than it looks initially to get rid of the loop. Just because something is difficult or a lot of work, does not mean it shouldn't be done or isn't worth doing.

                        1 Reply Last reply
                        0
                        • K Offline
                          K Offline
                          kay2e4
                          wrote on last edited by
                          #17

                          So I just redesign the loop change it to another function, then it will work? Is that what you mean?

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            andre
                            wrote on last edited by
                            #18

                            You need to come up with an asyncronous design. A design that allows to return to the event loop after each time you add output to your text edit. If you just move the loop to another function, nothing has changed.

                            Basically, instead of polling continuiously if there is new data to display, you need to switch to a design where either only poll at a specific interval, or much better, don't poll at all but react to a signal, event or interupt or something like that as the data actually comes in.

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved