在最近的项目中,使用socket进行网络套接字编程,其中使用upd协议进行传输,在recvfrom中返回值为-1,并且错误码为10022,表示参数使用错误。在反复检查和网上资料查询后,发现是bind()函数的问题。
在C++11中,在命名空间std中添加了函数std::bind(),当需要使用socket中的bind()是,如果实现使用过了std的命名空间,会发生冲突。具体的解决方法有如下两种。
1.在使用bind()的类别中,不适用std的命名空间。避免冲突,这时可直接使用函数bind().
2.使用运算符::,在使用bind()函数时,添加运算符::,通过::bind()来进行函数调用。