gpt4 book ai didi

c++ - QDialog::accept 不响应关闭

转载 作者:搜寻专家 更新时间:2023-10-31 01:31:24 25 4
gpt4 key购买 nike

我的代码是

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) 处理

最佳答案

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

您有一个派生自 QDialog 的类 BTSettingsTab,它覆盖了 virtual void accept() 成员函数。

如果您的 void accept() 实现应执行与 QDialog::accept 相同的功能,则需要在函数末尾显式调用它。

例如:

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

关于c++ - QDialog::accept 不响应关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45905294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com