Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;超载<&书信电报;用作流的类对象的运算符_C++_Sockets_Tcp_Operators - Fatal编程技术网

C++ C++;超载<&书信电报;用作流的类对象的运算符

C++ C++;超载<&书信电报;用作流的类对象的运算符,c++,sockets,tcp,operators,C++,Sockets,Tcp,Operators,所以我浏览了很多其他的帖子,但是没有一个是关于这个主题的问题。我正在编写一个C++库来进行socket通信。我有一个TCP套接字类,它处理TCP套接字上的所有操作(设置、读/写等)。我希望能够使用>操作符在TCP套接字对象中读写数据。例如,我希望能够以这种方式向套接字写入,例如,一个double: double x; TcpSocket *sock = new TcpSocket(); //socket setup stuff here... sock << x; double

所以我浏览了很多其他的帖子,但是没有一个是关于这个主题的问题。我正在编写一个C++库来进行socket通信。我有一个TCP套接字类,它处理TCP套接字上的所有操作(设置、读/写等)。我希望能够使用>操作符在TCP套接字对象中读写数据。例如,我希望能够以这种方式向套接字写入,例如,一个double:

double x;
TcpSocket *sock = new TcpSocket();

//socket setup stuff here...

sock << x;
doublex;
TcpSocket*sock=新的TcpSocket();
//套接字设置的东西在这里。。。
sock>val;
}
其中sock.Read()和sock.Write()使用套接字文件描述符发出系统调用Read()和Write()。对这些函数进行模板化是为了能够在一个位置获取几乎任何数据类型的字节

因此,我的问题是:

  • 这些重载运算符函数是否会按我希望的方式工作
  • 如果有效,我可以将这些链接在一起,例如:


    sock要使函数正常工作,需要对输入参数和返回类型使用对
    TcpSocket
    的引用。另外,
    val
    需要是
    T常量&
    ,而不是
    T&

    template<typename T>
    TcpSocket& operator<<(TcpSocket& sock, T const&val)
    {
        unsigned char bytesOfVal[] = //parse bytes of val here...
    
        sock.Write(bytesOfVal, sizeof(bytesOfVal));
        return sock;
    }
    
    模板
    TcpSocket&operator您建议的代码

    template<typename T>
    TcpSocket operator<<(TcpSocket sock, T &val)
    {
        unsigned char bytesOfVal[] = //parse bytes of val here...
    
        sock.Write(bytesOfVal, sizeof(bytesOfVal));
    }
    
    //或std::ostream ost(新的my_插座);
    谢谢你的建议!我会记住这一点,但我不想麻烦地编写自定义流缓冲区之类的东西。在查看了您的建议(以及github上的libsocket项目及其重载>运算符的使用)之后,我想到了我在原始帖子中附带的两个函数。我很确定它们适用于任何数据类型,也适用于级联变量,例如:“tcpSocket@Timmah339,使用
    size\t buflen=sizeof(val)令人担忧。这将适用于POD类型,但不适用于
    char*
    std::string
    和其他STL容器。你必须重新考虑这个策略。谢谢,我没有考虑那个问题。我认为将
    val
    填充到stringstream中,并在调用
    read()
    write()
    时使用
    .c_str
    是更好的解决方案。我只需要知道它的长度data@Timmah339,那就行了。如果你使用这些函数,你唯一需要考虑的就是性能。你应该避免在C++中使用MeMCPY,而使用STD::复制。
    //Write to socket
    template<typename T>
    TcpSocket& operator<<(TcpSocket& sock, T &val)
    {
        size_t buflen = sizeof(val);
        unsigned char buf[buflen];
    
        memcpy(buf, &val, buflen);
    
        sock.Write(buf, buflen);
    
        return sock;
    }
    
    //Read from socket
    template<typename T>
    TcpSocket& operator>>(TcpSocket &sock, T &val)
    {
        size_t buflen = sizeof(val);
        unsigned char buf[buflen];
        int numBytesRead = 0;
    
        numBytesRead = sock.Read(buf, buflen);
    
        memcpy(&val, buf, numBytesRead);
    
        return sock;
    } 
    
    template<typename T>
    TcpSocket& operator<<(TcpSocket& sock, T const&val)
    {
        unsigned char bytesOfVal[] = //parse bytes of val here...
    
        sock.Write(bytesOfVal, sizeof(bytesOfVal));
        return sock;
    }
    
    sock << 45.8 << 33.9 << "numbers";
    
    sock << 45.8 << 33.9 << "numbers" << std::endl;
    
    TcpSocket& operator<<(TcpSocket& soc,
                          std::ostream&(*f)(std::ostream&))
    {
        std::ostringstream s;
        s << f;
        return sock << s.str();
    }
    
    template<typename T>
    TcpSocket operator<<(TcpSocket sock, T &val)
    {
        unsigned char bytesOfVal[] = //parse bytes of val here...
    
        sock.Write(bytesOfVal, sizeof(bytesOfVal));
    }
    
    TcpSocket socket ;
    socket << std::string("Hello") ;
    
    std::ostream ost ;
    ost.imbue(new my_sockect_streambuf) ;