English 中文(简体)
A. 与袖珍地址比较的问题
原标题:Issue with Comparing socket addresses

目前,我正在开发一个UDP应用程序,使两名用户能够使用双赢图书向对方说话。 目前,在我实施该方案时,它首先可以挽救病媒的创伤,然后当用户发出信息时,它会把地址与病媒中的第一个地址相比较。 当我辩论和比较所比较的价值观时,这些价值观恰恰是一样的,然而,我是否承认其他价值观(认为,这些地址彼此平等)。

这是我拥有的法典:

#include <WinSock2.h>

sockaddr    clientAddress;

recvfrom( hSocket, msg, MAXLINE, 0, &clientAddress, &cbClientAddress );

myVector.pushback(clientAddress);

if (&clientAddresses[0] == &clientAddress)
{
//is the same address
}
else
{
//not the same address
}

Ive还尝试在后使用.sa_data更为具体地使用clientAddress[0&clientAddress.

成就

最佳回答

多个问题:

  • 在请<代码>recv from之前,您必须首先将cbClientAddress (socklen_t) to the number of bytes of address information You are prepared to receive, as this:

     struct sockaddr clientAddress;
     socklen_t cbClientAddress;
    
     cbClientAddress = sizeof(clientAddress);
     recvfrom(hSocket, msg, MAXLINE, 0, &clientAddress, &cbClientAddress);
    
  • 电话之后,cbClientAddress将超过收到地址的实际长度。 这将低于sizeof(struct sockaddr)。 事实上,这等于sizeof(struct sockaddr_in),因为这是一个UDP/IP的袖珍。

  • 你必须仅比较实际包含数据结构的一部分,而不是整个结构。 结构中未使用部分(在“条形”和“条形形形形形形形形形色色色”之间)可能为停车场。 你不希望加以比较。 这样做不仅需要把结构本身的内容放在一个媒体上,而且需要相当长的时间。

  • 在对照你刚刚收到的地址比较所节省的地址时,使用这种假装。 Don t试图比较整个结构(包括跟踪未使用部分)。

    if (
        (saved_length == this_length) &&
        (memcmp(saved_sockaddr, this_sockaddr, this_length) == 0)
    ) {
        it s a match
    }
    
  • 页: 1 如果结构的地址是平等的,则进行检查。 这意味着,请你重新测试,看看它是否拥有同一结构<>?>,而不是你想要的,如果它有,则该结构具有相同的内容。 使用<条码> 上文的假冒代码比较内容。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签