【发布时间】:2015-03-20 22:39:13
【问题描述】:
目前我正在构建一个点对点网络架构,并且正处于创建接收函数的阶段,该函数将接收来自网络上多个客户端的消息。本质上,当调用 recvfrom 函数时 - 最近向主客户端发送消息的客户端的地址被加载到名为 fromAddr 的 sockaddr_in 结构中。然后,该程序被设计为遍历包含客户端类的多个实例的向量(每个实例都包含代表网络上客户端的必要信息和功能),并找到其 sockaddr_in 结构与刚刚接收到的匹配的客户端实例信息。在程序中,目前的评估是这样的:
void UDPClass::checkID(Message* mess, sockaddr_in fraeAddress)
{
sockaddr_in anAddr;
//Iterate through the vector of clients and find the one who sent the message
for(int i = 0; i < theClients.size(); i++)
{
anAddr = theClients[i].getAddress();
//if the address of the recieved message matches the address of the current client
if((anAddr.sin_addr == fraeAddress.sin_addr) && (anAddr.sin_port == fraeAddress.sin_port))
{
//Update local instance of the client so that its location data matches that of the recieved message
theClients[i].setX(mess->x);
theClients[i].setY(mess->y);
}
}
}
程序编译时报如下错误:
错误 3 错误 C2678:二进制“==”:未找到采用“IN_ADDR”类型左侧操作数的运算符(或没有可接受的转换)
正如可能推断的那样,我也尝试通过简单地比较两个 sockaddr_in 结构本身来评估表达式:
if(anAddr == fraeAddress)
报告相同的错误。问题是:如果没有创建一个带有重载运算符函数的 sockaddr_in 类来允许您评估表达式,那么实现这种比较的最简单方法是什么?
【问题讨论】:
-
听起来像
memcmp()会帮助你。仅仅比较像if(anAddr == fraeAddress)这样的指针并没有多大帮助。 -
运算符重载
标签: c++ network-programming p2p winsock2 sockaddr-in