2017-08-27 4 views
2

meinen Code schließenQDialog :: accept reagiert nicht

ist
void BTSettingsTab::accept() 
{ 
    struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 }; 
    char buf[1024] = { 0 }; 
    char send[64] = {0}, temp[32]; 
    int s, client, bytes_read, bytes_written; 
    socklen_t opt = sizeof(rem_addr); 
    s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); 

    // bind socket to port 1 of the first available 
    // local bluetooth adapter 
    loc_addr.rc_family = AF_BLUETOOTH; 
    loc_addr.rc_bdaddr = (bdaddr_t){{0,0,0,0,0,0}};//*BDADDR_ANY; 
    loc_addr.rc_channel = (uint8_t) 1; 
    bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)); 

    // put socket into listening mode 
    listen(s, 1); 
    // accept one connection 
    client = ::accept(s, (struct sockaddr *)&rem_addr, &opt); 
    ba2str(&rem_addr.rc_bdaddr, buf); 
    fprintf(stderr, "accepted connection from %s\n", buf); 
    memset(buf, 0, sizeof(buf)); 
    strcat(send,passwd); 
    strcat(send,","); 
    snprintf(temp, sizeof(temp), "%ld", mobile_num); 
    strcat(send,temp); 
    strcat(send,","); 
    snprintf(temp, sizeof(temp), "%ld", imei); 
    strcat(send,temp); 
    //Thread_data * _data = new Thread_data(&s, send); 
    //pthread_create(&threadId,0,writeThread,_data); 


} 

wenn ich versuche, um das Dialogfeld mit x-Symbol zu schließen, hat es nicht in der Nähe. Was ist zu tun? Ich habe einen Server-Socket geöffnet und auf Verbindung gewartet. Das könnte der Grund sein. Wie löst man?

modifizierte Code mit ausgewählten

void BTSettingsTab::accept() 
{ 
    int MAX_BUFFER_SIZE = 64; 
    struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 }; 
    char buf[1024] = { 0 }; 
    char send[64] = {0}, temp[32]; 
    int s, client, bytes_read, bytes_written, maxfd; 
    int srvsock, peersock, j, result, result1, sent, len; 
    fd_set readset, tempset; 
    char buffer[MAX_BUFFER_SIZE+1]; 
    socklen_t opt = sizeof(rem_addr); 
    s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); 

    // bind socket to port 1 of the first available 
    // local bluetooth adapter 
    loc_addr.rc_family = AF_BLUETOOTH; 
    loc_addr.rc_bdaddr = (bdaddr_t){{0,0,0,0,0,0}};//*BDADDR_ANY; 
    loc_addr.rc_channel = (uint8_t) 1; 
    bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)); 

    // put socket into listening mode 
    listen(s, 1); 
    // accept one connection 
    FD_ZERO(&readset); 
     FD_SET(s, &readset); 
     maxfd = s; 
     do { 
      memcpy(&tempset, &readset, sizeof(tempset)); 

      result = select(maxfd + 1, &tempset, NULL, NULL, NULL); 

      if (result == 0) { 
      printf("select() timed out!\n"); 
      } 
      else if (result < 0 && errno != EINTR) { 
      printf("Error in select(): %s\n", strerror(errno)); 
      } 
      else if (result > 0) { 

      if (FD_ISSET(s, &tempset)) { 
       len = sizeof(rem_addr); 
       socklen_t opt = sizeof(rem_addr); 

       peersock = ::accept(s, (struct sockaddr *)&rem_addr, &opt); 
       if (peersock < 0) { 
        printf("Error in accept(): %s\n", strerror(errno)); 
       } 
       else { 
        FD_SET(peersock, &readset); 
        maxfd = (maxfd < peersock)?peersock:maxfd; 
       } 
       FD_CLR(s, &tempset); 
      } 

      for (j=0; j<maxfd+1; j++) { 
       if (FD_ISSET(j, &tempset)) { 

        do { 
         result = recv(j, buffer, MAX_BUFFER_SIZE, 0); 
        } while (result == -1 && errno == EINTR); 

        if (result > 0) { 
         buffer[result] = 0; 
         printf("Echoing: %s\n", buffer); 
         sent = 0; 

         do { 
         result1 = ::send(j, buffer+sent, result-sent, MSG_NOSIGNAL); 
         if (result1 > 0) 
          sent += result1; 
         else if (result1 < 0 && errno != EINTR); 
          break; 
         } while (result > sent); 

        } 
        else if (result == 0) { 
         ::close(j); 
         FD_CLR(j, &readset); 
        } 
        else { 
         printf("Error in recv(): %s\n", strerror(errno)); 
        } 
       }  // end if (FD_ISSET(j, &tempset)) 
      }  // end for (j=0;...) 
      }  // end else if (result > 0) 
     } while (1); 
    //client = ::accept(s, (struct sockaddr *)&rem_addr, &opt); 
    /*ba2str(&rem_addr.rc_bdaddr, buf); 
    fprintf(stderr, "accepted connection from %s\n", buf); 
    memset(buf, 0, sizeof(buf)); 
    strcat(send,passwd); 
    strcat(send,","); 
    snprintf(temp, sizeof(temp), "%ld", mobile_num); 
    strcat(send,temp); 
    strcat(send,","); 
    snprintf(temp, sizeof(temp), "%ld", imei); 
    strcat(send,temp);*/ 
    //Thread_data * _data = new Thread_data(&s, send); 
    //pthread_create(&threadId,0,writeThread,_data); 

    QDialog::accept(); 
} 

select wird verwendet, um die Socket-Deskriptor s abzufragen. Wenn eine neue Verbindung kommt, wird sie in Peersock gespeichert. Wählen Sie dann den Socket-Deskriptor Peersock für Daten. Wenn Daten kommen lesen.Wenn mehr akzeptieren Deskriptor kommt, wird es von behandelt, während (1)

+0

Wenn 'BTSettingsTab' von' QDialog' abgeleitet ist, sollten Sie umbenennen 'Leere BTSettingsTab :: accept()', um etwas, das nicht 'QDialog :: accept' außer Kraft setzen wird. –

+0

ja BTSettingsTab ist von QDialog abgeleitet. Aber ich habe nicht verstanden, wie Sie BTSettingsTab :: renewed (mit typedef oder was) – chinmay

+0

Wenn ich den Namen accept in accept1 ändern, wird es nicht die Schaltfläche OK von QDialogButtonBox aufrufen. Ich verstehe nicht, was du sagen willst – chinmay

Antwort

2

Der QDialog::accept Slot setzt den akzeptierten Zustand und schließt das Dialogfeld.

Sie haben eine Klasse BTSettingsTab, abgeleitet von QDialog, die die virtual void accept() Elementfunktion überschreibt. Wenn Ihre Implementierung von void accept() die gleiche Funktion wie QDialog::accept ausführen soll, müssen Sie sie explizit am Ende Ihrer Funktion aufrufen.

Zum Beispiel:

void BTSettingsTab::accept() 
{ 
    ... 
    QDialog::accept(); 
} 
+0

Ich habe QDialog :: am Ende akzeptiert, aber das Problem ist immer noch – chinmay

+0

Wenn ich den Socket-Code entfernen, funktioniert es gut – chinmay

+0

Just Blick auf Ihren Code wieder, tut ' :: akzeptieren? –