在std::thread中,socket()返回文件描述符1

3
当我在std::thread()内调用socket()时,它返回一个套接字描述符1。然后调用std::cout将文本发送到服务器而非终端。当我在调用socket()之前添加cout << "";时,stdin/out/err的描述符被创建,socket()返回3。
来自http://www.cplusplus.com/reference/iostream/cout/
静态初始化顺序上讲,保证在第一次构造类型为ios_base::Init的对象时,cout已经正确构造和初始化,包括<iostream>至少算作这些具有静态持续期的对象的一个初始化。默认情况下,coutstdout同步(参见ios_base::sync_with_stdio)。 std::cout应该已经初始化并与stdout同步,这就解释了为什么std::cout将消息发送到服务器而非终端。
我想知道是否在新线程中调用std::thread()会关闭stdin/out/err描述符或者这些描述符不存在于线程中,因为该线程不是由终端或initd创建的?
我在RHEL 6.4上使用GCC 4.8.2运行。下面是我的客户端代码,为了完整起见,已注释掉附加的cout << "";
Client.cpp:
#include "Client.hpp"

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <thread>
#include <vector>
#include <algorithm>
#include <sstream>
#include <functional>

#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>

using namespace std;

Client::Client( const string& hostname, const string& port ) {
    this->hostname = hostname;
    this->port = port;
}

Client::~Client() { close( fd ); }

void Client::operator()() {
    struct addrinfo hints;
    memset( &hints, 0, sizeof( struct addrinfo ) );

    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = 0;
    hints.ai_protocol = 0;

    struct addrinfo *result;
    int ret = getaddrinfo( hostname.c_str(), port.c_str(), &hints, &result );
    if( ret != 0) {
        cerr << "getaddrinfo failed: " << gai_strerror( ret ) << endl;
        return;
    }

    // cout << ""; // prevents socket() from returning 1 and redefining cout

    struct addrinfo *rp = NULL;
    for( rp = result; rp != NULL; rp = rp->ai_next ) {
        fd = socket( rp->ai_family, rp->ai_socktype, rp->ai_protocol );
        if( fd == -1 ) {
            continue;   /* Error */
        }
    
        if( connect( fd, rp->ai_addr, rp->ai_addrlen ) != -1) {
            break;      /* Success */
        }

        close( fd );        /* Try again */
    }

    if( rp == NULL ) {
        cerr << "Failed to connect to " << hostname << ":" << port << endl;
        return;
    }

    freeaddrinfo( result );

    cout << "Starting echo client" << endl;

    int i = 0;
    do {
        stringstream ss;
        ss << "Thread " << this_thread::get_id() << ": Message #" << ++i;
        string str = ss.str();

        _send( str.c_str() );
        string msg = _recv();

        //cout << "Thread " << this_thread::get_id() << " received message: " << msg << endl;
    } while ( i < 10 );

    cout << "Stopping echo client" << endl;

    close( fd );
}

string Client::_recv( ) const {
    const int BUF_SIZE = 1024;
    char* buf = ( char * ) malloc( sizeof( char) * BUF_SIZE );
    memset( buf, '\0', sizeof( char ) * BUF_SIZE );

    int bytes = recv( fd, buf, BUF_SIZE, 0 );
    if( bytes < 0 ) {
        perror( "recv failed" );
    }

    string msg = string( buf );

    free( buf );

    return msg;
}

void Client::_send( const string buf ) const {
    if( send( fd, buf.c_str(), buf.length(), 0 ) < 0 ) {
        perror( "send failed" );
    }
}
void usage() {
    cerr << "Usage: client <hostname> <port>" << endl;
    cerr << "   hostname - server name listening for incoming connctions" << endl;
    cerr << "   port - internet port to listen for connections from" << endl;
}

int main( int argc, char* argv[] ) {
    if( argc < 3 ) {
        cerr << "Not enough arguments!" << endl;
        usage();
        return EXIT_FAILURE;
    }

    vector<thread> clients;
    for( int i = 0; i < 1; i++ ) {
        clients.push_back( thread( ( Client( argv[1], argv[2] ) ) ) );
    }

    for_each( clients.begin(), clients.end(), mem_fn( &thread::join ) );

    return EXIT_SUCCESS;
}

Client.hpp:

#ifndef __CLIENT_HPP__
#define __CLIENT_HPP__

#include <string>

class Client {
    private:
        int fd;
        std::string hostname;
        std::string port;
        std::string _recv( ) const;
        void _send( const std::string buf ) const;

    public:
        Client( const std::string& hostname, const std::string& port);
        ~Client();
        void operator()();
};

#endif

1
请同时显示.hpp文件。 - pilcrow
1
这是一个标准的Rule of Three 违规。你的类持有一个资源(fd),在析构函数中销毁,你应该实现或禁止复制/移动构造/赋值。另外,在错误条件下调用freeaddrinforeturn之后,导致连接不成功时发生内存泄漏。 - Casey
1个回答

5

如果您意外关闭了stdout,即存在错误的close(1);,则通常会发生这种情况。然后FD将合法地成为编号为1的文件描述符。这可能在程序的其他位置中出现。

我遇到过这种情况多次,并且通常使用gdb并在close()上设置断点来找到它。

您的析构函数看起来可疑:

Client::~Client() { close( fd ); }

在构造函数中,您应该将fd设置为-1,并在关闭fd的任何其他地方仔细设置fd-1,如果fd==-1,则不要执行此操作。目前创建一个Client并销毁它将关闭一个随机的fd


所有的代码都发布在问题中。我使用g++ -std=gnu++11 -pthread -ggdb -Wall -c Client.cpp; g++ -std=gnu++11 -pthread -ggdb -Wall -o client Client.o;进行编译,并使用client localhost 9850启动程序。我没有使用gdb来启动程序,但是我确实启用了调试信息来编译它。 - walsht
将fd初始化为-1似乎解决了问题。我还在析构函数中添加了一个if( fd != -1 ) { close( fd ); },以防止关闭随机描述符。我会在每次调用close()时添加fd = -1;,以消除在对象被销毁时关闭随机描述符的情况。 - walsht
说句实话,虚假关闭并不是“代码的其他地方”。它发生在main函数中的for循环结束时,当客户端对象被复制到一个新对象以供新线程使用后,该对象就超出了作用域。 - pilcrow

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接