尝试在子线程中访问套接字描述符(使用 bind() 或 listen())时出现错误:
错误的文件描述符
这是代码(忽略标题):
class task {
private:
std::int32_t socFd;
std::string path;
char buffer[MAX_SOC_BUFFER];
struct sockaddr_un socAddr;
public:
void init() {
if ((socFd = socket((std::int32_t)AF_UNIX, (std::int32_t)SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
socAddr.sun_family = AF_UNIX;
strcpy(socAddr.sun_path, getSocPath().c_str());
}
task(const std::string& path) : path{path}
{
init();
}
auto getSocketFd() ->decltype(socFd) {
return socFd;
}
const std::string& getSocPath() {
return path;
}
auto getSocAddr() -> decltype(socAddr)&
{
return socAddr;
}
char* getBuff() {
return buffer;
}
virtual ~ task() {
close(getSocketFd());
}
};
class server_task : public task{
public:
void init () {
::unlink(getSocPath().c_str());
size_t len = sizeof(getSocAddr());
if (bind(getSocketFd(), (struct sockaddr *)&(getSocAddr()), len) == -1) {
perror("bind");
exit(1);
}
}
server_task(const std::string& path) : task(path)
{
init();
}
void operator()() {
struct sockaddr_un remote;
if (listen(getSocketFd(), 5) == -1) {
perror("listen");
exit(1);
}
for(;;) {
int done, n, socFd2;
printf("Waiting for a connection...\n");
socklen_t t = sizeof(remote);
if ((socFd2 = accept(getSocketFd(), (struct sockaddr *)&remote, &t)) == -1) {
perror("accept");
exit(1);
}
printf("Connected.\n");
done = 0;
do {
n = recv(socFd2, getBuff(), 100, 0);
if (n <= 0) {
if (n < 0) perror("recv");
done = 1;
}
if (!done) {
std::string buffStr(getBuff());
std::transform(buffStr.begin(), buffStr.end(),buffStr.begin(), ::toupper);
if (send(socFd2, buffStr.c_str(), n, 0) < 0) {
perror("send");
done = 1;
}
}
} while (!done);
close(socFd2);
}
}
};
int main(){
std::vector<std::thread> threads;
server_task server("/tmp/temp_socket");
threads.push_back(std::thread(server));
for(auto& thread : threads)
thread.join();
return 0;
}
如果我将main()
功能更改为:
int main(){
server_task server("/tmp/temp_socket");
server();
return 0;
}
不会发生此类错误。据我了解,文件描述符表在父进程及其子线程之间共享,因此套接字句柄应该是有效的。
我究竟做错了什么?