c ++ socket - sendto - 没有收到数据

时间:2017-07-08 01:43:23

标签: c++ sockets datagram

Visual Studio 2017社区

c ++ CLR项目

应该将UDP数据报发送到多播地址。

我发送没有错误。

--------------
MULTICAST
-------------------
'Project2.exe' (Win32): Loaded 'C:\Windows\SysWOW64\mswsock.dll'. Symbols loaded.

MULTICAST ----
IP:224.0.0.2
PORT:7125
-------------

Sent: 13 


Sent: 13 


Sent: 13 

接收者获得此输入

 {"socketId":0,"data":{},"remoteAddress":"192.168.0.80","remotePort":2535}

以下是代码的发送部分

const char *msg = "1 2 3 4 5 6 7";


while (sending) {

    //swprintf(str, L"\nMSG: \n%c ", channels);
    //OutputDebugString(str);

    // Send a message to the multicasting address.
    int ret = sendto(Sock, msg, strlen(msg), 0, (struct sockaddr FAR *) &dest_sin, sizeof(dest_sin));

    swprintf(str, L"\nSent: %d \n\n", ret);
    OutputDebugString(str);

    if (ret == SOCKET_ERROR)
    {
        swprintf(str, L"\nsendto failed! Error: %d ", WSAGetLastError());
        OutputDebugString(str);

        closesocket(Sock);
        sending = false;
    }
    Sleep(66);
}

如果有帮助的完整套接字代码

// Sent message string
TCHAR szError[100];               // Error message string
SOCKET Sock = INVALID_SOCKET;     // Datagram window socket

SOCKADDR_IN source_sin,           // Source socket address
    dest_sin;             // Destination socket address

WSADATA WSAData;                  // Contains details of the 
                                  // Winsock implementation


OutputDebugString(L"\n--------------\nMULTICAST\n-------------------\n");

// Initialize Winsock. 
if (WSAStartup(MAKEWORD(1, 1), &WSAData) != 0)
{
    swprintf(str, TEXT("WSAStartup failed! Error: %d"), WSAGetLastError());
    OutputDebugString(str);
    return FALSE;
}

// Create a datagram window socket, Sock.
if ((Sock = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
{
    swprintf(str, TEXT("Allocating socket failed! Error: %d"), WSAGetLastError());
    OutputDebugString(str);
    return FALSE;
}

// Fill out source socket's address information.
source_sin.sin_family = AF_INET;
source_sin.sin_port = htons(SOURCE_PORT);
source_sin.sin_addr.s_addr = htonl(INADDR_ANY);

// Associate the source socket's address with the socket, Sock.
if (bind(Sock,
    (struct sockaddr FAR *) &source_sin,
    sizeof(source_sin)) == SOCKET_ERROR)
{
    swprintf(str, TEXT("Binding socket failed! Error: %d"), WSAGetLastError());
    OutputDebugString(str);
    closesocket(Sock);
    return FALSE;
}

// Set the Time-to-Live of the multicast.
int set_sock = setsockopt(Sock, SOL_SOCKET, SO_BROADCAST, (char FAR *)&iOptVal, sizeof(int));
if (set_sock == SOCKET_ERROR)
{
    swprintf(str, TEXT("\n`setsockopt` failed! Error: %d\n\n"), WSAGetLastError());
    OutputDebugString(str);
    closesocket(Sock);
    return FALSE;
}


// Fill out the desination socket's address information.
dest_sin.sin_family = AF_INET;
dest_sin.sin_port = ntohs(mcast_port);
dest_sin.sin_addr.s_addr = inet_addr(mcast_ip);

swprintf(str, L"\nMULTICAST ----\nIP:%s\nPORT:%d\n-------------\n", mcast_ip, mcast_port);
OutputDebugString(str);


const char *msg = "1 2 3 4 5 6 7";


while (sending) {

    //swprintf(str, L"\nMSG: \n%c ", channels);
    //OutputDebugString(str);

    // Send a message to the multicasting address.
    int ret = sendto(Sock, msg, strlen(msg), 0, (struct sockaddr FAR *) &dest_sin, sizeof(dest_sin));

    swprintf(str, L"\nSent: %d \n\n", ret);
    OutputDebugString(str);

    if (ret == SOCKET_ERROR)
    {
        swprintf(str, L"\nsendto failed! Error: %d ", WSAGetLastError());
        OutputDebugString(str);

        closesocket(Sock);
        sending = false;
    }
    Sleep(66);
}


OutputDebugString(L"\n---------------------\nMULTICAST DONE\n---------------------\n");


if (!sending) {
    // Disable sending on Sock before closing it.
    shutdown(Sock, 0x01);

    // Close Sock.
    closesocket(Sock);

    WSACleanup();
}

从我所读到的。

const char * msg

应该是正确的。?

任何建议都表示赞赏。

修改

根据Remy Lebeau

int set_sock = setsockopt(Sock,IPPROTO_IP,IP_ADD_MEMBERSHIP,(char FAR *)& iOptVal,sizeof(int));

Error: 10042 -- An unknown, invalid or unsupported option or level was specified in a getsockopt or setsockopt call.

IP_ADD_MEMBERSHIP = 5

2 个答案:

答案 0 :(得分:1)

子网广播与多播不是一回事!

您已经实现了广播代码,但您使用的是多播IP地址作为广播目标,这将无效。

在这种情况下,根本不要使用setsockopt(SOL_SOCKET, SO_BROADCAST)。相反,您必须先使用setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP)加入组播组,然后才能向组中/从组中发送/接收数据报。

阅读MSDN以获取更多详细信息:

Multicast Programming

答案 1 :(得分:0)

原来......

setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP)不是答案,数据是使用setsockopt(SOL_SOCKET, SO_BROADCAST)发送的。我测试过,EJP指出了。

问题egg on my face我忘记了控制台中的数据总是空的,直到我解析缓冲区,然后存在数据。

谢谢大家。