简体   繁体   English

套接字write()函数的问题

[英]Issues with socket write() function

I am having issues getting the arguments of write() for sockets working properly. 我在获取套接字的write()参数正常工作时遇到问题。 I built my own write() wrapper to accept std::string or char * . 我建立了自己的write()包装器来接受std::stringchar * The code is below, as well as the compiler error, what silly mistake am I making that i can't seem to see properly? 下面的代码以及编译器错误,是我犯了什么愚蠢的错误,使我似乎无法正确看到? 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;
}

You need this line 你需要这条线

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

to read 读书

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM