QDialog :: accept不响应关闭

时间:2017-08-27 13:08:00

标签: c++ qt sockets

我的代码是

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


}

当我尝试使用x符号关闭对话框时,它不会关闭。该怎么办?我打开了一个服务器套接字并监听连接。这可能是原因。怎么解决?

使用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用于轮询套接字描述符s。随着新连接的到来,它将存储在peersock中。然后为数据选择套接字描述符peersock。如果读取数据。如果接受描述符更多,则由while(1)

处理

1 个答案:

答案 0 :(得分:3)

QDialog::accept插槽设置已接受状态并关闭对话框。

您有一个来自BTSettingsTab的{​​{1}}课,它会覆盖QDialog成员函数。

如果virtual void accept()的实现应该与void accept()执行相同的功能,则需要在函数结束时明确调用它。

例如:

QDialog::accept