开发者

UDP stops sending after first time

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开发者_如何学C();
        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);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜