简体   繁体   中英

QDialog::accept does not respond to close

my code is

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);


}

when I try to close the dialog box using x symbol, it does not close. What to do? I have opened a server socket and listening for connection. That may be the reason. How to solve?

modified code with select

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 is used to poll the socket descriptor s. As new connection comes, it is stored in peersock. Then select the socket descriptor peersock for data. If data comes read.If more accept descriptor comes, it is handled by while(1)

The QDialog::accept slot sets the accepted state and closes the dialog box.

You have a class BTSettingsTab derived from QDialog which overrides the virtual void accept() member function.

If your implementation of void accept() should perform the same function as QDialog::accept , you will need to call it explicitly at the end of your function.

For example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM