【问题标题】:.NET UdpClient and Winsock Interoperability Issue.NET UdpClient 和 Winsock 互操作性问题
【发布时间】:2012-03-28 13:34:45
【问题描述】:

我有一个用 C++ 编写的应用程序,它使用 Winsock 作为 UDP 客户端。我正在尝试将数据发送到另一个用 C++/CLI 编写的 UDP 客户端,该客户端使用 .Net UdpClient 类。消息已成功发送,但从未收到。也就是说,永远不要从listener中出来->Receive(endPoint);在 .NET 端调用。

我将C++/CLI代码中的.Net UDPClient代码替换为Winsock,没有问题。

两台机器都运行 Windows 7(因此,.NET 4,以及 Windows 7 下的任何版本的 WSA)

这是 C++ Winsock 代码:

    SOCKET jsDataSocket;
    WSADATA WSAData;
    string dottedInetAddress = "192.168.2.100"
    int port = 5601;

bool JoystickDataPublisher::socketSetup()
{
    int iError;

    // Call "WSAStartup" and display description text
    if (iError = WSAStartup (MAKEWORD(2,2), &WSAData))
    {
        OutputDebugString(L"JoystickDataPublisher - Unable to start WinSock");
        return false;
    }
    else
        return socketSetup();

    int status;

    // create UDP socket for receiving
    jsDataSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (jsDataSocket == SOCKET_ERROR)
    {
        OutputDebugString(L"JoystickDataPublisher - Server socket creation error\n");
        return false;
    }

bool JoystickDataPublisher::sendData(const char * buf, int size)
{

    struct sockaddr_in srv_addr;
    int status;

    if (jsDataSocket == 0)
    {
        OutputDebugString(L"No socket exists yet!");
        return false;
    }

    // this is where we fill in the specific server address 
    // and port, and use our generic client socket to send
    // a message
    srv_addr.sin_family = AF_INET;
    srv_addr.sin_addr.s_addr = inet_addr (dottedInetAddress.c_str()) ;
    srv_addr.sin_port = port;

    status = sendto(jsDataSocket, (const char *)buf, size, 0, 
        (struct sockaddr*)&srv_addr, sizeof(srv_addr));

    if (status != size)
    {
        OutputDebugString(L"Message not sent Perhaps your friend is not listening");
        return false;
    }

    return true;
}

这是 .NET 端:

    // this code exists in a System::Threading::Thread

    private: UdpClient ^ listener;

private: void UpdateFunction()

    listener = gcnew UdpClient(5601);
    IPEndPoint ^ endPoint = gcnew IPEndPoint(IPAddress::Any,5601);
             array<byte> ^ receiveData = gcnew array<byte>(1024);
             JoystickData jsData;

    while(1)
    {
     endPoint->Address = IPAddress::Any;
     endPoint->Port = 5601;
     try
     {
                     **// never unblock from following call**
         receiveData = listener->Receive(endPoint);
     }
     catch (SocketException ^ e)
     {
         // got here because either the Receive failed, or more
         // or more likely the socket was destroyed by 
         // exiting from the JoystickPositionWindow form
        Console::WriteLine(e->ToString());
        return;
     }
         Marshal::Copy(receiveData,0,(IntPtr)&jsData,sizeof(jsData));

                     currentXRelPosition = jsData.xRelPosition;
                     currentYRelPosition = jsData.yRelPosition;

     Invalidate();
         }
      }

【问题讨论】:

    标签: winsock udpclient


    【解决方案1】:

    问题归结为端口的字节顺序。在Winsock这边,设置端口的时候需要用到htons(port),如下:

    srv_addr.sin_port = htons(port);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多