UDP 在第一次后停止发送

发布于 2024-11-05 16:43:18 字数 3047 浏览 0 评论 0原文

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

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

我不认为有什么问题。

客户端 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);

I'm using a UDP socket to transmit data from a C++ client to a c# server .
But, after first transfer from client the UDP client socket was to stop operate!

Just only client socket stops, server send data normaly.

I don't see a problem.

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);

send code

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

ok is aways -1 after first send!

server 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);

to receive:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我ぃ本無心為│何有愛 2024-11-12 16:43:18

感谢所有评论!
没有你我就无法解决。

错误就在这里

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

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

Thanks for all comments!
Without you I would not have solved.

the error is here

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

The main loop of data receivement.
After add a error message to the process. I restrict the error to length of the buffer (recBuf) .
After pass from 512bytes to 65546bytes the crash is ending.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文