socket write()函数的问题

时间:2013-02-10 06:46:52

标签: c++ sockets

我在获取write()的参数以使套接字正常工作时遇到问题。我构建了自己的write()包装器以接受std::stringchar *。代码在下面,以及编译器错误,我做的那个愚蠢的错误,我似乎无法正常看到? TY

ftp.cpp:136:50: error: cannot convert 'std::basic_string<_CharT, _Traits, 
_Alloc>::length<char, std::char_traits<char>, std::allocator<char> >' from type 
'std::basic_string<char>::size_type (std::basic_string<char>::)()const {aka long unsigned 
int (std::basic_string<char>::)()const}' to type 'size_t {aka long unsigned int}'



int FTP::_write(std::string data)
{
    if (data != "")
    {
        int n = write(sockfd, data.c_str(), data.length); // this line is what the compiler is complaining about
        log->write("Write: " + data);
        if (n < 1)
            throw new FTPException(100, "Failed to write.");
    }
    else
    {
        int n = write(sockfd, buffer, strlen(buffer) + 1);
        log->write("Write: " + std::string(buffer));
        if (n < 1) 
            throw new FTPException(100, "Failed to write.");
    }
    return 1;
}

1 个答案:

答案 0 :(得分:3)

你需要这一行

int n = write(sockfd, data.c_str(), data.length);

阅读

int n = write(sockfd, data.c_str(), data.length());