UDP在第一次后停止发送

时间:2011-05-06 16:34:12

标签: c++ sockets networking udp

我正在使用UDP套接字将数据从C ++客户端传输到c#服务器。 但是,在第一次从客户端传输后,UDP客户端套接字就停止运行了!

只有客户端套接字停止,服务器正常发送数据。

我没有看到问题。

client c ++:

unsigned char IPv4[4];
if (getMyIp(IPv4))
{
    char strIP[256];
    sprintf(strIP,"%i.%i.%i.%i", (int) IPv4[0], (int) IPv4[1], (int) IPv4[2], (int) IPv4[3] );
    strcpy( Configurator::LOCAL_IP, strIP );
    //std::cout<<"IP: "<<strIP<<"\n";

    // Open windows connection
    conectado = false;

    if(WSAStartup(MAKEWORD(2,2),&WsaDat)!=0)
    {
        messagelog("Winsock error - Winsock initialization failed");
        WSACleanup();
        return 0;
    }

    //Open a datagram socket
    Socket = socket(AF_INET, SOCK_DGRAM, 0);
    if (Socket == INVALID_SOCKET)
    {
        messagelog("Could not create socket.\n");
        WSACleanup();
        return 0;
    }

    //options

    #ifndef IP_MULTICAST_TTL
        #define IP_MULTICAST_TTL 3
    #endif
    #ifndef IP_MULTICAST_LOOP
        #define IP_MULTICAST_LOOP 4
    #endif
    unsigned char one = 1;
    unsigned char ttl = 3;
    setsockopt(Socket, IPPROTO_IP, IP_MULTICAST_TTL, (const char*) &ttl,
          sizeof(unsigned char));
    setsockopt(Socket, IPPROTO_IP, IP_MULTICAST_LOOP,
                   (const char*) &one, sizeof(unsigned char));

    // Clear out server struct
    memset((void *)&SockAddr, '\0', sizeof(struct sockaddr_in));

    // Set family and port
    SockAddr.sin_family = AF_INET;
    SockAddr.sin_port = htons( Configurator::PORT ); //porta
    SockAddr.sin_addr.s_addr = inet_addr( Configurator::IP /*strIP*/ ); // ip local

    // Clear out client struct
    memset((void *)&SockAddrClient, '\0', sizeof(struct sockaddr_in));
    // Set family and port
    SockAddrClient.sin_family = AF_INET;
    SockAddrClient.sin_port = htons(0);
    SockAddrClient.sin_addr.s_addr = inet_addr( strIP ); // ip local // htonl(INADDR_ANY);

    if (bind(Socket, (struct sockaddr *)&SockAddrClient, sizeof(struct sockaddr_in)) == -1)
    {
        messagelog("Cannot bind address to socket.");
        WSACleanup();
        return 0;
    }
    server_length = sizeof(struct sockaddr_in);

发送代码

  int ok = sendto(Socket, mensagem, (int)strlen(mensagem) + 1, 0, (struct sockaddr *)&SockAddr, server_length);
    std::cout<<ok<<"\n";

首次发送后,确定离开-1!

服务器C#:

System.Net.IPEndPoint ip = new System.Net.IPEndPoint( System.Net.IPAddress.Any, PORT);
        conn = new System.Net.Sockets.Socket(
            System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
        conn.MulticastLoopback = true;

        conn.Bind(ip);

        sender = new System.Net.IPEndPoint(System.Net.IPAddress.Any,0);
        remote = (System.Net.EndPoint)(sender);
        System.Console.WriteLine("udp " + PORT);

接收:

int res = conn.ReceiveFrom(b, ref Connector.instance.remote);

1 个答案:

答案 0 :(得分:0)

感谢所有评论! 没有你我就不会解决。

错误在这里

//recebe via UDP
inDataLength = recvfrom( Socket, recBuf, BUFFDERECEBIMENTO_SIZE, 0, (struct sockaddr *)&SockAddrClient, &server_length);

数据接收的主循环。 向进程添加错误消息后。我将错误限制为缓冲区的长度(recBuf)。 从512字节传递到65546字节后,崩溃即将结束。